Answers: <p>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:</p>
  
  <pre><code>options:
    docker: true
  
  pipelines:
    default:
      - step:
          script:
            - ./build-script.sh API true true
      - step:
          script:
            - ./build-script.sh Write true true
          deployment: production
  </code></pre>
  
Answers: CC BY-SA 3.0
Answers: 2018-03-03T13:07:08.107Z
Answers: 2018-03-03T13:07:08.107Z
Answers: 030
Body: <p>The following bitbucket-pipelines file has been created:</p>
  
  <pre><code>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: production
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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: production
  </code></pre>
  
  <p>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.</p>
  
Tags: <bitbucket-pipelines>
Title: How to avoid code duplication in bitbucket-pipelines?
UserDisplayName: 030
ThreadText: # How to avoid code duplication in bitbucket-pipelines?
  
  (Asked by: username_0 on 2018-03-03)
  
  The following bitbucket-pipelines file has been created:
  
  ```
  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: production
  
  ```
  
  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:
  
  ```
  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: production
  
  ```
  
  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.
  
  **Tags:** bitbucket-pipelines
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-03-03. Score: 1 | Accepted answer
  
  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: production
  
  ```
  
  
  ---
  
  