name: Golang CI

on:
  push:
    branches: [main]
  pull_request:
    types: [opened, reopened, synchronize]
  schedule:
  - cron: '15 3 * * 0'

jobs:
  build:
    name: CI Build
    runs-on: ubuntu-latest
    steps:
    - name: Setup Go
      uses: actions/setup-go@v2
      with:
        go-version: '1.17'
    - name: Setup tools
      run: |
        go get golang.org/x/lint/golint
    - name: Checkout code
      uses: actions/checkout@v2
    - name: Test Project
      run: |
        make test
    - name: Build Project
      run: |
        make

  docker_build:
    runs-on: ubuntu-20.04
    name: Docker Build
    if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == 'rebuy-de/aws-nuke' }}

    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0

    - name: Generate image tags
      shell: bash
      run: |
        if [ "${GITHUB_EVENT_NAME}" == "pull_request" ]; then
          echo "##[set-output name=tags;]quay.io/rebuy/aws-nuke:${GITHUB_HEAD_REF},docker.io/rebuy/aws-nuke:${GITHUB_HEAD_REF}"
        else
          echo "##[set-output name=tags;]quay.io/rebuy/aws-nuke:main,docker.io/rebuy/aws-nuke:main,\
            quay.io/rebuy/aws-nuke:latest,docker.io/rebuy/aws-nuke:latest"
        fi
      id: generate_tags

    - name: Set up QEMU
      id: qemu
      uses: docker/setup-qemu-action@v1
      with:
        platforms: arm64

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1
      with:
        install: true

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

    - name: Login to Quay.io
      uses: docker/login-action@v1
      with:
        registry: quay.io
        username: ${{ secrets.QUAY_USERNAME }}
        password: ${{ secrets.QUAY_PASSWORD }}

    - name: Build and push
      uses: docker/build-push-action@v2
      with:
        context: .
        push: true
        tags: ${{ steps.generate_tags.outputs.tags }}
        platforms: linux/amd64,linux/arm64
