name: publish

on:
  push:
    tags:
      - '*'

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - 
        uses: actions/checkout@master
        with:
          fetch-depth: 1
      - 
        name: Get tags
        id: get_tag
        run: echo ::set-output name=TAG::${GITHUB_REF#refs/tags/}
      -
        name: Set Username/Repo and ImagePrefix as ENV vars
        run: |
          echo "USER_REPO"=$(echo "$GITHUB_REPOSITORY" | awk '{print tolower($1)}' | sed -e "s/:refs//") >> $GITHUB_ENV && \
          echo "IMAGE_PREFIX"=$(echo "ghcr.io/$GITHUB_REPOSITORY" | awk '{print tolower($1)}' | sed -e "s/:refs//") >> $GITHUB_ENV
      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      - 
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - 
        name: Login to Github Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      -
        name: Build and Push container images (non-root)
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          platforms: linux/amd64,linux/arm/v7,linux/arm64
          target: release
          build-args: |
            VERSION=${{ steps.get_tag.outputs.TAG }}
            GIT_COMMIT=${{ github.sha }}
            REPO_URL=https://github.com/${{ env.USER_REPO }}
          push: true
          tags: |
            ${{ env.IMAGE_PREFIX }}:${{ github.sha }}
            ${{ env.IMAGE_PREFIX }}:${{ steps.get_tag.outputs.TAG }}
            ${{ env.IMAGE_PREFIX }}:latest
      - 
        name: Build and Push container images (root)
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          platforms: linux/amd64,linux/arm/v7,linux/arm64
          target: root
          build-args: |
            VERSION=${{ steps.get_tag.outputs.TAG }}
            GIT_COMMIT=${{ github.sha }}
            REPO_URL=https://github.com/${{ env.USER_REPO }}
          push: true
          tags: |
            ${{ env.IMAGE_PREFIX }}:${{ github.sha }}-root
            ${{ env.IMAGE_PREFIX }}:${{ steps.get_tag.outputs.TAG }}-root
            ${{ env.IMAGE_PREFIX }}:latest-root
      -
        name: Build binaries for multiple environments
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile.redist
          platforms: linux/amd64
          build-args: |
            VERSION=${{ steps.get_tag.outputs.TAG }}
            GIT_COMMIT=${{ github.sha }}
          load: true
          push: false
          tags: |
           openfaas/faas-cli:${{ steps.get_tag.outputs.TAG }}
      - 
        name: Copy binaries to host
        run: ./extract_binaries.sh ${{ steps.get_tag.outputs.TAG }}
      - 
        name: Create SHA of binaries
        run: cd bin && ../ci/hashgen.sh && cd ../
      - 
        name: Upload binaries and their SHA to Github Release
        uses: alexellis/upload-assets@0.2.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          asset_paths: '["./bin/faas-cli*"]'
