Skip to content

Commit e5882bc

Browse files
authored
Publish to RubyGems when a GitHub release is published (#44)
Runs the tests, checks that the tag matches the gemspec version, builds the gem and pushes it with trusted publishing. A manual run does everything except the push.
1 parent b6d409b commit e5882bc

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

‎.github/workflows/publish.yml‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
name: publish
3+
4+
on:
5+
# Publishing a GitHub release creates the tag and pushes the gem.
6+
release:
7+
types: [published]
8+
9+
# A manual run tests, builds and signs in to RubyGems, but does not push.
10+
workflow_dispatch:
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
rubygems:
17+
runs-on: ubuntu-latest
18+
19+
permissions:
20+
contents: read
21+
# Trusted publishing: RubyGems swaps the OIDC token for a short-lived API key.
22+
id-token: write
23+
24+
steps:
25+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
26+
- name: Set up Ruby
27+
uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0
28+
with:
29+
ruby-version: '3.4'
30+
- name: gem install
31+
run: gem install minitest timecop
32+
- name: test
33+
run: rake
34+
# A published version cannot be replaced, so stop before the push if the
35+
# release was tagged without the version bump.
36+
- name: Check that the tag matches the gemspec version
37+
if: github.event_name == 'release'
38+
run: |
39+
version=$(ruby -e 'puts Gem::Specification.load("personnummer.gemspec").version')
40+
if [ "v$version" != "$GITHUB_REF_NAME" ]; then
41+
echo "::error::Tag $GITHUB_REF_NAME does not match gemspec version $version"
42+
exit 1
43+
fi
44+
- name: build
45+
run: gem build personnummer.gemspec
46+
- name: Sign in to RubyGems
47+
uses: rubygems/configure-rubygems-credentials@dc5a8d8553e6ee01fc26761a49e99e733d17954a # v2.1.0
48+
- name: push
49+
if: github.event_name == 'release'
50+
run: gem push personnummer-*.gem

0 commit comments

Comments
 (0)