name: Publish to crates.io
on:
workflow_run:
workflows: ["CI"]
types:
- completed
branches: [ main ]
jobs:
publish:
name: Publish to crates.io
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Get current version
id: version
run: |
VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Current version: $VERSION"
- name: Check if version already published
id: check-published
run: |
CRATE_NAME=$(grep '^name = ' Cargo.toml | head -1 | sed 's/name = "\(.*\)"/\1/')
VERSION="${{ steps.version.outputs.version }}"
echo "Checking if $CRATE_NAME version $VERSION is already published..."
# Try to download the exact version from crates.io
if cargo search "$CRATE_NAME" --limit 1 | grep -q "^$CRATE_NAME = \"$VERSION\""; then
echo "already_published=true" >> $GITHUB_OUTPUT
echo "⚠️ Version $VERSION is already published on crates.io"
else
echo "already_published=false" >> $GITHUB_OUTPUT
echo "✅ Version $VERSION is not yet published"
fi
- name: Run tests
if: steps.check-published.outputs.already_published == 'false'
run: cargo test --workspace --all-features
- name: Verify package
if: steps.check-published.outputs.already_published == 'false'
run: cargo package --allow-dirty
- name: Publish to crates.io
if: steps.check-published.outputs.already_published == 'false'
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_TOKEN }}
run: |
if [ -z "$CARGO_REGISTRY_TOKEN" ]; then
echo "❌ CARGO_REGISTRY_TOKEN is empty or not set!"
exit 1
fi
echo "Publishing version ${{ steps.version.outputs.version }} to crates.io..."
cargo publish --token "$CARGO_REGISTRY_TOKEN"
echo "✅ Successfully published to crates.io!"
- name: Create Git tag
if: steps.check-published.outputs.already_published == 'false'
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a "v$VERSION" -m "Release v$VERSION"
git push origin "v$VERSION"
echo "✅ Created and pushed tag v$VERSION"
- name: Create GitHub Release
if: steps.check-published.outputs.already_published == 'false'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ steps.version.outputs.version }}
release_name: Release v${{ steps.version.outputs.version }}
body: |
Release v${{ steps.version.outputs.version }}
See [CHANGELOG.md](https://github.com/${{ github.repository }}/blob/main/CHANGELOG.md) for details.
**Install:**
```toml
[dependencies]
atento-core = "${{ steps.version.outputs.version }}"
```
draft: false
prerelease: false
- name: Skip publishing (already published)
if: steps.check-published.outputs.already_published == 'true'
run: |
echo "ℹ️ Skipping publish - version ${{ steps.version.outputs.version }} already exists on crates.io"