name: Publish consumable Docker images

on:
  push:
    tags: ['v?[0-9]+.[0-9]+.[0-9]+*']

jobs:
  compile:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        arch:
        - x86_64-unknown-linux-musl
        - x86_64-unknown-linux-gnu
    steps:
    - uses: actions/checkout@v2
    - name: Install rust-embedded/cross
      env: { VERSION: v0.1.16 }
      run: >-
        wget -nv https://github.com/rust-embedded/cross/releases/download/${VERSION}/cross-${VERSION}-x86_64-unknown-linux-gnu.tar.gz
        -O- | sudo tar xz -C /usr/local/bin/
    - name: compile for specific target
      env: { arch: '${{ matrix.arch }}' }
      run: |
        cross build --target ${{ matrix.arch }} --release
        # leave only the executable file
        rm -frd target/${{ matrix.arch }}/release/{*/*,*.d,*.rlib,.fingerprint}
        find . -empty -delete
    - uses: actions/upload-artifact@master
      with:
        name: ${{ matrix.arch }}
        path: target/${{ matrix.arch }}/release

  docker:
    name: Build and publish docker images
    needs: compile
    runs-on: ubuntu-latest
    env:
      DOCKER_REGISTRY: quay.io/nushell
      DOCKER_PASSWORD: ${{ secrets.DOCKER_REGISTRY }}
      DOCKER_USER: ${{ secrets.DOCKER_USER }}
    strategy:
      matrix:
        tag:
        - alpine
        - slim
        - debian
        - glibc-busybox
        - musl-busybox
        - musl-distroless
        - glibc-distroless
        - glibc
        - musl
        include:
        - { tag: alpine,           base-image: alpine,                     arch: x86_64-unknown-linux-musl, plugin: true,  use-patch: false}
        - { tag: slim,             base-image: 'debian:stable-slim',       arch: x86_64-unknown-linux-gnu,  plugin: true,  use-patch: false}
        - { tag: debian,           base-image: debian,                     arch: x86_64-unknown-linux-gnu,  plugin: true,  use-patch: false}
        - { tag: glibc-busybox,    base-image: 'busybox:glibc',            arch: x86_64-unknown-linux-gnu,  plugin: false, use-patch: true }
        - { tag: musl-busybox,     base-image: 'busybox:musl',             arch: x86_64-unknown-linux-musl, plugin: false, use-patch: false}
        - { tag: musl-distroless,  base-image: 'gcr.io/distroless/static', arch: x86_64-unknown-linux-musl, plugin: false, use-patch: false}
        - { tag: glibc-distroless, base-image: 'gcr.io/distroless/cc',     arch: x86_64-unknown-linux-gnu,  plugin: false, use-patch: true }
        - { tag: glibc,            base-image: scratch,                    arch: x86_64-unknown-linux-gnu,  plugin: false, use-patch: false}
        - { tag: musl,             base-image: scratch,                    arch: x86_64-unknown-linux-musl, plugin: false, use-patch: false}
    steps:
    - uses: actions/checkout@v2
    - uses: actions/download-artifact@master
      with: { name: '${{ matrix.arch }}', path: target/release }
    - name: Build and publish exact version
      run: |-
        export DOCKER_TAG=${GITHUB_REF##*/}-${{ matrix.tag }}
        export NU_BINS=target/release/$( [ ${{ matrix.plugin }} = true ] && echo nu* || echo nu )
        export PATCH=$([ ${{ matrix.use-patch }} = true ] && echo .${{ matrix.tag }} || echo '')
        chmod +x $NU_BINS

        echo ${DOCKER_PASSWORD} | docker login ${DOCKER_REGISTRY} -u ${DOCKER_USER} --password-stdin
        docker-compose --file docker/docker-compose.package.yml build
        docker-compose --file docker/docker-compose.package.yml push              # exact version
      env:
        BASE_IMAGE: ${{ matrix.base-image }}

    #region semantics tagging
    - name: Retag and push with suffixed version
      run: |-
        VERSION=${GITHUB_REF##*/}

        latest_version=${VERSION%%%.*}-${{ matrix.tag }}
        latest_feature=${VERSION%%.*}-${{ matrix.tag }}
        latest_patch=${VERSION%.*}-${{ matrix.tag }}
        exact_version=${VERSION}-${{ matrix.tag }}

        tags=( ${latest_version} ${latest_feature} ${latest_patch} ${exact_version} )

        for tag in ${tags[@]}; do
          docker tag ${DOCKER_REGISTRY}/nu:${VERSION}-${{ matrix.tag }} ${DOCKER_REGISTRY}/nu:${tag}
          docker push ${DOCKER_REGISTRY}/nu:${tag}
        done

        # latest version
        docker tag ${DOCKER_REGISTRY}/nu:${VERSION}-${{ matrix.tag }} ${DOCKER_REGISTRY}/nu:${{ matrix.tag }}
        docker push ${DOCKER_REGISTRY}/nu:${{ matrix.tag }}

    - name: Retag and push debian as latest
      if: matrix.tag == 'debian'
      run: |-
        VERSION=${GITHUB_REF##*/}

        # ${latest features} ${latest patch} ${exact version}
        tags=( ${VERSION%%.*} ${VERSION%.*} ${VERSION} )

        for tag in ${tags[@]}; do
          docker tag ${DOCKER_REGISTRY}/nu:${VERSION}-${{ matrix.tag }} ${DOCKER_REGISTRY}/nu:${tag}
          docker push ${DOCKER_REGISTRY}/nu:${tag}
        done

        # latest version
        docker tag ${DOCKER_REGISTRY}/nu:${{ matrix.tag }} ${DOCKER_REGISTRY}/nu:latest
        docker push ${DOCKER_REGISTRY}/nu:latest
    #endregion semantics tagging
