variablesgitlabcicd

gitlab variable not picked up in feature branch cicd


I am trying to run gitlab cicd using a feature branch but the variables which I have defined in the variable section is not picked up on the cicd

variables:
    IMAGE_NAME: $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/juice-shop
    DOCKER_PASS: $CI_COMMIT_SHA
    IMAGE_TAG: juice-shop-1.1
    DOCKER_USER: $DOCKER_USER
    AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
    AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
    AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
    AWS_ACCOUNT_ID: $AWS_ACCOUNT_ID
    SERVER_IP: $SERVER_IP
    SERVER_USER: ubuntu

Define Stages

stages:
    - cache
    - test
    - build
    - deploy
    - upload_reports

Install dependencies

create_cache:
    image: node:18-bullseye
    timeout: 2h
    stage: cache
    script:
        - yarn install
    cache:
        key:
            files:
                - yarn.lock
        paths:
            - node_modules/
            - yarn.lock
            - .yarn
        policy: pull-push
    tags:
      - shell
      - ec2

Build and push image to repository

build_image_and_push:
    stage: build
    # image: docker:24
    # services:
    #     - docker:24-dind
    tags:
      - shell
      - ec2
    before_script:
        - aws ecr get-login-password | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
        # - echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
    script:
        - docker build -t $IMAGE_NAME:$IMAGE_TAG -t $IMAGE_NAME:latest  .
        - docker push $IMAGE_NAME:$IMAGE_TAG 
        - docker push $IMAGE_NAME:latest 

Deploy application

deploy_stage:
    stage: deploy
    image: debian:bullseye-slim
    needs: ["build_image_and_push", "trivy"]
    before_script:
        - apt update -y && apt install openssh-client -y
        - eval $(ssh-agent -s)
        - chmod 400 "$SSH_PRIVATE_KEY"
        - ssh-add "$SSH_PRIVATE_KEY"
        - mkdir -p ~/.ssh
        - chmod 700 ~/.ssh
        - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
        - ssh-keyscan -H $SERVER_IP >> ~/.ssh/known_hosts
        - aws ecr get-login-password | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com

    script:
        - ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker pull $IMAGE_NAME:latest"
        - ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker stop juice-shop && docker rm juice-shop || true"
        - ssh -o StrictHostKeyChecking=no  $SERVER_USER@$SERVER_IP "docker run -d -p 3000:3000 --name juice-shop $IMAGE_NAME:latest"
    tags:
      - shell
      - ec2

How do I solve this issue


Solution

  • this can happen if the variable in the ci/cd settings section is marked as protected
    did you check the Protect Variable checkbox in the variable settings?
    another possibility is that your feature branch needs to be marked as protected in the repository’s branch settings.