name: Build
on:
  push:
    tags: '*'

jobs:
  prepare-data:
    name: Prepare Data
    runs-on: ubuntu-latest
    outputs:
      tag: ${{ steps.prepare.outputs.SOURCE_TAG }}
    steps:
      - id: prepare
        name: Prepare environment variables
        run: |
          echo ::set-output name=SOURCE_NAME::${GITHUB_REF#refs/*/}
          echo ::set-output name=SOURCE_BRANCH::${GITHUB_REF#refs/heads/}
          echo ::set-output name=SOURCE_TAG::${GITHUB_REF#refs/tags/}

  build:
    name: GitHub Container Registry
    runs-on: ubuntu-latest
    needs: [prepare-data]
    steps:
    - uses: actions/checkout@v3
    - name: Login
      run: echo ${{ secrets.PAT }} | docker login ghcr.io -u luketainton --password-stdin
    - name: Build
      run: docker build . --file Dockerfile --tag ghcr.io/luketainton/hesk:${{ needs.prepare-data.outputs.tag }} --tag ghcr.io/luketainton/hesk:latest
    - name: Push
      run: |
        docker push ghcr.io/luketainton/hesk:latest
        docker push ghcr.io/luketainton/hesk:${{ needs.prepare-data.outputs.tag }}
