|
| 1 | +name: Tag on Merge and Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [closed] |
| 6 | + branches: [main] |
| 7 | + push: |
| 8 | + tags: |
| 9 | + - "v*" |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + packages: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + determine-tag: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + outputs: |
| 19 | + tag_name: ${{ steps.determine.outputs.tag }} |
| 20 | + steps: |
| 21 | + - name: Checkout Repository |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 0 |
| 25 | + |
| 26 | + - name: Determine tag to use |
| 27 | + id: determine |
| 28 | + shell: bash |
| 29 | + run: | |
| 30 | + echo "Event: ${GITHUB_EVENT_NAME}" |
| 31 | + if [ "${GITHUB_EVENT_NAME}" = "pull_request" ]; then |
| 32 | + if [ "${{ github.event.pull_request.merged }}" = "true" ]; then |
| 33 | + echo "PR merged, creating tag..." |
| 34 | + branch="${GITHUB_HEAD_REF}" |
| 35 | + # S'assurer que la branche commence par 'release/' |
| 36 | + if [[ "$branch" != release/* ]]; then |
| 37 | + echo "Error: Branch '$branch' does not start with 'release/'" |
| 38 | + exit 1 |
| 39 | + fi |
| 40 | + version="${branch#release/}" |
| 41 | + if [ -z "$version" ]; then |
| 42 | + echo "Error: Extracted version is empty." |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | + tag="v${version}" |
| 46 | + echo "Creating tag: $tag" |
| 47 | + git config user.name "GitHub Actions" |
| 48 | + git config user.email "actions@github.com" |
| 49 | + git tag "$tag" -a -m "Release version $version" |
| 50 | + git push origin "$tag" |
| 51 | + echo "tag=$tag" >> $GITHUB_OUTPUT |
| 52 | + else |
| 53 | + echo "PR not merged. Doing nothing." |
| 54 | + # On n'émet pas de tag, ainsi le job suivant sera ignoré. |
| 55 | + echo "tag=" >> $GITHUB_OUTPUT |
| 56 | + exit 0 |
| 57 | + fi |
| 58 | + elif [ "${GITHUB_EVENT_NAME}" = "push" ]; then |
| 59 | + echo "Push event detected." |
| 60 | + tag="${GITHUB_REF#refs/tags/}" |
| 61 | + if [ -z "$tag" ]; then |
| 62 | + echo "Error: TAG value from push event is empty." |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | + echo "Using pushed tag: $tag" |
| 66 | + echo "tag=$tag" >> $GITHUB_OUTPUT |
| 67 | + else |
| 68 | + echo "Error: Unsupported event '${GITHUB_EVENT_NAME}'." |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + env: |
| 72 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 73 | + |
| 74 | + validate-and-release: |
| 75 | + # Le job de déploiement ne s'exécute que si un tag a été déterminé |
| 76 | + if: ${{ needs.determine-tag.outputs.tag_name != '' }} |
| 77 | + needs: determine-tag |
| 78 | + runs-on: windows-latest |
| 79 | + env: |
| 80 | + # Le tag est accessible dans toutes les étapes via la variable TAG_NAME |
| 81 | + TAG_NAME: ${{ needs.determine-tag.outputs.tag_name }} |
| 82 | + CARGO_TERM_COLOR: always |
| 83 | + RUSTFLAGS: -D warnings |
| 84 | + steps: |
| 85 | + - name: Validate TAG_NAME presence |
| 86 | + shell: pwsh |
| 87 | + run: | |
| 88 | + if (-not $env:TAG_NAME -or $env:TAG_NAME -eq "") { |
| 89 | + Write-Error "TAG_NAME is empty. Aborting deployment." |
| 90 | + exit 1 |
| 91 | + } else { |
| 92 | + Write-Host "TAG_NAME is set to $env:TAG_NAME." |
| 93 | + } |
| 94 | +
|
| 95 | + - name: Checkout repository |
| 96 | + uses: actions/checkout@v4 |
| 97 | + |
| 98 | + - name: Cache Cargo Registry |
| 99 | + uses: actions/cache@v3 |
| 100 | + with: |
| 101 | + path: ~/.cargo/registry |
| 102 | + key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} |
| 103 | + restore-keys: ${{ runner.os }}-cargo-registry- |
| 104 | + |
| 105 | + - name: Cache Cargo Git Index |
| 106 | + uses: actions/cache@v3 |
| 107 | + with: |
| 108 | + path: ~/.cargo/git |
| 109 | + key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }} |
| 110 | + restore-keys: ${{ runner.os }}-cargo-index- |
| 111 | + |
| 112 | + - name: Install Rust |
| 113 | + shell: pwsh |
| 114 | + run: rustup update stable && rustup default stable |
| 115 | + |
| 116 | + - name: Install PSSemVer |
| 117 | + shell: pwsh |
| 118 | + run: | |
| 119 | + try { |
| 120 | + Import-Module -Name PSSemVer -ErrorAction Stop |
| 121 | + } catch { |
| 122 | + Write-Host "Installing PSSemVer module..." |
| 123 | + Install-Module -Name PSSemVer -Scope CurrentUser -Force -ErrorAction Stop |
| 124 | + Import-Module -Name PSSemVer -ErrorAction Stop |
| 125 | + } |
| 126 | +
|
| 127 | + - name: Validate tag with PSSemVer |
| 128 | + id: validate |
| 129 | + shell: pwsh |
| 130 | + run: | |
| 131 | + $tagName = "${env:TAG_NAME}" |
| 132 | + Import-Module -Name PSSemVer -ErrorAction Stop |
| 133 | + try { |
| 134 | + $rawVersion = $tagName -replace '^v', '' |
| 135 | + $Version = [PSSemVer]::Parse($rawVersion) |
| 136 | + $prereleaseValue = ($Version.Prerelease -ne $null).ToString().ToLower() |
| 137 | + echo "prerelease=$prereleaseValue" >> $env:GITHUB_ENV |
| 138 | + if (-not [string]::IsNullOrEmpty($Version.BuildMetadata)) { |
| 139 | + echo "nuget_version=$($Version.BuildMetadata)" >> $env:GITHUB_ENV |
| 140 | + } else { |
| 141 | + Write-Host "No metadata found. Using version without metadata for NuGet." |
| 142 | + echo "nuget_version=$rawVersion" >> $env:GITHUB_ENV |
| 143 | + } |
| 144 | + } catch { |
| 145 | + Write-Error "Invalid semantic version in tag: $tagName" |
| 146 | + exit 1 |
| 147 | + } |
| 148 | + continue-on-error: false |
| 149 | + |
| 150 | + - name: Run Cargo Publish in Dry-Run Mode |
| 151 | + run: cargo publish --dry-run |
| 152 | + |
| 153 | + - name: Create GitHub Release |
| 154 | + id: release |
| 155 | + uses: softprops/action-gh-release@v2 |
| 156 | + with: |
| 157 | + tag_name: ${{ env.TAG_NAME }} |
| 158 | + name: ${{ env.prerelease == 'true' && 'Prerelease' || 'Release' }} ${{ env.TAG_NAME }} |
| 159 | + body: | |
| 160 | + This is a **${{ env.prerelease == 'true' && 'prerelease' || 'release' }}** release of **${{ github.repository }}**. |
| 161 | + - The crate is available on [Crates.io](https://crates.io/crates/${{ github.repository.name }}). |
| 162 | + - This version provides bindings for **WSLPluginAPI v${{ env.nuget_version }}**, available on [NuGet](https://www.nuget.org/packages/Microsoft.WSL.PluginApi/${{ env.nuget_version }}). |
| 163 | + prerelease: ${{ env.prerelease == 'true' }} |
| 164 | + |
| 165 | + - name: Cargo Publish on crates.io |
| 166 | + run: cargo publish |
| 167 | + env: |
| 168 | + CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} |
0 commit comments