I have 3 branches dev
, qa
and prod
and 3 corresponding environments development
, testing
and production
. So when code is merged into specific branch I want to build only that branch and deploy to corresponding environment
dev
-> development
qa
-> testing
prod
-> production
dev
branch is the default branch for the repository.
Using only
attribute, I was able to deploy to specific environment based on which branch the code is merged.
But in the build
stage I am not able to figure out, how to tell gitlab to pull specific branch where the code is checked in.
When cicd pipeline pulls the code on the runner, is it always going to pull the code from the default branch or is it going to pull the code from the branch where the code checked in?
Here is my current YAML
default:
image: node:14
tags:
- my-runner
stages:
- build
- deploy
build-job:
stage: build
script:
- npm install
- npm run build:prod
- echo "Compile complete."
artifacts:
paths:
- deploy/build.zip
deploy-dev:
image: docker.xxxx/awscli
stage: deploy
environment:
name: development
script:
- aws s3 cp deploy/build.zip s3://dev-bucket
- aws lambda update-function-code --function-name dev-lambda --s3-bucket dev-bucket --s3-key build.zip --region us-west-2
only:
- dev
deploy-testing:
image: docker.xxxx/awscli
stage: deploy
environment:
name: testing
script:
- aws s3 cp deploy/build.zip s3://qa-bucket
- aws lambda update-function-code --function-name qa-lambda --s3-bucket qa-bucket --s3-key build.zip --region us-west-2
only:
- qa
deploy-production:
image: docker.xxxx/awscli
stage: deploy
environment:
name: production
script:
- aws s3 cp deploy/build.zip s3://production-bucket
- aws lambda update-function-code --function-name production-lambda --s3-bucket production-bucket --s3-key build.zip --region us-west-2
only:
- prod
Generally speaking, you don't. Pipelines inherently belong to a specific git branch/ref and the GitLab runner clones the appropriate ref automatically before the build begins.
For example, if you push a single commit to the master
branch, this will trigger a pipeline belonging to the HEAD (the new commit) on the master
branch for that push event. When jobs in this pipeline run, the GitLab runner will checkout this specific ref automatically for each job. You can see this clearly in the pipeline UI: each pipeline is associated with a specific branch/ref:
In other words, it is this association between the ref and pipeline that determines how the runner checks out the relevant code from the repository. You do not need to specify it in your .gitlab-ci.yml
.