name: Publish to Crates.io
on:
push:
branches: [ main ]
paths: [ "Cargo.toml" ]
workflow_dispatch: {}
env:
CARGO_TERM_COLOR: always
jobs:
publish:
name: Pulish to crates.io
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Cache Cargo
uses: Swatinem/rust-cache@v1.3.0
- name: Define Tag and Publish to crates.io
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CRATES_IO_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
run: |
TAG=v$(cargo metadata --no-deps --quiet | jq -r '.packages[0] | .version')
git fetch --tags
if [ $(git tag -l $TAG) ]
then
echo "Git tag $TAG already exists; skipping publishing to crates.io."
else
git config user.name "Add Tag from CI"
git config user.email ""
git tag $TAG
git push --tags
cargo login $CRATES_IO_TOKEN
cargo publish
fi