name: Release
on:
  push:
    tags: '*'

jobs:
  build-windows:
    name: Build Windows Assets
    runs-on: windows-latest
    strategy:
      matrix:
        go: ['1.17.x']
    steps:
    - uses: actions/checkout@v1
    - uses: actions/setup-ruby@v1
    - uses: actions/setup-go@v2
      with:
        go-version: ${{ matrix.go }}
    - run: mkdir -p "$HOME/go/bin"
      shell: bash
    - run: set GOPATH=%HOME%\go
    - run: cinst InnoSetup -y
    - run: cinst strawberryperl -y
    - run: cinst zip -y
    - run: cinst jq -y
    - run: gem install ronn
    - run: refreshenv
    - run: GOPATH="$HOME/go" PATH="$HOME/go/bin:$PATH" go get github.com/josephspurrier/goversioninfo/cmd/goversioninfo
      shell: bash
    - run: mkdir -p bin/releases
      shell: bash
    - run: CERT_FILE="$HOME/cert.pfx" make release-write-certificate
      shell: bash
      env:
        CERT_CONTENTS: ${{secrets.WINDOWS_CERT_BASE64}}
    - run: PATH="$HOME/go/bin:$PATH" GOARCH=amd64 go generate && make bin/releases/git-lfs-windows-amd64-$(git describe).zip
      shell: bash
    - run: PATH="$HOME/go/bin:$PATH" GOARCH=386 go generate && make bin/releases/git-lfs-windows-386-$(git describe).zip
      shell: bash
    - run: PATH="$HOME/go/bin:$PATH" GOARCH=arm64 go generate && make bin/releases/git-lfs-windows-arm64-$(git describe).zip
      shell: bash
    - run: PATH="$HOME/go/bin:/c/Program Files (x86)/Windows Kits/10/bin/x86:$PATH" CERT_FILE="$HOME/cert.pfx" make release-windows
      shell: bash
      env:
        CERT_PASS: ${{secrets.WINDOWS_CERT_PASS}}
    - run: make release-windows-rebuild
      shell: bash
    - uses: actions/upload-artifact@v1
      with:
        name: windows-assets
        path: bin/releases
  build-macos:
    name: Build macOS Assets
    runs-on: macos-latest
    strategy:
      matrix:
        go: ['1.17.x']
    steps:
    - uses: actions/checkout@v1
    - uses: actions/setup-ruby@v1
    - uses: actions/setup-go@v2
      with:
        go-version: ${{ matrix.go }}
    - run: brew install mitchellh/gon/gon
    - run: gem install ronn
    - run: make release
    - run: CERT_FILE="$HOME/cert.p12" make release-write-certificate
      env:
        CERT_CONTENTS: ${{secrets.MACOS_CERT_BASE64}}
    - run: CERT_FILE="$HOME/cert.p12" make release-import-certificate
      env:
        CERT_PASS: ${{secrets.MACOS_CERT_PASS}}
    - run: make release-darwin
      env:
        DARWIN_DEV_USER: ${{secrets.MACOS_DEV_USER}}
        DARWIN_DEV_PASS: ${{secrets.MACOS_DEV_PASS}}
        DARWIN_CERT_ID: ${{secrets.MACOS_CERT_ID}}
    - uses: actions/upload-artifact@v1
      with:
        name: macos-assets
        path: bin/releases
  build-main:
    name: Main Release Assets
    needs:
      - build-windows
      - build-macos
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        go: ['1.17.x']
    steps:
    - uses: actions/checkout@v1
    - uses: actions/setup-ruby@v1
    - uses: actions/setup-go@v2
      with:
        go-version: ${{ matrix.go }}
    - uses: actions/download-artifact@v1
      with:
        name: windows-assets
    - uses: actions/download-artifact@v1
      with:
        name: macos-assets
    - run: gem install ronn
    - run: CGO_ENABLED=0 make release
    - run: rm -f bin/releases/*windows* bin/releases/*darwin*
    - run: 'find windows-assets -name "*windows*" -type f | xargs -L1 -I{} mv {} bin/releases'
    - run: 'find macos-assets -name "*darwin*" -type f | xargs -L1 -I{} mv {} bin/releases'
    - run: script/upload --skip-verify $(git describe)
      env:
        GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
  build-docker:
    name: Build Linux Packages
    runs-on: ubuntu-20.04
    steps:
    - uses: actions/checkout@v1
    - uses: actions/setup-ruby@v1
    - run: gem install packagecloud-ruby
    - run: git clone https://github.com/git-lfs/build-dockers.git "$HOME/build-dockers"
    - run: (cd "$HOME/build-dockers" && ./build_dockers.bsh)
    - run: DOCKER_AUTOPULL=0 ./docker/run_dockers.bsh --prune
    # If this is a pre-release tag, don't upload anything to packagecloud.
    - run: '[ -z "${GITHUB_REF%%refs/tags/*-pre*}" ] || ./script/packagecloud.rb'
      env:
        PACKAGECLOUD_TOKEN: ${{secrets.PACKAGECLOUD_TOKEN}}
