How to avoid code duplication in bitbucket-pipelines? 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.
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