name: release
on:
  push:
    branches:
      - main
jobs:
  build:
    if: |
      github.repository_owner == 'maidsafe' &&
      startsWith(github.event.head_commit.message, 'chore(release):')
    name: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: cargo-bins/cargo-binstall@main
      - shell: bash
        run: cargo binstall --no-confirm just
      - uses: actions-rs/toolchain@v1
        id: toolchain
        with:
          profile: minimal
          toolchain: stable
          override: true
      # This method was copied from another repository, where we support multiple architectures. We
      # will just leave the mechanism in place in this repository, even though we're only using one
      # architecture. We can extend it later if need be.
      - shell: bash
        run: just build-release-artifacts "x86_64-unknown-linux-musl"
      - uses: actions/upload-artifact@main
        with:
          name: testnet-deploy-x86_64-unknown-linux-musl
          path: |
            artifacts
            !artifacts/.cargo-lock
  release:
    if: |
      github.repository_owner == 'maidsafe' &&
      startsWith(github.event.head_commit.message, 'chore(release):')
    name: publish and release
    runs-on: ubuntu-latest
    needs: [build]
    env:
      AWS_ACCESS_KEY_ID: ${{ secrets.S3_DEPLOY_AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_DEPLOY_AWS_SECRET_ACCESS_KEY }}
      AWS_DEFAULT_REGION: eu-west-2
      GH_TOKEN: ${{ secrets.RELEASE_PAT }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: "0"
          token: ${{ secrets.RELEASE_PAT }}
      - uses: actions/download-artifact@master
        with:
          name: testnet-deploy-x86_64-unknown-linux-musl
          path: artifacts/x86_64-unknown-linux-musl/release
      - name: configure git for release
        shell: bash
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
      - uses: cargo-bins/cargo-binstall@main
      - name: install tools
        shell: bash
        run: |
          cargo binstall --no-confirm just
          cargo binstall --no-confirm release-plz
      - name: publish and release
        shell: bash
        run: |
          cargo login ${{ secrets.CRATES_IO_TOKEN }}
          release-plz release --git-token ${{ secrets.RELEASE_PAT }}
          just package-release-assets
          just upload-release-assets-to-s3
