|
| 1 | +name: Build and Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + determine_release: |
| 13 | + name: Determine if release is needed |
| 14 | + runs-on: ubuntu-latest |
| 15 | + outputs: |
| 16 | + SHOULD_RELEASE: ${{ steps.release_check.outputs.SHOULD_RELEASE }} |
| 17 | + NEW_TAG: ${{ steps.release_check.outputs.NEW_TAG }} |
| 18 | + steps: |
| 19 | + - name: Checkout Code |
| 20 | + uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 |
| 23 | + |
| 24 | + - name: Determine if release is needed |
| 25 | + id: release_check |
| 26 | + shell: bash |
| 27 | + run: | |
| 28 | + git fetch --tags |
| 29 | + CURRENT_VERSION=$(grep '^version = ' Cargo.toml | head -n1 | awk -F\" '{print $2}') |
| 30 | + echo "Current version: $CURRENT_VERSION" |
| 31 | + if git rev-parse "v$CURRENT_VERSION" >/dev/null 2>&1; then |
| 32 | + echo "Tag v$CURRENT_VERSION already exists. No release needed." |
| 33 | + echo "SHOULD_RELEASE=false" >> $GITHUB_OUTPUT |
| 34 | + else |
| 35 | + echo "No existing tag for version $CURRENT_VERSION. Release is needed." |
| 36 | + echo "SHOULD_RELEASE=true" >> $GITHUB_OUTPUT |
| 37 | + echo "NEW_TAG=v$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 38 | + fi |
| 39 | +
|
| 40 | + build: |
| 41 | + name: Build |
| 42 | + runs-on: ${{ matrix.runs-on }} |
| 43 | + needs: determine_release |
| 44 | + if: needs.determine_release.outputs.SHOULD_RELEASE == 'true' |
| 45 | + strategy: |
| 46 | + matrix: |
| 47 | + include: |
| 48 | + - name: linux_x86_64 |
| 49 | + runs-on: ubuntu-latest |
| 50 | + target: x86_64-unknown-linux-gnu |
| 51 | + artifact_name: grimoire-css-watcher-linux-x86_64 |
| 52 | + artifact_path: target/x86_64-unknown-linux-gnu/release/grimoire-css-watcher |
| 53 | + - name: macos_x86_64 |
| 54 | + runs-on: macos-latest |
| 55 | + target: x86_64-apple-darwin |
| 56 | + artifact_name: grimoire-css-watcher-macos-x86_64 |
| 57 | + artifact_path: target/x86_64-apple-darwin/release/grimoire-css-watcher |
| 58 | + - name: macos_arm64 |
| 59 | + runs-on: macos-latest |
| 60 | + target: aarch64-apple-darwin |
| 61 | + artifact_name: grimoire-css-watcher-macos-arm64 |
| 62 | + artifact_path: target/aarch64-apple-darwin/release/grimoire-css-watcher |
| 63 | + - name: windows_x86_64 |
| 64 | + runs-on: windows-latest |
| 65 | + target: x86_64-pc-windows-msvc |
| 66 | + artifact_name: grimoire-css-watcher-windows-x86_64.exe |
| 67 | + artifact_path: target/x86_64-pc-windows-msvc/release/grimoire-css-watcher.exe |
| 68 | + |
| 69 | + steps: |
| 70 | + - name: Checkout Code |
| 71 | + uses: actions/checkout@v4 |
| 72 | + with: |
| 73 | + fetch-depth: 0 |
| 74 | + |
| 75 | + - name: Set up Rust |
| 76 | + uses: actions-rs/toolchain@v1 |
| 77 | + with: |
| 78 | + profile: minimal |
| 79 | + toolchain: stable |
| 80 | + target: ${{ matrix.target }} |
| 81 | + |
| 82 | + - name: Cache Cargo registry |
| 83 | + uses: actions/cache@v4 |
| 84 | + with: |
| 85 | + path: ~/.cargo/registry |
| 86 | + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} |
| 87 | + restore-keys: | |
| 88 | + ${{ runner.os }}-cargo-registry- |
| 89 | +
|
| 90 | + - name: Cache Cargo git |
| 91 | + uses: actions/cache@v4 |
| 92 | + with: |
| 93 | + path: ~/.cargo/git |
| 94 | + key: ${{ runner.os }}-cargo-git-${{ hashFiles('**/Cargo.lock') }} |
| 95 | + restore-keys: | |
| 96 | + ${{ runner.os }}-cargo-git- |
| 97 | +
|
| 98 | + - name: Cache Cargo build |
| 99 | + uses: actions/cache@v4 |
| 100 | + with: |
| 101 | + path: target |
| 102 | + key: ${{ runner.os }}-build-${{ matrix.target }}-${{ hashFiles('**/Cargo.lock') }} |
| 103 | + restore-keys: | |
| 104 | + ${{ runner.os }}-build-${{ matrix.target }}- |
| 105 | +
|
| 106 | + - name: Build project |
| 107 | + run: cargo build --release --target ${{ matrix.target }} |
| 108 | + |
| 109 | + - name: Prepare artifact |
| 110 | + run: | |
| 111 | + mkdir -p artifacts |
| 112 | + cp "${{ matrix.artifact_path }}" "artifacts/${{ matrix.artifact_name }}" |
| 113 | +
|
| 114 | + - name: Upload artifacts |
| 115 | + uses: actions/upload-artifact@v4 |
| 116 | + with: |
| 117 | + name: ${{ matrix.artifact_name }} |
| 118 | + path: artifacts/${{ matrix.artifact_name }} |
| 119 | + |
| 120 | + publish: |
| 121 | + name: Publish to crates.io |
| 122 | + runs-on: ubuntu-latest |
| 123 | + needs: [determine_release, build] |
| 124 | + if: needs.determine_release.outputs.SHOULD_RELEASE == 'true' |
| 125 | + steps: |
| 126 | + - name: Checkout Code |
| 127 | + uses: actions/checkout@v4 |
| 128 | + |
| 129 | + - name: Set up Rust |
| 130 | + uses: actions-rs/toolchain@v1 |
| 131 | + with: |
| 132 | + profile: minimal |
| 133 | + toolchain: stable |
| 134 | + |
| 135 | + - name: Cache Cargo registry |
| 136 | + uses: actions/cache@v4 |
| 137 | + with: |
| 138 | + path: ~/.cargo/registry |
| 139 | + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} |
| 140 | + restore-keys: | |
| 141 | + ${{ runner.os }}-cargo-registry- |
| 142 | +
|
| 143 | + - name: Cache Cargo git |
| 144 | + uses: actions/cache@v4 |
| 145 | + with: |
| 146 | + path: ~/.cargo/git |
| 147 | + key: ${{ runner.os }}-cargo-git-${{ hashFiles('**/Cargo.lock') }} |
| 148 | + restore-keys: | |
| 149 | + ${{ runner.os }}-cargo-git- |
| 150 | +
|
| 151 | + - name: Publish to crates.io |
| 152 | + env: |
| 153 | + CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} |
| 154 | + run: | |
| 155 | + cargo login ${{ secrets.CARGO_REGISTRY_TOKEN }} |
| 156 | + cargo publish |
| 157 | +
|
| 158 | + release: |
| 159 | + name: Release |
| 160 | + runs-on: ubuntu-latest |
| 161 | + needs: [determine_release, publish] |
| 162 | + if: needs.determine_release.outputs.SHOULD_RELEASE == 'true' |
| 163 | + steps: |
| 164 | + - name: Checkout Code |
| 165 | + uses: actions/checkout@v4 |
| 166 | + with: |
| 167 | + fetch-depth: 0 |
| 168 | + |
| 169 | + - name: Download artifacts |
| 170 | + uses: actions/download-artifact@v4 |
| 171 | + with: |
| 172 | + path: ./artifacts |
| 173 | + |
| 174 | + - name: Create and push tag |
| 175 | + run: | |
| 176 | + git config user.name "${{ github.actor }}" |
| 177 | + git config user.email "${{ github.actor }}@users.noreply.github.com" |
| 178 | + git tag -a "${{ needs.determine_release.outputs.NEW_TAG }}" -m "Release ${{ needs.determine_release.outputs.NEW_TAG }}" |
| 179 | + git push origin "${{ needs.determine_release.outputs.NEW_TAG }}" |
| 180 | +
|
| 181 | + - name: Create GitHub Release |
| 182 | + uses: softprops/action-gh-release@v1 |
| 183 | + with: |
| 184 | + tag_name: ${{ needs.determine_release.outputs.NEW_TAG }} |
| 185 | + files: ./artifacts/**/* |
| 186 | + body: "Release of grimoire-css-watcher version ${{ needs.determine_release.outputs.NEW_TAG }}" |
| 187 | + draft: false |
| 188 | + prerelease: false |
| 189 | + env: |
| 190 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments