|
| 1 | +name: Release |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: write |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + |
| 11 | +jobs: |
| 12 | + check_release: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + outputs: |
| 15 | + releaseExists: ${{ steps.check_release.outputs.releaseExists }} |
| 16 | + steps: |
| 17 | + - name: Check if release exists |
| 18 | + id: check_release |
| 19 | + uses: actions/github-script@v5 |
| 20 | + with: |
| 21 | + script: | |
| 22 | + const { owner, repo } = context.repo; |
| 23 | + const version = "${{ env.VERSION }}"; |
| 24 | + const tag = `v${version}`; |
| 25 | +
|
| 26 | + let releaseExists = false; |
| 27 | + try { |
| 28 | + await github.rest.repos.getReleaseByTag({ |
| 29 | + owner, |
| 30 | + repo, |
| 31 | + tag, |
| 32 | + }); |
| 33 | + releaseExists = true; |
| 34 | + } catch (error) { |
| 35 | + if (error.status !== 404) { |
| 36 | + throw error; |
| 37 | + } |
| 38 | + } |
| 39 | +
|
| 40 | + return { releaseExists }; |
| 41 | +
|
| 42 | + release: |
| 43 | + needs: check_release |
| 44 | + if: ${{ needs.check_release.outputs.releaseExists == 'false' }} |
| 45 | + strategy: |
| 46 | + matrix: |
| 47 | + target: |
| 48 | + - x86_64-unknown-linux-gnu |
| 49 | + - i686-unknown-linux-gnu |
| 50 | + - aarch64-unknown-linux-gnu |
| 51 | + - armv7-unknown-linux-gnueabihf |
| 52 | + - arm-unknown-linux-gnueabihf |
| 53 | + - powerpc64le-unknown-linux-gnu |
| 54 | + - s390x-unknown-linux-gnu |
| 55 | + - aarch64-apple-darwin |
| 56 | + - x86_64-apple-darwin |
| 57 | + runs-on: ${{ (matrix.target == 'aarch64-apple-darwin' || matrix.target == 'x86_64-apple-darwin') && 'macos-latest' || 'ubuntu-latest' }} |
| 58 | + steps: |
| 59 | + - name: Checkout repository |
| 60 | + uses: actions/checkout@v2 |
| 61 | + |
| 62 | + - name: Install Rust |
| 63 | + uses: actions-rs/toolchain@v1 |
| 64 | + with: |
| 65 | + toolchain: stable |
| 66 | + target: ${{ matrix.target }} |
| 67 | + override: true |
| 68 | + |
| 69 | + - name: Build target |
| 70 | + uses: actions-rs/cargo@v1 |
| 71 | + with: |
| 72 | + use-cross: true |
| 73 | + command: build |
| 74 | + args: --release --target ${{ matrix.target }} |
| 75 | + |
| 76 | + - name: Package binaries |
| 77 | + shell: bash |
| 78 | + run: | |
| 79 | + cd target/${{ matrix.target }}/release |
| 80 | + short_target=$(echo "${{ matrix.target }}" | sed 's/-unknown//') |
| 81 | + tar czvf ../../../killport-${short_target}.tar.gz killport |
| 82 | + cd - |
| 83 | +
|
| 84 | + - name: Extract version from Cargo.toml |
| 85 | + id: get_version |
| 86 | + run: | |
| 87 | + VERSION=$(grep '^version =' Cargo.toml | head -n 1 | awk '{print $3}' | tr -d '"' | tr -d "\n") |
| 88 | + echo "VERSION=$VERSION" >> $GITHUB_ENV |
| 89 | + echo "Found version: $VERSION" |
| 90 | + echo "::set-output name=version::$VERSION" |
| 91 | +
|
| 92 | + - name: Create release and upload assets |
| 93 | + uses: softprops/action-gh-release@v1 |
| 94 | + with: |
| 95 | + tag_name: v${{ env.VERSION }} |
| 96 | + name: Release v${{ env.VERSION }} |
| 97 | + files: 'killport*' |
| 98 | + env: |
| 99 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 100 | + |
| 101 | + publish: |
| 102 | + needs: release |
| 103 | + runs-on: ubuntu-latest |
| 104 | + steps: |
| 105 | + - name: Checkout repository |
| 106 | + uses: actions/checkout@v2 |
| 107 | + |
| 108 | + - name: Install Rust |
| 109 | + uses: actions-rs/toolchain@v1 |
| 110 | + with: |
| 111 | + toolchain: stable |
| 112 | + override: true |
| 113 | + |
| 114 | + - name: Publish to crates.io |
| 115 | + run: cargo publish --token ${{ secrets.CRATES_IO }} |
0 commit comments