name: Go
on:
  pull_request:
  push:
    branches:
      - master
jobs:
  build:
    strategy:
      matrix:
        go: ["1.18", "1.19"]
    env:
      DEFAULT_GO: "1.19"
    name: Build with go version ${{ matrix.go }}
    runs-on: ubuntu-latest
    steps:
      - name: Set up GoLang ${{ matrix.go }}
        uses: actions/setup-go@v1
        with:
          go-version: ${{ matrix.go }}
        id: go

      - name: Check out code into the Go module directory
        uses: actions/checkout@v1

      - name: golangci-lint
        uses: golangci/golangci-lint-action@v3
        with:
          version: latest

      - name: Run tests
        run: go test ./...

      - name: Build binary
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          go build -o build/go-semantic-release-temp ./cmd/go-semantic-release/
          ./build/go-semantic-release-temp next --no-cache --loglevel trace
          GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o build/go-semantic-release.linux_x86_64  -ldflags "-w -s --X main.version=`./build/go-semantic-release-temp next`" ./cmd/go-semantic-release/
          GOOS=windows GOARCH=386 CGO_ENABLED=0 go build -o build/go-semantic-release.windows_i386.exe  -ldflags "-w -s -X main.version=`./build/go-semantic-release-temp next`" ./cmd/go-semantic-release/
          GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o build/go-semantic-release.windows_x86_64.exe  -ldflags "-w -s -X main.version=`./build/go-semantic-release-temp next`" ./cmd/go-semantic-release/
          GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o build/go-semantic-release.darwin_x86_64  -ldflags "-w -s -X main.version=`./build/go-semantic-release-temp next`" ./cmd/go-semantic-release/
      - name: Build Docker image PR
        if: github.ref != 'refs/heads/master'
        run: |
          docker build -t nightapes/go-semantic-release:development-${{matrix.go}} .
      - name: Build Docker image master
        if: github.ref == 'refs/heads/master'
        run: |
          docker login -u nightapes -p ${{ secrets.DOCKER_PASSWORD }}
          docker login -u nightapes -p ${{ secrets.GITHUB_TOKEN }} docker.pkg.github.com
          docker build -t nightapes/go-semantic-release:development-${{matrix.go}} .
          docker push nightapes/go-semantic-release:development-${{matrix.go}}
          docker tag nightapes/go-semantic-release:development-${{matrix.go}} docker.pkg.github.com/nightapes/go-semantic-release/go-semantic-release:development-${{matrix.go}}
          docker push docker.pkg.github.com/nightapes/go-semantic-release/go-semantic-release:development-${{matrix.go}}
      - uses: actions/upload-artifact@v1
        if: matrix.go == env.DEFAULT_GO
        with:
          name: build
          path: build
  release:
    name: Release
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v1
      - uses: actions/download-artifact@v1
        with:
          name: build
          path: build
      - name: Release
        if: github.ref == 'refs/heads/master'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          chmod -R +x build
          docker login -u nightapes -p ${{ secrets.DOCKER_PASSWORD }}
          docker login -u nightapes -p $GITHUB_TOKEN docker.pkg.github.com
          ./build/go-semantic-release-temp release --loglevel trace
      - name: Release PR
        if:  github.ref != 'refs/heads/master'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          chmod -R +x build
          ./build/go-semantic-release-temp release --loglevel trace
