Skip to content

Commit

Permalink
chore: refact and add last tests check on last commit in release.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
ludndev committed Jul 18, 2024
1 parent 45ea48b commit aaf5c37
Showing 1 changed file with 101 additions and 25 deletions.
126 changes: 101 additions & 25 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,125 @@ env:

jobs:
check-version:
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest

outputs:
version_match: ${{ steps.version_check.outputs.version_match }}

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Check semver
uses: obi1kenobi/cargo-semver-checks-action@v2

- name: Check version
id: version_check
shell: pwsh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Extract tag version (remove 'v' prefix)
$TAG_VERSION = $env:GITHUB_REF -replace '^refs/tags/v', ''
# Extract version from Cargo.toml
$CARGO_VERSION = (Select-String -Path Cargo.toml -Pattern '^version = "(.+)"').Matches.Groups[1].Value
Write-Output "Tag version: $TAG_VERSION"
Write-Output "Cargo.toml version: $CARGO_VERSION"
if ($TAG_VERSION -ne $CARGO_VERSION) {
if ($TAG_VERSION -eq $CARGO_VERSION) {
Write-Output "version_match=true" >> $env:GITHUB_OUTPUT
Write-Output "Versions match. Proceeding with the release check."
} else {
Write-Output "version_match=false" >> $env:GITHUB_OUTPUT
Write-Output "Version mismatch detected!"
}
- name: Display version_match output
run: echo "${{ steps.version_check.outputs.version_match }}"

# Create an issue
$ISSUE_BODY = "Version mismatch detected between tag ($TAG_VERSION) and Cargo.toml ($CARGO_VERSION)."
$ISSUE_URL = gh issue create --title "Version Mismatch in Release $TAG_VERSION" --body $ISSUE_BODY --assignee @me
check-last-commit:
needs: check-version

outputs:
passed_test: ${{ steps.check_last_commit.outputs.passed_test }}

if: needs.check-version.outputs.version_match == 'true'

# If this is a release, change it to a draft
if ($env:GITHUB_EVENT_NAME -eq "release") {
gh release edit $TAG_VERSION --draft
Write-Output "Release has been changed to draft due to version mismatch."
}
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

Write-Output "An issue has been created: $ISSUE_URL"
- name: Check last commit status
id: check_last_commit
shell: pwsh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
$LAST_COMMIT_SHA = git rev-parse HEAD
$CHECK_RUNS = gh api repos/$env:GITHUB_REPOSITORY/commits/$LAST_COMMIT_SHA/check-runs --jq '.check_runs[] | {name, conclusion}'
$FAILED_CHECKS = $CHECK_RUNS | ConvertFrom-Json | Where-Object { $_.conclusion -ne 'success' }
if ($FAILED_CHECKS) {
Write-Output "passed_test=false" >> $env:GITHUB_OUTPUT
$FAILED_NAMES = ($FAILED_CHECKS | ForEach-Object { $_.name }) -join ', '
Write-Output "The last commit does not have all successful tests. Failed checks: $FAILED_NAMES"
exit 1
} else {
Write-Output "Versions match. Proceeding with the release."
Write-Output "passed_test=true" >> $env:GITHUB_OUTPUT
Write-Output "All checks passed for the last commit."
}
- name: Display passed_test output
run: echo "${{ steps.check_last_commit.outputs.passed_test }}"

handle-release:
needs: [check-version, check-last-commit]

if: |
always() &&
(needs.check-version.result == 'failure' || needs.check-last-commit.result == 'failure' || needs.check-version.outputs.version_match == 'false')
outputs:
status: ${{ steps.handle.outputs.status }}

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Handle failed checks
id: handle
shell: pwsh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
$TAG_VERSION = $env:GITHUB_REF -replace '^refs/tags/v', ''
if ('${{ needs.check-version.outputs.version_match }}' -eq 'false') {
$ISSUE_BODY = "Version mismatch detected between tag and Cargo.toml."
} else {
$ISSUE_BODY = "The last commit does not have all successful tests."
}
$ISSUE_BODY += " The release has been unpublished."
$ISSUE_URL = gh issue create --title "Release $TAG_VERSION Check Failed" --body $ISSUE_BODY --assignee @me
if ($env:GITHUB_EVENT_NAME -eq "release") {
Write-Output "status=false" >> $env:GITHUB_OUTPUT
gh release delete $TAG_VERSION --yes
Write-Output "Release has been unpublished due to check failure."
git push origin :refs/tags/v$TAG_VERSION
Write-Output "Tag v$TAG_VERSION has been deleted."
}
Write-Output "status=true" >> $env:GITHUB_OUTPUT
Write-Output "An issue has been created: $ISSUE_URL"
- name: Display status output
run: echo "${{ steps.handle.outputs.status }}"

release:
needs: check-version
needs: handle-release

runs-on: ${{ matrix.os }}

Expand All @@ -79,7 +154,8 @@ jobs:
asset_content_type: application/octet-stream

steps:
- uses: actions/checkout@v4
- name: Checkout
uses: actions/checkout@v4

- name: Install Rust
uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -128,7 +204,7 @@ jobs:
run: echo "Release process failed, check logs for details."

publish-crate:
needs: [check-version, release]
needs: handle-release

runs-on: ubuntu-latest

Expand Down

0 comments on commit aaf5c37

Please sign in to comment.