-1

This is my first experience with gitlab. I need to create additional branches for the "service" project: test, stage, and prod. ECR, ECS, and Task Definition should be dedicated to each environment, as well as ECS clusters. I have a master branch and I add variables in child settings. Do I need to add all the variables there or prescribe each variable in the YAML file? I have requirements:

  • Build stage:
    • tagging must include hash_sum (only commitID must be selected) and 'latest' keyword
    • keep artifact to use it on second step
  • Push stage:
    • use aws cli to push image into appropriate repository (dedicated ECR repos for every service)
  • Deploy stage:
    • Check if the previous revision of TD running , if so, stop the task first, then deploy a new revision
    • Parse the deploy output:
    • leave only TD name and IP address in the deploy output.

script:

    - docker build -t $CI_AWS_ECR_SERVICE/$REPOSITORY_NAME:$CI_COMMIT_SHORT_SHA .
    - docker save -o my-artifact.tar $CI_AWS_ECR_SERVICE/$REPOSITORY_NAME:$CI_COMMIT_SHORT_SHA
  artifacts:
    paths:
      - my-artifact.tar
    - echo "Logging in to Amazon ECR..."
    - aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
    - aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
    - aws configure set region $AWS_DEFAULT_REGION
    - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $CI_AWS_ECR_SERVICE
    - docker load -i my-artifact.tar
    - docker tag $CI_AWS_ECR_SERVICE/$REPOSITORY_NAME:$CI_COMMIT_SHORT_SHA $CI_AWS_ECR_SERVICE/$REPOSITORY_NAME:$CI_COMMIT_SHA
    - docker push $CI_AWS_ECR_SERVICE/$REPOSITORY_NAME:latest
   # - echo "Logging in to Amazon ECS and update service..."
   # - aws ecs update-service --cluster $CI_AWS_ECS_CLUSTER --service $CI_AWS_ECS_SERVICE --force-new-deployment --desired-count 1 --region $AWS_DEFAULT_REGION
    - aws ecs describe-task-definition --task-definition $CI_AWS_ECS_TASK_DEFINITION --output json --query taskDefinition > task-definition.json
    - python replace-container-defn-img.py task-definition.json $CI_AWS_ECR_SERVICE/$REPOSITORY_NAME:latest
    - aws ecs register-task-definition --cli-input-json file://task-definition.json
    - aws ecs update-service --cluster $CI_AWS_ECS_CLUSTER  --service $CI_AWS_ECS_SERVICE --task-definition $CI_AWS_ECS_TASK_DEFINITION  --force-new-deployment --desired-count 1 --region $AWS_DEFAULT_REGION

all these variables from the master branch, how can i add variables from other branches?

Pedro Queiroga
  • 381
  • 1
  • 12
Jennifer
  • 15
  • 3

1 Answers1

0

You could use rules:variables to change the value of the variables depending on some condition, like the branch name.

from the doc:

job:
  variables:
    DEPLOY_VARIABLE: "default-deploy"
  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
      variables:                              # Override DEPLOY_VARIABLE defined
        DEPLOY_VARIABLE: "deploy-production"  # at the job level.
    - if: $CI_COMMIT_REF_NAME =~ /feature/
      variables:
        IS_A_FEATURE: "true"                  # Define a new variable.
  script:
    - echo "Run script with $DEPLOY_VARIABLE as an argument"
    - echo "Run another script if $IS_A_FEATURE exists"

Another option is to use environments, but I prefer rules:variables since the deploys are identical except for the variable values. If you end up with a very long variable list that needs to be appended to many jobs, then you could use yaml tricks to avoid duplication

Pedro Queiroga
  • 381
  • 1
  • 12