name: Create Release & Upload Assets

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
    - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
  test:
    name: Lint, Test, Build
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        go: [ '1.17', '1.18' ]
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ matrix.go }}
      -
        name: golangci-lint
        uses: golangci/golangci-lint-action@v2
        with:
          version: 'v1.45.0'
          args: --timeout=30m
      -
        name: Test, Build
        id: lintTestBuild
        run: V=1 make ci

  create_release:
    name: Create Release
    needs: test
    runs-on: ubuntu-20.04
    outputs:
      version: ${{ steps.extract-tag.outputs.VERSION }}
      vversion: ${{ steps.extract-tag.outputs.VVERSION }}
      is_prerelease: ${{ steps.is_prerelease.outputs.IS_PRERELEASE }}
    steps:
      -
        name: Extract Tag Names
        id: extract-tag
        run: |
          VVERSION=${GITHUB_REF#refs/tags/}
          VERSION=${GITHUB_REF#refs/tags/v}
          echo "::set-output name=VVERSION::${VVERSION}"
          echo "::set-output name=VERSION::${VERSION}"
      -
        name: Is Pre-release
        id: is_prerelease
        run: |
          set +e
          echo ${{ github.ref }} | grep "\-rc.*"
          OUT=$?
          if [ $OUT -eq 0 ]; then IS_PRERELEASE=true; else IS_PRERELEASE=false; fi
          echo "::set-output name=IS_PRERELEASE::${IS_PRERELEASE}"
      -
        name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: false
          prerelease: ${{ steps.is_prerelease.outputs.IS_PRERELEASE }}

  goreleaser:
    name: Upload Assets to Github w/ goreleaser
    runs-on: ubuntu-20.04
    needs: create_release
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      -
        name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.18
      -
        name: Configure Go
        id: configure_go
        run: |
          PATH=$PATH:/usr/local/go/bin:/home/admin/go/bin
      -
        name: Install cosign
        uses: sigstore/cosign-installer@v1.1.0
        with:
          cosign-release: 'v1.1.0'
      -
        name: Write cosign key to disk
        id: write_key
        run: echo "${{ secrets.COSIGN_KEY }}" > "/tmp/cosign.key"
      -
        name: Get Release Date
        id: release_date
        run: |
          RELEASE_DATE=$(date +"%y-%m-%d")
          echo "::set-output name=RELEASE_DATE::${RELEASE_DATE}"
      -
        name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          version: 'v1.7.0'
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}
          COSIGN_PWD: ${{ secrets.COSIGN_PWD }}
          RELEASE_DATE: ${{ steps.release_date.outputs.RELEASE_DATE }}

  build_upload_docker:
    name: Build & Upload Docker Images
    runs-on: ubuntu-20.04
    needs: test
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.18
      -
        name: Install cosign
        uses: sigstore/cosign-installer@v1.1.0
        with:
          cosign-release: 'v1.1.0'
      -
        name: Write cosign key to disk
        id: write_key
        run: echo "${{ secrets.COSIGN_KEY }}" > "/tmp/cosign.key"
      -
        name: Build
        id: build
        run: |
          PATH=$PATH:/usr/local/go/bin:/home/admin/go/bin
          make docker-artifacts
        env:
          DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
          COSIGN_PWD: ${{ secrets.COSIGN_PWD }}

# All jobs below this are for full releases (non release candidates e.g. *-rc.*)

  build_upload_aws_s3_binaries:
    name: Build & Upload AWS S3 Binaries
    runs-on: ubuntu-20.04
    needs: create_release
    if: needs.create_release.outputs.is_prerelease == 'false'
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.18
      -
        name: Build
        id: build
        run: |
          PATH=$PATH:/usr/local/go/bin:/home/admin/go/bin
          make -j1 binary-linux-amd64 binary-linux-arm64 binary-darwin-amd64 binary-windows-amd64
          mkdir -p ./.releases
          cp ./output/binary/linux-amd64/bin/step ./.releases/step_${{ needs.create_release.outputs.version }}_linux_amd64
          cp ./output/binary/linux-amd64/bin/step ./.releases/step_latest_linux_amd64
          cp ./output/binary/linux-arm64/bin/step ./.releases/step_${{ needs.create_release.outputs.version }}_linux_arm64
          cp ./output/binary/linux-arm64/bin/step ./.releases/step_latest_linux_arm64
          cp ./output/binary/darwin-amd64/bin/step ./.releases/step_${{ needs.create_release.outputs.version }}_darwin_amd64
          cp ./output/binary/darwin-amd64/bin/step ./.releases/step_latest_darwin_amd64
          cp ./output/binary/windows-amd64/bin/step ./.releases/step_${{ needs.create_release.outputs.version }}_windows.exe
          cp ./output/binary/windows-amd64/bin/step ./.releases/step_latest_windows.exe
      -
        name: Upload s3
        id: upload-s3
        uses: jakejarvis/s3-sync-action@v0.5.1
        with:
          args: --acl public-read --follow-symlinks
        env:
          AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_REGION: us-east-1
          SOURCE_DIR: ./.releases

  update_reference_docs:
    name: Update Reference Docs
    runs-on: ubuntu-20.04
    needs: create_release
    if: needs.create_release.outputs.is_prerelease == 'false'
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Setup Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.18
      -
        name: Build
        id: build
        run: V=1 make build
      -
        name: Checkout Docs
        uses: actions/checkout@master
        with:
          repository: smallstep/docs
          token: ${{ secrets.PAT }}
          path: './docs'
      -
        name: Update Reference
        id: update_refrence
        run: |
          ./bin/step help --markdown ./docs/src/pages/docs/step-cli/reference
          cd ./docs
          git config user.email "eng@smallstep.com"
          git config user.name "Github Action CI"
          git add . && git commit -a -m "step-cli ${{ needs.create_release.outputs.vversion }} reference update"
      -
        name: Push changes
        uses: ad-m/github-push-action@v0.6.0
        with:
          github_token: ${{ secrets.PAT }}
          branch: 'main'
          directory: './docs'
          repository: 'smallstep/docs'
