I am trying to build multiple Docker images with less code duplication.
Every step is doing:
- docker build -t ${ECR_ENDPOINT}/${IMAGE_NAME}:${BITBUCKET_BRANCH} .
- docker push ${ECR_ENDPOINT}/${IMAGE_NAME}:${BITBUCKET_BRANCH}
But it is not possible to set IMAGE_NAME variable per step.
Maybe if BITBUCKET_STEP_NAME is available, I can put the docker image name there ?
https://confluence.atlassian.com/bitbucket/variables-in-pipelines-794502608.html
Right now I am hacking with:
- if [[ $BITBUCKET_PARALLEL_STEP -eq 0 ]];then IMAGE_NAME="image";fi
- if [[ $BITBUCKET_PARALLEL_STEP -eq 1 ]];then IMAGE_NAME="another-image";fi
- if [[ $BITBUCKET_PARALLEL_STEP -eq 2 ]];then IMAGE_NAME="image-foo";fi
- aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${ECR_ENDPOINT}/${IMAGE_NAME}
- cd ${IMAGE_NAME}
- docker build -t ${ECR_ENDPOINT}/${IMAGE_NAME}:${BITBUCKET_BRANCH} .
- docker push ${ECR_ENDPOINT}/${IMAGE_NAME}:${BITBUCKET_BRANCH}
Hello @[deleted] ,
Welcome to the Community!
I tagged your question so that Pipelines team will see it sooner.
I'm not saying this is invalid – it totally makes sense to me, – but in this particular case it might be easier (in terms of maintenance) to switch to build scripts stored somewhere in your repository which then are called by Pipelines steps. You'll get all the power of whatever script language you'll be using, while Pipelines will orchestrate the process by pulling the right threads.
So each of the parallel steps would call the very same script with a single parameter – image name.
Does this make sense?
Cheers,
Daniil
Hello Daniil ,
Thanks for this solution, I might use it for more complex scenarios.
The problem here is that I won't be able to do this even with a script:
- step:
name: my-image
script: *build-push
services:
- docker
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hm, I didn't get this, sorry.
What I meant was you can create a script like this:
#!/bin/bash
IMAGE_NAME=$1
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${ECR_ENDPOINT}/${IMAGE_NAME}
cd ${IMAGE_NAME}
docker build -t ${ECR_ENDPOINT}/${IMAGE_NAME}:${BITBUCKET_BRANCH} .
docker push ${ECR_ENDPOINT}/${IMAGE_NAME}:${BITBUCKET_BRANCH}
and save it in your repository, let's say as bin/docker-build-push.sh (don't forget to commit it with executable bit on, otherwise you'd need to chmod +x in the pipeline).
Now, the the parallel steps in your pipeline would look like this:
- parallel:
- step:
services:
- docker
script:
- bin/docker-build-push.sh "image"
- step:
services:
- docker
script:
- bin/docker-build-push.sh "another-image"
- step:
services:
- docker
script:
- bin/docker-build-push.sh "image-foo"
This is an equivalent of what you posted in the original message, if I got it right, and it solves duplication problem.
Hope this helps.
Cheers,
Daniil
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.