Publish Docker image to different repos based on branch?


#1

I’m trying to publish Docker images built from the master branch to a production repository and all other dev branches to a dev repository. Right now I’m basically duplicating the Drone configs like this:

  - name: publish_prod
    image: plugins/gcr
    pull: always
    volumes:
      - name: docker
        path: /var/run/docker.sock
    privileged: true
    settings:
      repo: gcr.io/blah/blah_prod
      target: prod
      tags:
        - ${DRONE_COMMIT_SHA:0:7}
    when:
      branch: 
      - master
      event: 
      - push
  
  - name: publish_dev
    image: plugins/gcr
    pull: always
    volumes:
      - name: docker
        path: /var/run/docker.sock
    privileged: true
    settings:
      repo: gcr.io/blah/blah_dev
      target: prod
      tags:
        - ${DRONE_COMMIT_SHA:0:7}
    when:
      branch:
        exclude:
        - master
      event:
      - push

This certainly works but I’m hoping there is a better way to do this instead of duplicating the two parts. Is there a way to provide a repo variable conditioned on the branch name or something like that?


#2

The approach you are using is the recommended approach. If you want to reduce duplication in your yaml you can use anchors [1] or you can use jsonnet [2]

[1] https://medium.com/@kinghuang/docker-compose-anchors-aliases-extensions-a1e4105d70bd
[2] How to reduce Yaml boilerplate