The problem arises when you want to use variables in different situations. A excellent example would be if you wanted to include the URLs of your production, staging, and development databases in the same task but didn't want to write separate jobs for each environment.
When a single procedure (deploy to s3) necessitates multiple jobs for different environments, we have a problem. As a result, managing multiple jobs for a single procedure takes time. So I came up with a solution based on a workflow job. Using a workflow job and a rule condition, we can integrate variable values based on branch. As a result, instead of writing jobs for each environment, a single job can handle multiple environment credentials.
If you have any questions about this topic, please contact me at email@example.com.
A sample of code is provided below:
image: node:latest variables: GIT_DEPTH: '0' stages: - build - deploy workflow: rules: - if: $CI_COMMIT_REF_NAME == "develop" variables: DEVELOP: "true" ENVIRONMENT_NAME: Develop WEBSITE_URL: DEVELOP_WEBSITE_URL S3_BUCKET: (develop-s3-bucket-name) AWS_REGION: ************** develop AWS_ACCOUNT: ********develop - if: $CI_COMMIT_REF_NAME == "main" variables: PRODUCTION: "true" ENVIRONMENT_NAME: PRODUCTION WEBSITE_URL: $PROD_WEBSITE_URL S3_BUCKET: $PROD-S3-BUCKET-NAME AWS_REGION: ************** (prod-region) AWS_ACCOUNT: ***********(prod-acct) - when: always build-app: stage: build script: #build-script deploy-app: stage: deploy script: #deploy-script - echo "$S3_BUCKET" - echo "$WEBSITE_URL" environment: - name: $ENVIRONMENT_NAME