|
1 | | -name: Build |
| 1 | +name: Build and Release |
2 | 2 |
|
3 | | -on: [push] |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v*' |
4 | 7 |
|
5 | 8 | jobs: |
6 | 9 | build: |
7 | | - runs-on: ubuntu-latest |
| 10 | + name: Build on ${{ matrix.os }} |
| 11 | + runs-on: ${{ matrix.os }} |
| 12 | + strategy: |
| 13 | + matrix: |
| 14 | + os: [ubuntu-latest, macos-latest, windows-latest] |
| 15 | + include: |
| 16 | + - os: ubuntu-latest |
| 17 | + target: x86_64-unknown-linux-gnu |
| 18 | + binary_name: codeinput |
| 19 | + archive_name: codeinput-linux.zip |
| 20 | + binary_path_suffix: release |
| 21 | + - os: macos-latest |
| 22 | + target: x86_64-apple-darwin |
| 23 | + binary_name: codeinput |
| 24 | + archive_name: codeinput-macos.zip |
| 25 | + binary_path_suffix: release |
| 26 | + - os: windows-latest |
| 27 | + target: x86_64-pc-windows-msvc |
| 28 | + binary_name: codeinput.exe |
| 29 | + archive_name: codeinput-windows.zip |
| 30 | + binary_path_suffix: release |
8 | 31 |
|
9 | 32 | steps: |
10 | 33 | - name: Checkout Source |
11 | | - id: checkout-source |
12 | 34 | uses: actions/checkout@v2 |
| 35 | + |
13 | 36 | - name: Set variables |
14 | 37 | id: vars |
15 | 38 | run: | |
16 | | - echo "::set-output name=package_name::$(sed -En 's/name[[:space:]]*=[[:space:]]*"([^"]+)"/\1/p' Cargo.toml | head -1)" |
17 | | - echo "::set-output name=package_version::$(sed -En 's/version[[:space:]]*=[[:space:]]*"([^"]+)"/\1/p' Cargo.toml | head -1)" |
18 | | - - run: | |
19 | | - echo "${{steps.vars.outputs.package_name}}" |
20 | | - echo "${{steps.vars.outputs.package_version}}" |
21 | | - - uses: actions/cache@v4.2.0 |
| 39 | + echo "package_name=$(sed -En 's/name[[:space:]]*=[[:space:]]*\"([^\"]+)\"/\1/p' Cargo.toml | head -1)" >> $GITHUB_OUTPUT |
| 40 | + echo "package_version=$(sed -En 's/version[[:space:]]*=[[:space:]]*\"([^\"]+)\"/\1/p' Cargo.toml | head -1)" >> $GITHUB_OUTPUT |
| 41 | + shell: bash |
| 42 | + |
| 43 | + - name: Output variables (for debugging) |
| 44 | + run: | |
| 45 | + echo "Package Name: ${{ steps.vars.outputs.package_name }}" |
| 46 | + echo "Package Version: ${{ steps.vars.outputs.package_version }}" |
| 47 | + echo "Target: ${{ matrix.target }}" |
| 48 | + echo "Binary Name: ${{ matrix.binary_name }}" |
| 49 | + echo "Archive Name: ${{ matrix.archive_name }}" |
| 50 | + shell: bash |
| 51 | + |
| 52 | + - name: Cache Cargo dependencies |
| 53 | + uses: actions/cache@v4 |
22 | 54 | with: |
23 | 55 | path: | |
24 | 56 | ~/.cargo/registry |
25 | 57 | ~/.cargo/git |
26 | 58 | target |
27 | | - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} |
| 59 | + key: ${{ matrix.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} |
| 60 | + |
| 61 | + - name: Install Rust target |
| 62 | + run: rustup target add ${{ matrix.target }} |
| 63 | + shell: bash |
28 | 64 |
|
29 | 65 | - name: Build |
30 | | - id: build-release |
31 | | - run: cargo build --release |
| 66 | + run: cargo build --release --target ${{ matrix.target }} |
| 67 | + shell: bash |
| 68 | + |
| 69 | + - name: Prepare archive |
| 70 | + shell: bash |
| 71 | + run: | |
| 72 | + BINARY_PATH="target/${{ matrix.target }}/${{ matrix.binary_path_suffix }}/${{ matrix.binary_name }}" |
| 73 | + echo "Binary path is $BINARY_PATH" |
| 74 | + if [ ! -f "$BINARY_PATH" ]; then |
| 75 | + echo "Binary not found at $BINARY_PATH" |
| 76 | + ls -R target |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + if [[ "${{ matrix.os }}" == "ubuntu-latest" || "${{ matrix.os }}" == "macos-latest" ]]; then |
| 80 | + chmod +x "$BINARY_PATH" |
| 81 | + fi |
| 82 | + |
| 83 | + # Create a staging directory |
| 84 | + STAGING_DIR="staging" |
| 85 | + mkdir -p "$STAGING_DIR" |
| 86 | + |
| 87 | + # Copy the binary to the staging directory |
| 88 | + cp "$BINARY_PATH" "$STAGING_DIR/" |
| 89 | + |
| 90 | + # Zip the contents of the staging directory |
| 91 | + cd "$STAGING_DIR" |
| 92 | + zip ../${{ matrix.archive_name }} ${{ matrix.binary_name }} |
| 93 | + cd .. |
| 94 | + echo "Created archive ${{ matrix.archive_name }}" |
32 | 95 |
|
33 | | - - name: Zip release |
34 | | - id: zip-release |
35 | | - run: zip -j build.zip target/release/codeinput |
36 | 96 |
|
37 | | - - name: Artifact Production |
38 | | - id: create-artifact |
| 97 | + - name: Upload build artifact |
39 | 98 | uses: actions/upload-artifact@v4 |
40 | 99 | with: |
41 | | - name: build |
42 | | - path: build.zip |
| 100 | + name: build-${{ matrix.os }} # e.g., build-ubuntu-latest |
| 101 | + path: ${{ matrix.archive_name }} |
| 102 | + |
| 103 | + release: |
| 104 | + name: Create GitHub Release |
| 105 | + runs-on: ubuntu-latest |
| 106 | + needs: build # Ensures this job runs after all build jobs in the matrix are complete |
| 107 | + permissions: |
| 108 | + contents: write # Required to create releases and upload assets |
| 109 | + actions: read # Required to download artifacts |
| 110 | + |
| 111 | + steps: |
| 112 | + - name: Checkout Source (for version info) |
| 113 | + uses: actions/checkout@v2 |
| 114 | + |
| 115 | + - name: Set version variables from tag |
| 116 | + id: tag_vars |
| 117 | + run: | |
| 118 | + TAG_NAME="${{ github.ref_name }}" |
| 119 | + # Assuming tag is vA.B.C, package_version becomes A.B.C |
| 120 | + PACKAGE_VERSION="${TAG_NAME#v}" |
| 121 | + echo "package_version=${PACKAGE_VERSION}" >> $GITHUB_OUTPUT |
| 122 | + # We can try to get package_name from Cargo.toml if needed, or define it statically |
| 123 | + # For simplicity, let's try Cargo.toml first |
| 124 | + PACKAGE_NAME=$(sed -En 's/name[[:space:]]*=[[:space:]]*"([^"]+)"/\1/p' Cargo.toml | head -1) |
| 125 | + echo "package_name=${PACKAGE_NAME}" >> $GITHUB_OUTPUT |
| 126 | + shell: bash |
| 127 | + |
| 128 | + - name: Output version variables (for debugging) |
| 129 | + run: | |
| 130 | + echo "Package Name: ${{ steps.tag_vars.outputs.package_name }}" |
| 131 | + echo "Package Version: ${{ steps.tag_vars.outputs.package_version }}" |
| 132 | + shell: bash |
| 133 | + |
| 134 | + - name: Download all build artifacts |
| 135 | + uses: actions/download-artifact@v4 |
| 136 | + with: |
| 137 | + path: release-artifacts # Artifacts will be in release-artifacts/build-ubuntu-latest/*, release-artifacts/build-macos-latest/* etc. |
| 138 | + |
| 139 | + - name: List downloaded artifacts (for debugging) |
| 140 | + run: ls -R release-artifacts |
| 141 | + shell: bash |
43 | 142 |
|
44 | | - - name: Remove Same Release |
| 143 | + - name: Remove Same Release (if exists) |
45 | 144 | uses: omarabid-forks/action-rollback@stable |
46 | | - continue-on-error: true |
| 145 | + continue-on-error: true # Important if the release doesn't exist yet |
47 | 146 | with: |
48 | | - tag: ${{ steps.vars.outputs.package_version }} |
| 147 | + tag: ${{ github.ref_name }} # Use the tag that triggered the workflow |
49 | 148 | env: |
50 | 149 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
51 | 150 |
|
52 | 151 | - name: Create Release |
53 | | - id: create-release |
| 152 | + id: create_release |
54 | 153 | uses: actions/create-release@latest |
55 | 154 | env: |
56 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token |
| 155 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
57 | 156 | with: |
58 | | - tag_name: ${{steps.vars.outputs.package_version}} |
59 | | - release_name: Version ${{steps.vars.outputs.package_version}} |
60 | | - body: ${{steps.vars.outputs.package_name}} - ${{steps.vars.outputs.package_version}} |
| 157 | + tag_name: ${{ github.ref_name }} |
| 158 | + release_name: Version ${{ steps.tag_vars.outputs.package_version }} |
| 159 | + body: | |
| 160 | + Release of version ${{ steps.tag_vars.outputs.package_version }} for ${{ steps.tag_vars.outputs.package_name }}. |
| 161 | + Includes builds for Windows, macOS (x86_64), and Linux (x86_64). |
61 | 162 | draft: false |
62 | 163 | prerelease: false |
63 | 164 |
|
64 | | - - name: Upload Artifact |
65 | | - id: upload-release-asset |
66 | | - uses: actions/upload-release-asset@v1 |
| 165 | + - name: Upload Release Assets |
| 166 | + shell: bash |
67 | 167 | env: |
68 | 168 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
69 | | - with: |
70 | | - upload_url: ${{ steps.create-release.outputs.upload_url }} |
71 | | - asset_path: build.zip |
72 | | - asset_name: build.zip |
73 | | - asset_content_type: application/zip |
| 169 | + UPLOAD_URL: ${{ steps.create_release.outputs.upload_url }} |
| 170 | + run: | |
| 171 | + echo "Listing files to upload:" |
| 172 | + find release-artifacts -type f -print0 | while IFS= read -r -d $'\0' file; do |
| 173 | + ARCHIVE_NAME=$(basename "$file") |
| 174 | + echo "Uploading $file as $ARCHIVE_NAME" |
| 175 | + gh release upload "${{ github.ref_name }}" "$file" --clobber |
| 176 | + done |
| 177 | + echo "Finished uploading assets." |
74 | 178 |
|
75 | | - - uses: omarabid-forks/purge-artifacts@v1 |
| 179 | + - name: Purge All Artifacts (optional, cleans up build artifacts after release) |
| 180 | + if: always() # Run this step even if previous steps fail |
| 181 | + uses: omarabid-forks/purge-artifacts@v1 |
76 | 182 | with: |
77 | 183 | token: ${{ secrets.GITHUB_TOKEN }} |
78 | | - expire-in: 0 |
| 184 | + expire-in: 0 # Immediately purge |
| 185 | + # Optional: specify by name if you only want to purge the 'build-*' artifacts |
| 186 | + # name: build-* |
| 187 | +``` |
0 commit comments