|
| 1 | +name: Release Build and Upload |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - master # In case the default branch is master |
| 8 | + tags: |
| 9 | + - 'v*' # Trigger on version tags like v0.1.0 |
| 10 | + |
| 11 | +jobs: |
| 12 | + build_and_release: |
| 13 | + name: Build and Release |
| 14 | + runs-on: ubuntu-latest |
| 15 | + strategy: |
| 16 | + matrix: |
| 17 | + # Define targets to build for |
| 18 | + # Note: aarch64-apple-darwin might require specific setup or a newer cross version |
| 19 | + include: |
| 20 | + - target: x86_64-unknown-linux-gnu |
| 21 | + os: linux |
| 22 | + ext: "" |
| 23 | + archive_ext: ".tar.gz" |
| 24 | + artifact_name_suffix: "linux-x86_64" |
| 25 | + - target: x86_64-pc-windows-gnu |
| 26 | + os: windows |
| 27 | + ext: ".exe" |
| 28 | + archive_ext: ".zip" |
| 29 | + artifact_name_suffix: "windows-x86_64" |
| 30 | + - target: x86_64-apple-darwin |
| 31 | + os: macos |
| 32 | + ext: "" |
| 33 | + archive_ext: ".tar.gz" # Using .tar.gz for macOS, .zip is also common |
| 34 | + artifact_name_suffix: "macos-x86_64" |
| 35 | + - target: aarch64-apple-darwin |
| 36 | + os: macos |
| 37 | + ext: "" |
| 38 | + archive_ext: ".tar.gz" |
| 39 | + artifact_name_suffix: "macos-aarch64" |
| 40 | + |
| 41 | + steps: |
| 42 | + - name: Checkout code |
| 43 | + uses: actions/checkout@v4 |
| 44 | + |
| 45 | + - name: Install Rust toolchain |
| 46 | + uses: actions-rs/toolchain@v1 |
| 47 | + with: |
| 48 | + toolchain: stable |
| 49 | + override: true |
| 50 | + components: rustfmt, clippy # Optional: add if you use them in build/test |
| 51 | + |
| 52 | + - name: Install cross |
| 53 | + run: cargo install cross --git https://github.com/cross-rs/cross --branch main |
| 54 | + # For a specific released version, e.g., cargo install cross --version 0.2.5 |
| 55 | + |
| 56 | + - name: Build binary with cross |
| 57 | + run: cross build --target ${{ matrix.target }} --release |
| 58 | + env: |
| 59 | + # For aarch64-apple-darwin, you might need to set specific linkers or SDKs |
| 60 | + # See cross-rs documentation if this target fails. |
| 61 | + # Example (might not be needed or correct for all setups): |
| 62 | + # CFLAGS_aarch64_apple_darwin: "-mmacosx-version-min=10.7" |
| 63 | + # CXXFLAGS_aarch64_apple_darwin: "-mmacosx-version-min=10.7" |
| 64 | + # For some targets, you might need to specify a Docker image for cross |
| 65 | + # CROSS_CONTAINER_ENGINE: docker # or podman |
| 66 | + # CROSS_IMAGE_aarch64_apple_darwin: your-custom-image-for-macos-arm |
| 67 | + CARGO_TERM_COLOR: always # Ensure colors in cargo output |
| 68 | + |
| 69 | + - name: Prepare artifact name and binary path |
| 70 | + id: prep_artifact |
| 71 | + run: | |
| 72 | + BINARY_NAME="pykill" |
| 73 | + TARGET_DIR="target/${{ matrix.target }}/release" |
| 74 | + BASE_ARTIFACT_NAME="${BINARY_NAME}-${{ matrix.target }}" |
| 75 | + RENAMED_BINARY="${BINARY_NAME}-${{ matrix.target }}${{ matrix.ext }}" |
| 76 | + |
| 77 | + echo "Original binary path: ${TARGET_DIR}/${BINARY_NAME}" |
| 78 | + echo "Renamed binary: ${RENAMED_BINARY}" |
| 79 | + |
| 80 | + mv "${TARGET_DIR}/${BINARY_NAME}" "${TARGET_DIR}/${RENAMED_BINARY}" |
| 81 | + |
| 82 | + echo "binary_path=${TARGET_DIR}/${RENAMED_BINARY}" >> $GITHUB_OUTPUT |
| 83 | + echo "artifact_filename=${BASE_ARTIFACT_NAME}${matrix.archive_ext}" >> $GITHUB_OUTPUT |
| 84 | + echo "renamed_binary_name=${RENAMED_BINARY}" >> $GITHUB_OUTPUT |
| 85 | +
|
| 86 | +
|
| 87 | + - name: Package binary (Linux/macOS - tar.gz) |
| 88 | + if: runner.os == 'Linux' && (matrix.os == 'linux' || matrix.os == 'macos') |
| 89 | + run: | |
| 90 | + tar -czvf ${{ steps.prep_artifact.outputs.artifact_filename }} -C target/${{ matrix.target }}/release ${{ steps.prep_artifact.outputs.renamed_binary_name }} |
| 91 | + echo "Packaged ${{ steps.prep_artifact.outputs.artifact_filename }}" |
| 92 | +
|
| 93 | + - name: Package binary (Windows - zip) |
| 94 | + if: runner.os == 'Linux' && matrix.os == 'windows' # Still run on Linux, but package for Windows |
| 95 | + run: | |
| 96 | + zip -j ${{ steps.prep_artifact.outputs.artifact_filename }} target/${{ matrix.target }}/release/${{ steps.prep_artifact.outputs.renamed_binary_name }} |
| 97 | + echo "Packaged ${{ steps.prep_artifact.outputs.artifact_filename }}" |
| 98 | +
|
| 99 | + - name: Upload build artifact |
| 100 | + uses: actions/upload-artifact@v4 |
| 101 | + with: |
| 102 | + name: pykill-${{ matrix.artifact_name_suffix }} # e.g., pykill-linux-x86_64 |
| 103 | + path: ${{ steps.prep_artifact.outputs.artifact_filename }} |
| 104 | + |
| 105 | + # This job depends on all matrix builds completing successfully |
| 106 | + create_release: |
| 107 | + name: Create GitHub Release |
| 108 | + if: startsWith(github.ref, 'refs/tags/') |
| 109 | + needs: build_and_release # Ensure build job (all matrix variations) is complete |
| 110 | + runs-on: ubuntu-latest |
| 111 | + permissions: |
| 112 | + contents: write # Required to create releases |
| 113 | + steps: |
| 114 | + - name: Download all build artifacts |
| 115 | + uses: actions/download-artifact@v4 |
| 116 | + # No 'name' specified downloads all artifacts from the workflow run |
| 117 | + # into a directory named after each artifact |
| 118 | + with: |
| 119 | + path: artifacts/ # All artifacts will be in subdirectories here |
| 120 | + |
| 121 | + - name: List downloaded artifacts (for debugging) |
| 122 | + run: ls -R artifacts/ |
| 123 | + |
| 124 | + - name: Create Release and Upload Assets |
| 125 | + uses: softprops/action-gh-release@v1 |
| 126 | + with: |
| 127 | + # token: ${{ secrets.GITHUB_TOKEN }} # Implicitly available |
| 128 | + files: | |
| 129 | + artifacts/pykill-linux-x86_64/*.tar.gz |
| 130 | + artifacts/pykill-windows-x86_64/*.zip |
| 131 | + artifacts/pykill-macos-x86_64/*.tar.gz |
| 132 | + artifacts/pykill-macos-aarch64/*.tar.gz |
| 133 | + # body_path: CHANGELOG.md # Optional: if you have a changelog |
| 134 | + # draft: false # Optional: set to true to create a draft release |
| 135 | + # prerelease: false # Optional: set to true for pre-releases |
| 136 | + env: |
| 137 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 138 | + # The GITHUB_TOKEN is automatically available to the workflow. |
| 139 | + # softprops/action-gh-release uses it by default. |
| 140 | +``` |
0 commit comments