name: MXE stage 2 Docker Image CI

# trigger this second stage via a repository dispaych event
# the version that we are building is passed in via the event type that is available to us here
# as github.event.action

on: repository_dispatch

jobs:
  mxe-build-container-stage2:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
      with:
        ref: ${{ github.event.client_payload.target_branch }}

      # Grab the version from the event name that we were triggered by and add ".stage1" to find the docker image to start FROM
      # And create the NAME of the final docker image (including the branch if not master)
    - name: set env
      run: |
        v=${{ github.event.action }}
        s=".stage1"
        b=${{ github.event.client_payload.target_branch }} # -BRANCH suffix, unless the branch is master
        b=${b/refs\/heads\//}
        b=${b,,} # the name needs to be all lower case
        if [ "$b" = "master" ] || [ "$b" = "" ] ; then b="" ; else b="-$b" ; fi
        echo "::set-env name=VERSION::${v}${s}"
        echo "::set-env name=NAME::subsurface/mxe-build-container${b}:${v}"

    - name: Build and Publish stage 2 Docker image to Dockerhub
      uses: elgohr/Publish-Docker-Github-Action@master
      with:
        name:       ${{ env.NAME }}
        username:   ${{ secrets.DOCKER_USERNAME }}
        password:   ${{ secrets.DOCKER_PASSWORD }}
        dockerfile: 'Dockerfile-stage2'
        workdir:    './scripts/docker/mxe-build-container/'
        buildargs:  VERSION

