name: Build and Deploy

on:
  push:
    branches:
      - main
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Build application
        run: |
          docker build . -f Dockerfile -t "adriano_fyi:${GITHUB_SHA}"
          mkdir out
          docker save "adriano_fyi:${GITHUB_SHA}" -o "out/adriano_fyi-${GITHUB_SHA}.tar"
      - name: S3 Sync
        uses: jakejarvis/s3-sync-action@v0.5.1
        env:
          AWS_ACCESS_KEY_ID: "D1NFLA7HVIDF1MOHI2RT"
          AWS_SECRET_ACCESS_KEY: ${{ secrets.LINODE_SECRET_ACCESS_KEY }}
          AWS_S3_BUCKET: "adriano-fyi-static-site"
          AWS_REGION: "us-east-1"
          AWS_S3_ENDPOINT: "https://us-east-1.linodeobjects.com"
          SOURCE_DIR: "out"
  deploy:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Tailscale Login
        uses: tailscale/github-action@main
        with:
          authkey: ${{ secrets.TAILSCALE_AUTHKEY }}
          version: 1.32.1
        env:
          ADDITIONAL_ARGS: "--accept-dns"
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Deploy nomad job
        run: |
          curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
          sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
          sudo apt-get update && sudo apt-get install nomad
          tailscale status
          nomad job run -var version=${GITHUB_SHA} job.nomad.hcl
        env:
          NOMAD_ADDR: http://cluster-0:4646
          NOMAD_TOKEN: ${{ secrets.NOMAD_TOKEN }}
