Skip to content

Commit 1eef8a5

Browse files
authored
Factor out publish action to semver-bump-and-cargo-publish (#7)
* Factor out publish action to semver-bump-and-cargo-publish * Use semicolon, not comma, as wait_for_checks separator
1 parent 6a580ec commit 1eef8a5

1 file changed

Lines changed: 25 additions & 177 deletions

File tree

.github/workflows/publish.yml

Lines changed: 25 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ on:
2323
default: true
2424
type: boolean
2525

26-
env:
27-
CARGO_TERM_COLOR: always
28-
29-
# Grant permissions for GITHUB_TOKEN (alternative to PAT)
26+
# Grant permissions for GITHUB_TOKEN
3027
permissions:
3128
contents: write
3229
pull-requests: read
@@ -39,180 +36,31 @@ jobs:
3936
- uses: actions/checkout@v4
4037
with:
4138
ref: ${{ github.event.inputs.branch }}
42-
token: ${{ secrets.GITHUB_TOKEN }}
39+
token: ${{ secrets.PAT_TOKEN }}
4340
fetch-depth: 0
44-
persist-credentials: false # we set up our own PAT later to push to 'main', bypassing checks
45-
46-
- name: Install Rust
47-
uses: dtolnay/rust-toolchain@stable
48-
with:
49-
toolchain: nightly-2025-09-30
50-
components: clippy, rustfmt
51-
52-
- name: Cache cargo dependencies
53-
uses: actions/cache@v4
54-
with:
55-
path: |
56-
~/.cargo/registry
57-
~/.cargo/git
58-
target
59-
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
60-
restore-keys: |
61-
${{ runner.os }}-cargo-
62-
63-
- name: Cache cargo tools
64-
uses: actions/cache@v4
65-
with:
66-
path: ~/.cargo/bin
67-
key: cargo-tools-${{ runner.os }}-semver-bump-1.0.1-tomato-toml-1.0.0
68-
69-
- name: Install cargo tools
70-
run: |
71-
if ! command -v semver-bump &> /dev/null; then
72-
cargo install semver-bump --version 1.0.1
73-
fi
74-
if ! command -v tomato &> /dev/null; then
75-
cargo install tomato-toml --version 1.0.0
76-
fi
77-
78-
- name: Validate tokens
79-
run: |
80-
if [ -z "${{ secrets.CARGO_REGISTRY_TOKEN }}" ]; then
81-
echo "⚠️ CARGO_REGISTRY_TOKEN secret is not set"
82-
exit 1
83-
fi
84-
if [ -z "${{ secrets.PAT_TOKEN }}" ]; then
85-
echo "⚠️ PAT_TOKEN secret is not set"
86-
exit 1
87-
fi
88-
echo "✅ All required tokens are configured"
89-
90-
- name: Get package info
91-
id: package_info
92-
run: |
93-
PACKAGE_NAME=$(tomato get package.name Cargo.toml)
94-
CURRENT_VERSION=$(tomato get package.version Cargo.toml)
95-
echo "name=$PACKAGE_NAME" >> $GITHUB_OUTPUT
96-
echo "current=$CURRENT_VERSION" >> $GITHUB_OUTPUT
97-
echo "Package: $PACKAGE_NAME"
98-
echo "Current version: $CURRENT_VERSION"
99-
100-
- name: Check if tag already exists
101-
run: |
102-
CURRENT_VERSION=$(tomato get package.version Cargo.toml)
103-
NEW_VERSION=$(echo "$CURRENT_VERSION" | semver-bump ${{ github.event.inputs.bump_type }})
104-
if [ "${{ github.event.inputs.branch }}" = "main" ]; then
105-
TAG_NAME="v$NEW_VERSION"
106-
else
107-
TAG_NAME="${{ github.event.inputs.branch }}-v$NEW_VERSION"
108-
fi
109-
echo "TAG_NAME=$TAG_NAME" >> $GITHUB_ENV
110-
if git tag -l | grep -q "^$TAG_NAME$"; then
111-
echo "Tag $TAG_NAME already exists!"
112-
exit 1
113-
fi
114-
115-
- name: Bump version
116-
run: |
117-
CURRENT_VERSION=$(tomato get package.version Cargo.toml)
118-
NEW_VERSION=$(echo "$CURRENT_VERSION" | semver-bump ${{ github.event.inputs.bump_type }})
119-
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
120-
tomato set package.version "$NEW_VERSION" Cargo.toml
121-
echo "Bumped version from $CURRENT_VERSION to $NEW_VERSION"
122-
123-
- name: Update Cargo.lock
124-
run: |
125-
cargo update -p ${{ steps.package_info.outputs.name }}
126-
echo "Updated Cargo.lock"
127-
128-
- name: Verify version change
129-
run: |
130-
NEW_VERSION_CHECK=$(tomato get package.version Cargo.toml)
131-
if [ "$NEW_VERSION_CHECK" != "${{ env.NEW_VERSION }}" ]; then
132-
echo "Version update failed. Expected ${{ env.NEW_VERSION }}, got $NEW_VERSION_CHECK"
133-
exit 1
134-
fi
135-
echo "Version update verified: $NEW_VERSION_CHECK"
13641

137-
- name: Run tests
138-
run: cargo test --verbose
139-
140-
- name: Run clippy
141-
run: cargo clippy --all-targets --all-features -- -D warnings
142-
143-
- name: Check formatting
144-
run: cargo fmt -- --check
145-
146-
- name: Build package
147-
run: cargo build --release
148-
149-
- name: Configure git
150-
run: |
151-
git config --local user.email "nikhilidiculla@gmail.com"
152-
git config --local user.name "Nikhil Idiculla (via GitHub Actions)"
153-
git remote set-url origin https://tsnl:${{ secrets.PAT_TOKEN }}@github.com/${{ github.repository }}.git
154-
155-
- name: Commit version bump (but don't push yet)
156-
run: |
157-
git add Cargo.toml Cargo.lock
158-
git commit -m "Bump version to ${{ env.TAG_NAME }}"
159-
git tag "${{ env.TAG_NAME }}"
160-
echo "Created commit and tag for ${{ env.TAG_NAME }}"
161-
162-
- name: Dry run publish
163-
run: cargo publish --dry-run
164-
165-
- name: Push changes since dry run publish succeeded
166-
if: ${{ github.event.inputs.dry_run != 'true' }}
167-
run: |
168-
git push origin ${{ github.event.inputs.branch }}
169-
git push origin "${{ env.TAG_NAME }}"
170-
echo "Pushed changes and tag to repository"
171-
172-
- name: Publish to crates.io
173-
if: ${{ github.event.inputs.dry_run != 'true' && github.event.inputs.branch == 'main' }}
42+
- name: Publish crate with `tsnl/semver-bump-and-cargo-publish`
17443
id: publish
175-
run: cargo publish
176-
env:
177-
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
178-
continue-on-error: true
179-
180-
- name: Rollback if publish failed
181-
if: ${{ github.event.inputs.dry_run != 'true' && github.event.inputs.branch == 'main' && steps.publish.outcome == 'failure' }}
182-
run: |
183-
echo "🔄 Publishing failed, rolling back changes..."
184-
185-
# Delete the tag locally and remotely
186-
git tag -d "${{ env.TAG_NAME }}"
187-
git push origin --delete "${{ env.TAG_NAME }}" || echo "Tag may not exist on remote"
188-
189-
# Reset to previous commit (undo version bump commit)
190-
git reset --hard HEAD~1
191-
192-
# Force push to revert the commit on main
193-
git push --force-with-lease origin ${{ github.event.inputs.branch }}
194-
195-
echo "❌ Rollback completed. Version bump and tag have been reverted."
196-
exit 1
197-
198-
- name: Explain skip publish if non-main branch
199-
if: ${{ github.event.inputs.dry_run != 'true' && github.event.inputs.branch != 'main' }}
200-
run: |
201-
echo "⚠️ Skipping cargo publish because this is not the main branch"
202-
echo "cargo publish can only be run from the main branch"
203-
echo "Current branch: ${{ github.event.inputs.branch }}"
204-
205-
- name: Summarize dry run
206-
if: ${{ github.event.inputs.dry_run == 'true' }}
44+
uses: tsnl/semver-bump-and-cargo-publish@main
45+
with:
46+
branch: ${{ github.event.inputs.branch }}
47+
bump_type: ${{ github.event.inputs.bump_type }}
48+
dry_run: ${{ github.event.inputs.dry_run }}
49+
cargo_registry_token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
50+
pat_token: ${{ secrets.PAT_TOKEN }}
51+
wait_for_checks: "Test; Lint; Check Formatting"
52+
check_wait_interval: "60"
53+
check_timeout_count: "20"
54+
rust_toolchain: "nightly-2025-09-30"
55+
git_user_email: "nikhilidiculla@gmail.com"
56+
git_user_name: "Nikhil Idiculla (via GitHub Actions)"
57+
58+
- name: Output summary
20759
run: |
208-
echo "🔍 DRY RUN COMPLETED"
209-
echo "Package: ${{ steps.package_info.outputs.name }}"
210-
echo "Branch: ${{ github.event.inputs.branch }}"
211-
echo "Version would be bumped from ${{ steps.package_info.outputs.current }} to ${{ env.NEW_VERSION }}"
212-
echo "Changes would be committed and tagged as ${{ env.TAG_NAME }}"
213-
if [ "${{ github.event.inputs.branch }}" = "main" ]; then
214-
echo "Package would be published to crates.io"
215-
else
216-
echo "Package would NOT be published to crates.io (non-main branch)"
217-
fi
218-
echo "No actual changes were made to the repository or crates.io"
60+
echo "## 📦 Publish Summary" >> $GITHUB_STEP_SUMMARY
61+
echo "- **Package**: ${{ steps.publish.outputs.package_name }}" >> $GITHUB_STEP_SUMMARY
62+
echo "- **Branch**: ${{ github.event.inputs.branch }}" >> $GITHUB_STEP_SUMMARY
63+
echo "- **Version**: ${{ steps.publish.outputs.old_version }} → ${{ steps.publish.outputs.new_version }}" >> $GITHUB_STEP_SUMMARY
64+
echo "- **Tag**: ${{ steps.publish.outputs.tag_name }}" >> $GITHUB_STEP_SUMMARY
65+
echo "- **Published to crates.io**: ${{ steps.publish.outputs.published }}" >> $GITHUB_STEP_SUMMARY
66+
echo "- **Dry run**: ${{ github.event.inputs.dry_run }}" >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)