Problem
The following is to do with GitLab CI/CD.
I have two stages. build and test.
In the build stage, I am currently building a docker image to my GCP Artifact Registry with the $CI_COMMIT_SHA as its tag and the $CI_PROJECT_NAME as its image name. This works great and pushes just fine.
I am having trouble with the test stage. I have the following issues:
How can I authorise the pulling of the newly built image from my Artifact Registry to be used to run the
testjob in?How do I specify the specific image tag to use?
NB
I do not want to use docker-in-docker as it is considered bad practice and requires my runners to run in privileged mode. Therefore, I will be building my images using GCP's Cloud Build.
What doesn't work
I have found this link containing a closed issue for transferring environment variables across jobs.
This issue of authorising the the pull of the image from GCP Artifact Registry also poses some issues for similar reasons. In the past, I have authorised applications pulling docker images via a short-living GCP auth access token obtained via:
export ACCESS_TOKEN=$(gcloud auth print-access-token)
However, this won't work for a similar reason as the aforementioned:
test:
stage: test
image: https://oauth2accesstoken:${ACCESS_TOKEN}@${LOCATION}-docker.pkg.dev/${PROJECT_ID}/docker-repo/${IMAGE_NAME}:${IMAGE_TAG}
...
Summary
So in other words, I want to achieve the following, except that it works:
build: # builds image to GCP Artifact Registry. This already works.
stage: build
image: google/cloud-sdk
...
test: # run some unit tests using the image built in the `build` stage to mimic the production environment
stage: test
image: https://oauth2accesstoken:${ACCESS_TOKEN}@${LOCATION}-docker.pkg.dev/${PROJECT_ID}/docker-repo/${IMAGE_NAME}:${IMAGE_TAG}
...
How do I do this? Please help. Many thanks in advance!
"prerequisite" answers to unasked questions I assume will help you out:
user:pass@host/path).With that out of the way...
Depends on how "granular" you want to be. If you want absolutely always-repeatable pipeline runs without ANY clashing possibility; use the short commit sha (
CI_COMMIT_SHORT_SHA) as the container tag. Though I would recommend you useCI_COMMIT_REF_SLUGin order to not have hundreds of tags in the registry, but still differentiate between refs (branches & tags).You can authenticate specific CI jobs by setting the
DOCKER_AUTH_CONFIGenv var. You can combine this with the previously-mentioned Dynamic child pipelines and you're good to go.