name: release-nightly

on:
  push:
    branches: [ main ]

env:
  GOPATH: /go_path
  GOCACHE: /go_cache

jobs:
  goreleaser:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0 # all history for all branches and tags
      - uses: actions/setup-go@v3
        with:
          go-version: '>=1.21.0'
      - uses: https://gitea.com/actions/go-hashfiles@v0.0.1
        id: hash-go
        with:
          patterns: |
            go.mod
            go.sum
      - name: cache go
        id: cache-go
        uses: https://github.com/actions/cache@v3
        with:
          path: |
            /go_path
            /go_cache
          key: go_path-${{ steps.hash-go.outputs.hash }}
      - name: goreleaser
        uses: https://github.com/goreleaser/goreleaser-action@v4
        with:
          distribution: goreleaser-pro
          version: latest
          args: release --nightly
        env:
          GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
          AWS_REGION: ${{ secrets.AWS_REGION }}
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          S3_REGION: ${{ secrets.AWS_REGION }}
          S3_BUCKET: ${{ secrets.AWS_BUCKET }}
  release-image:
    runs-on: ubuntu-latest
    container:
      image: catthehacker/ubuntu:act-latest
    env:
      DOCKER_ORG: gitea
      DOCKER_LATEST: nightly
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0 # all history for all branches and tags

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker BuildX
        uses: docker/setup-buildx-action@v2

      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Get Meta
        id: meta
        run: |
          echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT
          echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT

      - name: Build and push
        uses: docker/build-push-action@v4
        env:
          ACTIONS_RUNTIME_TOKEN: '' # See https://gitea.com/gitea/act_runner/issues/119
        with:
          context: .
          file: ./Dockerfile
          platforms: |
            linux/amd64
            linux/arm64
          push: true
          tags: |
            ${{ env.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ env.DOCKER_LATEST }}

      - name: Build and push dind-rootless
        uses: docker/build-push-action@v4
        env:
          ACTIONS_RUNTIME_TOKEN: '' # See https://gitea.com/gitea/act_runner/issues/119
        with:
          context: .
          file: ./Dockerfile.rootless
          platforms: |
            linux/amd64
            linux/arm64
          push: true
          tags: |
            ${{ env.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ env.DOCKER_LATEST }}-dind-rootless