name: Release

on:
  push:
    tags:
    - 'v*'

permissions:
  contents: write
  packages: write

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
    - name: Setup Go
      uses: actions/setup-go@v4
      with:
        go-version: 1.x
    - name: Cross build
      run: make cross
    - name: Create Release
      uses: ncipollo/release-action@v1
      with:
        name: Release ${{ github.ref_name }}
        artifacts: 'goxz/*'

  docker:
    name: Docker
    runs-on: ubuntu-latest
    needs: release
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
    - name: Docker metadata
      uses: docker/metadata-action@v4
      id: metadata
      with:
        images: |
          ${{ github.repository }}
          ghcr.io/${{ github.repository }}
        tags: |
          type=semver,pattern={{version}}
          type=semver,pattern={{major}}.{{minor}}
          type=semver,pattern={{major}}
    - name: Set up QEMU
      uses: docker/setup-qemu-action@v2
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v2
    - name: Login to Docker Hub
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
    - name: Login to GitHub Container Registry
      uses: docker/login-action@v2
      with:
        registry: ghcr.io
        username: ${{ github.repository_owner }}
        password: ${{ secrets.GITHUB_TOKEN }}
    - name: Build and release Docker image
      uses: docker/build-push-action@v4
      with:
        context: .
        push: true
        platforms: linux/amd64, linux/arm64
        tags: ${{ steps.metadata.outputs.tags }}
        labels: ${{ steps.metadata.outputs.labels }}
