snippetMinor
How to avoid code duplication in bitbucket-pipelines?
Viewed 0 times
pipelinesduplicationbitbucketavoidhowcode
Problem
The following bitbucket-pipelines file has been created:
When it is run by bitbucket, it indicates that the authentication to the docker registry fails so now this step has to be added over and over again:
Is it possible to prevent such duplication? An attempt has been made (see first code snippet), but that does not seem to work, i.e. a docker login is required by every step.
options:
docker: true
pipelines:
default:
- step:
script:
- echo "Login to the docker registry"
- docker login some-docker-registry
- step:
script:
- echo "Build some-image"
- docker build some-docker-registry/some-image .
- docker push some-docker-registry/some-image
- step:
script:
- echo "Build some-image2"
- docker build some-docker-registry/some-image2 .
- docker push some-docker-registry/some-image2
deployment: productionWhen it is run by bitbucket, it indicates that the authentication to the docker registry fails so now this step has to be added over and over again:
options:
docker: true
pipelines:
default:
- step:
script:
- echo "Login to the docker registry"
- docker login some-docker-registry
- echo "Build some-image"
- docker build some-docker-registry/some-image .
- docker push some-docker-registry/some-image
- step:
script:
- echo "Login to the docker registry"
- docker login some-docker-registry
- echo "Build some-image2"
- docker build some-docker-registry/some-image2 .
- docker push some-docker-registry/some-image2
deployment: productionIs it possible to prevent such duplication? An attempt has been made (see first code snippet), but that does not seem to work, i.e. a docker login is required by every step.
Solution
Unlike the declarative pipelines in Jenkins, bitbucket does not seem to have such functionality. In order to prevent code duplication a script was created and the current bitbucket-pipelines looks as follows:
options:
docker: true
pipelines:
default:
- step:
script:
- ./build-script.sh API true true
- step:
script:
- ./build-script.sh Write true true
deployment: productionCode Snippets
options:
docker: true
pipelines:
default:
- step:
script:
- ./build-script.sh API true true
- step:
script:
- ./build-script.sh Write true true
deployment: productionContext
StackExchange DevOps Q#3526, answer score: 1
Revisions (0)
No revisions yet.