Repository navigation
158 lines (148 loc) · 6.06 KB
/
Copy pathrelease.yml
File metadata and controls
158 lines (148 loc) · 6.06 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Releases a version when a tag like v1.0.1 is pushed:
# 1. checks that the tag, FlightByte::VERSION and the gemspec agree and that
# CHANGELOG.md has a section for the version, then runs every check;
# 2. publishes to RubyGems with trusted publishing, unless that version is
# already there;
# 3. creates the GitHub release from the version's CHANGELOG.md section.
#
# Run it by hand from the Actions tab to rehearse: it runs every check and
# builds the gem, but publishes and releases nothing.
#
# One-time setup on rubygems.org: add a trusted publisher (a pending one
# before the first release) for the gem flightbyte, the repository
# FlightByte-dev/flightbyte-ruby and the workflow release.yml, from the
# account whose ID is RUBYGEMS_OWNER below.
name: Release
on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'
workflow_dispatch:
permissions:
contents: read
concurrency:
group: release-${{ github.ref }}
cancel-in-progress: false
jobs:
check:
name: Check versions, notes and code
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
published: ${{ steps.version.outputs.published }}
steps:
- uses: actions/checkout@v7
- uses: ruby/setup-ruby@v1
with:
ruby-version: '4.0'
bundler-cache: true
- id: version
name: Compare versions
run: |
version="$(ruby -Ilib -rflightbyte/version -e 'print FlightByte::VERSION')"
gemspec="$(ruby -e 'print Gem::Specification.load("flightbyte.gemspec").version')"
if [[ "$GITHUB_REF_TYPE" == tag && "$GITHUB_REF_NAME" != "v$version" ]]; then
echo "::error::Tag $GITHUB_REF_NAME does not match FlightByte::VERSION $version."
exit 1
fi
if [[ "$gemspec" != "$version" ]]; then
echo "::error::flightbyte.gemspec says $gemspec, FlightByte::VERSION says $version."
exit 1
fi
published=false
if curl -fsS -o /dev/null "https://rubygems.org/api/v2/rubygems/flightbyte/versions/$version.json"; then
published=true
fi
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "published=$published" >> "$GITHUB_OUTPUT"
echo "Version $version, already on RubyGems: $published"
- name: Release notes
run: |
{
echo "## Release notes for ${{ steps.version.outputs.version }}"
echo
tool/release_notes.sh "${{ steps.version.outputs.version }}"
} >> "$GITHUB_STEP_SUMMARY"
- run: bundle exec rubocop
- run: bundle exec rake rbs
- run: bundle exec rake test
publish:
name: RubyGems
needs: check
if: needs.check.outputs.published == 'false'
runs-on: ubuntu-latest
permissions:
contents: read
# Trusted publishing: RubyGems exchanges this for a short-lived API key.
id-token: write
steps:
- uses: actions/checkout@v7
- uses: ruby/setup-ruby@v1
with:
ruby-version: '4.0'
- name: Build
run: gem build flightbyte.gemspec --output "flightbyte-${{ needs.check.outputs.version }}.gem"
- name: Rehearse
if: github.event_name != 'push'
run: |
ruby -rrubygems/package -e 'spec = Gem::Package.new(ARGV[0]).spec; puts spec.full_name, spec.files' \
"flightbyte-${{ needs.check.outputs.version }}.gem"
echo "Rehearsal: the gem was built and nothing was pushed."
# Trusted publishing, done by hand rather than with
# rubygems/configure-rubygems-credentials, which cannot yet name the
# owner: RubyGems turns a pending publisher into a gem only when the
# token exchange names the account that set it up, by its immutable ID.
# The key it hands back is good for 15 minutes and only for gem push.
- name: Publish
if: github.event_name == 'push'
env:
RUBYGEMS_OWNER: '256135'
GEM_FILE: flightbyte-${{ needs.check.outputs.version }}.gem
shell: bash
run: |
jwt="$(curl -sS --fail-with-body \
-H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
"$ACTIONS_ID_TOKEN_REQUEST_URL&audience=rubygems.org" | jq -r .value)"
echo "::add-mask::$jwt"
response="$(jq -n --arg jwt "$jwt" --arg owner "$RUBYGEMS_OWNER" '{jwt: $jwt, owner: $owner}' \
| curl -sS -X POST -H 'content-type: application/json' -H 'accept: application/json' \
--data @- https://rubygems.org/api/v1/oidc/trusted_publisher/exchange_token)"
key="$(jq -r '.rubygems_api_key // empty' <<< "$response" 2> /dev/null || true)"
if [[ -z "$key" ]]; then
echo "::error::RubyGems handed out no API key for this workflow: $response"
exit 1
fi
echo "::add-mask::$key"
echo "API key for $(jq -r '.owner // "an existing gem"' <<< "$response"), expires $(jq -r .expires_at <<< "$response")"
GEM_HOST_API_KEY="$key" gem push "$GEM_FILE"
release:
name: GitHub release
needs: [check, publish]
if: >-
always() &&
github.event_name == 'push' &&
needs.check.result == 'success' &&
(needs.publish.result == 'success' || needs.publish.result == 'skipped')
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v7
- name: Create the release
env:
GH_TOKEN: ${{ github.token }}
VERSION: ${{ needs.check.outputs.version }}
run: |
if gh release view "$GITHUB_REF_NAME" > /dev/null 2>&1; then
echo "Release $GITHUB_REF_NAME already exists."
exit 0
fi
tool/release_notes.sh "$VERSION" > "$RUNNER_TEMP/notes.md"
flags=()
# RubyGems marks a prerelease with letters: 1.1.0.pre1, 1.1.0.rc1.
[[ "$VERSION" =~ [a-zA-Z] ]] && flags+=(--prerelease)
gh release create "$GITHUB_REF_NAME" \
--title "$GITHUB_REF_NAME" \
--notes-file "$RUNNER_TEMP/notes.md" \
--verify-tag \
"${flags[@]}"