-
Notifications
You must be signed in to change notification settings - Fork 31
130 lines (110 loc) · 3.53 KB
/
Copy pathrelease.yml
File metadata and controls
130 lines (110 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
name: Release
on:
workflow_dispatch:
inputs:
bump:
description: Version bump to release
type: choice
required: true
options:
- patch
- minor
- major
concurrency:
group: release-${{ github.ref_name }}
cancel-in-progress: false
permissions:
contents: write
jobs:
publish-rust:
name: Release Rust Crate
if: ${{ github.ref == 'refs/heads/main' }}
runs-on: ubuntu-latest
environment: Production
env:
CRATE_NAME: tinycortex
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
with:
workspaces: .
- name: Check formatting
run: cargo fmt --all -- --check
- name: Run clippy
run: cargo clippy --all-targets -- -D warnings
- name: Run tests
run: cargo test
- name: Compute next version
id: version
shell: bash
run: |
set -euo pipefail
current_version="$(cargo metadata --format-version 1 --no-deps | jq -r --arg crate "$CRATE_NAME" '.packages[] | select(.name == $crate) | .version')"
if [[ -z "$current_version" ]]; then
echo "Could not resolve current version for crate $CRATE_NAME" >&2
exit 1
fi
IFS=. read -r major minor patch <<< "$current_version"
case "${{ inputs.bump }}" in
major)
major=$((major + 1))
minor=0
patch=0
;;
minor)
minor=$((minor + 1))
patch=0
;;
patch)
patch=$((patch + 1))
;;
*)
echo "Unsupported bump: ${{ inputs.bump }}" >&2
exit 1
;;
esac
next_version="${major}.${minor}.${patch}"
tag="v${next_version}"
git fetch --tags origin
if git rev-parse --verify --quiet "refs/tags/${tag}"; then
echo "Tag ${tag} already exists" >&2
exit 1
fi
{
echo "current_version=${current_version}"
echo "next_version=${next_version}"
echo "tag=${tag}"
} >> "$GITHUB_OUTPUT"
- name: Update crate version
env:
NEXT_VERSION: ${{ steps.version.outputs.next_version }}
run: |
perl -0pi -e 's/(\[package\][\s\S]*?\nversion = ")[^"]+(")/$1$ENV{NEXT_VERSION}$2/' Cargo.toml
cargo update -p "$CRATE_NAME" --precise "$NEXT_VERSION"
- name: Commit version bump and tag
env:
NEXT_VERSION: ${{ steps.version.outputs.next_version }}
RELEASE_TAG: ${{ steps.version.outputs.tag }}
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add Cargo.toml Cargo.lock
git commit -m "Release ${RELEASE_TAG}"
git tag -a "${RELEASE_TAG}" -m "Release ${RELEASE_TAG}"
- name: Package crate
run: cargo package --locked
- name: Push release commit and tag
env:
RELEASE_TAG: ${{ steps.version.outputs.tag }}
run: |
git push origin "HEAD:${GITHUB_REF_NAME}"
git push origin "${RELEASE_TAG}"
- name: Publish to crates.io
run: cargo publish --locked
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}