region-proxy 1.3.0

A CLI tool to create a SOCKS proxy through AWS EC2 in any region
name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: macos-latest
    strategy:
      matrix:
        target: [x86_64-apple-darwin, aarch64-apple-darwin]
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@4be9e76fd7c4901c61fb841f559994984270fce7 # stable
        with:
          targets: ${{ matrix.target }}

      - name: Cache cargo
        uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: release-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            release-${{ matrix.target }}-

      - name: Build release
        env:
          TARGET: ${{ matrix.target }}
        run: cargo build --release --target "$TARGET"

      - name: Upload binary
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: binary-${{ matrix.target }}
          path: target/${{ matrix.target }}/release/region-proxy
          retention-days: 1

  package:
    needs: build
    runs-on: macos-latest
    outputs:
      sha256: ${{ steps.sha256.outputs.sha256 }}
    steps:
      - name: Download binaries
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          path: binaries

      - name: Create universal binary
        run: |
          lipo -create \
            binaries/binary-x86_64-apple-darwin/region-proxy \
            binaries/binary-aarch64-apple-darwin/region-proxy \
            -output region-proxy
          chmod +x region-proxy
          lipo -info region-proxy

      - name: Create tarball
        env:
          TAG: ${{ github.ref_name }}
        run: |
          tar -czvf "region-proxy-${TAG}-macos.tar.gz" region-proxy

      - name: Calculate SHA256
        id: sha256
        env:
          TAG: ${{ github.ref_name }}
        run: |
          SHA256=$(shasum -a 256 "region-proxy-${TAG}-macos.tar.gz" | cut -d ' ' -f 1)
          echo "sha256=$SHA256" >> "$GITHUB_OUTPUT"
          echo "SHA256: $SHA256"

      - name: Upload artifacts
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: macos-release
          path: |
            region-proxy-${{ github.ref_name }}-macos.tar.gz

  release:
    needs: package
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Download macOS artifacts
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: macos-release

      - name: Create Release
        uses: softprops/action-gh-release@efb35369e0ad2afab669f228072c1b0d510eae64 # v2
        with:
          files: |
            region-proxy-${{ github.ref_name }}-macos.tar.gz
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish-crates:
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@4be9e76fd7c4901c61fb841f559994984270fce7 # stable

      - name: Publish to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --token "$CARGO_REGISTRY_TOKEN"

  update-homebrew:
    needs: [package, release]
    runs-on: ubuntu-latest
    steps:
      - name: Update Homebrew formula
        uses: peter-evans/repository-dispatch@28959ce8df70de7be546dd1250a005dd32156697 # v4.0.1
        with:
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}
          repository: M-Igashi/homebrew-tap
          event-type: update-formula
          client-payload: |
            {
              "version": "${{ github.ref_name }}",
              "sha256": "${{ needs.package.outputs.sha256 }}"
            }