I have a Gitlab CI pipeline to build and publish a Docker container. I want to push the container image to an undefined number (“N”) of repositories. I originally had something like this:
build-and-push:
image: docker:cli
services:
- docker:dind
parallel:
matrix:
- REGISTRY: $REGISTRY1
USERNAME: $USER1
PASSWORD: $PASS1
- REGISTRY: $REGISTRY2
USERNAME: $USER2
PASSWORD: $PASS2
script: |
docker build --file Dockerfile --tag "$REGISTRY/my-image" .
echo "$PASSWORD" | docker login --username "$USERNAME" --password-stdin "$REGISTRY"
docker push "$REGISTRY/my-image"
But this does the docker build
for each job, which takes some time and it’s unnecessary to duplicate that part when I only want the docker push
part duplicated.
I tried doing docker build
in one job and docker push
in a subsequent job. But in the second job it sometimes locally sees the image built in the first job, and sometimes it doesn’t (not sure why).
I considered saving to artifacts as a tar
file and passing that to docker import
/push
, but those images can get to multiple GB which seems like overkill for an artifact.
So what’s the right approach here?
4