Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: optimize build_release workflow #21

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 44 additions & 37 deletions .github/workflows/build_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,39 @@ permissions:
contents: write

jobs:
determine_release:
name: Determine if release is needed
runs-on: ubuntu-latest
outputs:
SHOULD_RELEASE: ${{ steps.release_check.outputs.SHOULD_RELEASE }}
NEW_TAG: ${{ steps.release_check.outputs.NEW_TAG }}
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Determine if release is needed
id: release_check
shell: bash
run: |
git fetch --tags
CURRENT_VERSION=$(grep '^version = ' Cargo.toml | head -n1 | awk -F\" '{print $2}')
echo "Current version: $CURRENT_VERSION"
if git rev-parse "v$CURRENT_VERSION" >/dev/null 2>&1; then
echo "Tag v$CURRENT_VERSION already exists. No release needed."
echo "SHOULD_RELEASE=false" >> $GITHUB_OUTPUT
else
echo "No existing tag for version $CURRENT_VERSION. Release is needed."
echo "SHOULD_RELEASE=true" >> $GITHUB_OUTPUT
echo "NEW_TAG=v$CURRENT_VERSION" >> $GITHUB_OUTPUT
fi

build:
name: Build
runs-on: ${{ matrix.runs-on }}
needs: determine_release
if: needs.determine_release.outputs.SHOULD_RELEASE == 'true'
strategy:
matrix:
include:
Expand All @@ -37,32 +67,11 @@ jobs:
artifact_path: target/x86_64-pc-windows-msvc/release/grimoire_css.exe

steps:
- uses: actions/checkout@v4
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Determine if release is needed
id: release_check
shell: bash
run: |
git fetch --tags
CURRENT_VERSION=$(grep '^version = ' Cargo.toml | head -n1 | awk -F\" '{print $2}')
echo "Current version: $CURRENT_VERSION"
if git rev-parse "v$CURRENT_VERSION" >/dev/null 2>&1; then
echo "Tag v$CURRENT_VERSION already exists. No release needed."
echo "SHOULD_RELEASE=false" >> $GITHUB_OUTPUT
else
echo "No existing tag for version $CURRENT_VERSION. Release is needed."
echo "SHOULD_RELEASE=true" >> $GITHUB_OUTPUT
echo "NEW_TAG=v$CURRENT_VERSION" >> $GITHUB_OUTPUT
fi

- name: Stop if no release is needed
if: steps.release_check.outputs.SHOULD_RELEASE == 'false'
run: |
echo "No release needed."
exit 0

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
Expand Down Expand Up @@ -108,17 +117,14 @@ jobs:
name: ${{ matrix.artifact_name }}
path: artifacts/${{ matrix.artifact_name }}

outputs:
SHOULD_RELEASE: ${{ steps.release_check.outputs.SHOULD_RELEASE }}
NEW_TAG: ${{ steps.release_check.outputs.NEW_TAG }}

publish:
name: Publish to crates.io
runs-on: ubuntu-latest
needs: build
if: needs.build.outputs.SHOULD_RELEASE == 'true'
needs: [determine_release, build]
if: needs.determine_release.outputs.SHOULD_RELEASE == 'true'
steps:
- uses: actions/checkout@v4
- name: Checkout Code
uses: actions/checkout@v4

- name: Set up Rust
uses: actions-rs/toolchain@v1
Expand Down Expand Up @@ -152,10 +158,11 @@ jobs:
release:
name: Release
runs-on: ubuntu-latest
needs: [publish]
if: needs.build.outputs.SHOULD_RELEASE == 'true'
needs: [determine_release, publish]
if: needs.determine_release.outputs.SHOULD_RELEASE == 'true'
steps:
- uses: actions/checkout@v4
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0

Expand All @@ -168,15 +175,15 @@ jobs:
run: |
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
git tag -a "${{ needs.build.outputs.NEW_TAG }}" -m "Release ${{ needs.build.outputs.NEW_TAG }}"
git push origin "${{ needs.build.outputs.NEW_TAG }}"
git tag -a "${{ needs.determine_release.outputs.NEW_TAG }}" -m "Release ${{ needs.determine_release.outputs.NEW_TAG }}"
git push origin "${{ needs.determine_release.outputs.NEW_TAG }}"

- name: Create GitHub Release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ needs.build.outputs.NEW_TAG }}
tag_name: ${{ needs.determine_release.outputs.NEW_TAG }}
files: ./artifacts/**/*
body: "Release of Grimoire CSS version ${{ needs.build.outputs.NEW_TAG }}"
body: "Release of Grimoire CSS version ${{ needs.determine_release.outputs.NEW_TAG }}"
draft: false
prerelease: false
env:
Expand Down
Loading