-
Notifications
You must be signed in to change notification settings - Fork 0
144 lines (120 loc) · 5.31 KB
/
gem-push.yml
File metadata and controls
144 lines (120 loc) · 5.31 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
name: Gem Release
on:
release:
types: [published]
workflow_dispatch:
inputs:
test_version:
description: 'Test version (e.g. 0.0.99-test) - skips gem pushes if provided'
required: false
type: string
default: ''
jobs:
build:
name: Build + Publish
runs-on: ubuntu-latest
permissions:
contents: write # for commit, push, branch ops
pull-requests: write # for creating/editing PR + adding labels
packages: write
steps:
- uses: actions/checkout@v6
with:
fetch-depth: 0
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: Extract Versions and Package Name
run: |
OLD_VERSION=$(grep 'spec.version' *.gemspec | cut -d'"' -f2 | tr -d ' ')
PACKAGE_NAME=$(grep 'spec.name' *.gemspec | cut -d'"' -f2 | tr -d ' ')
if [ -n "${{ inputs.test_version }}" ]; then
NEW_VERSION="${{ inputs.test_version }}"
IS_TEST_MODE="true"
echo "TEST MODE: Using provided test version ${{ inputs.test_version }} (gem pushes will be skipped)"
else
NEW_VERSION=${GITHUB_REF_NAME#v}
IS_TEST_MODE="false"
echo "REAL MODE: Using tag-derived version $NEW_VERSION"
fi
echo "OLD_VERSION=$OLD_VERSION" >> $GITHUB_ENV
echo "VERSION=$NEW_VERSION" >> $GITHUB_ENV
echo "PACKAGE_NAME=$PACKAGE_NAME" >> $GITHUB_ENV
echo "IS_TEST_MODE=$IS_TEST_MODE" >> $GITHUB_ENV
RELEASE_BRANCH="release/v${NEW_VERSION}"
echo "RELEASE_BRANCH=$RELEASE_BRANCH" >> $GITHUB_ENV
echo "Old version: $OLD_VERSION"
echo "New version: $NEW_VERSION"
echo "Package: $PACKAGE_NAME"
echo "Branch: $RELEASE_BRANCH"
echo "Test mode: $IS_TEST_MODE"
- name: Update Version in gemspec
run: |
sed -i -e "s/spec.version\s*=.*/spec.version = \"${{ env.VERSION }}\"/" \
${{ env.PACKAGE_NAME }}.gemspec
- name: Build Gem
run: gem build ${{ env.PACKAGE_NAME }}.gemspec
- name: Credentials & Push to RubyGems
if: env.IS_TEST_MODE != 'true'
run: |
mkdir -p $HOME/.gem
touch $HOME/.gem/credentials
chmod 0600 $HOME/.gem/credentials
printf -- "---\n:rubygems: ${{ secrets.RUBYGEMS_TOKEN }}\n" > $HOME/.gem/credentials
printf -- ":github: Bearer ${{ secrets.GEM_PUSH_TOKEN }}\n" >> $HOME/.gem/credentials
# Push to GitHub Packages
gem push \
--key=github \
--host=https://rubygems.pkg.github.com/${{ github.repository_owner }} \
${{ env.PACKAGE_NAME }}-*.gem
# Push to rubygems.org
gem push --key=rubygems ${{ env.PACKAGE_NAME }}-*.gem
- name: Commit version bump
run: |
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git add ${{ env.PACKAGE_NAME }}.gemspec
git commit -m "chore: bump version to ${{ env.VERSION }}" || echo "No changes to commit"
- name: Push to release branch
run: git push origin HEAD:refs/heads/${{ env.RELEASE_BRANCH }} --force
- name: Create PR and merge immediately
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_TITLE="chore: update ${{ env.PACKAGE_NAME }} to ${{ env.VERSION }}"
PR_BODY="Automated version bump to ${{ env.VERSION }} after release publication.\n\nOld version was ${{ env.OLD_VERSION }}.\n\nMerging immediately via workflow."
# Ensure label exists (optional, for visibility only now)
gh label create "gem-release" --color "0E8A16" --description "Gem version bump PR" --force || true
# Check for existing open PR on this branch
EXISTING_PR=$(gh pr list --head "${{ env.RELEASE_BRANCH }}" --state open --json number --jq '.[0].number // ""')
if [ -n "$EXISTING_PR" ]; then
echo "Updating existing PR #$EXISTING_PR"
gh pr edit "$EXISTING_PR" \
--title "$PR_TITLE" \
--body "$PR_BODY"
gh pr reopen "$EXISTING_PR" || true
PR_NUMBER="$EXISTING_PR"
else
echo "Creating new PR"
PR_URL=$(gh pr create \
--base main \
--head "${{ env.RELEASE_BRANCH }}" \
--title "$PR_TITLE" \
--body "$PR_BODY" \
--label "gem-release")
PR_NUMBER=$(echo "$PR_URL" | grep -o '[0-9]\+')
fi
# Immediately merge (squash is clean for version bumps; change to --merge or --rebase if preferred)
echo "Merging PR #$PR_NUMBER immediately"
gh pr merge "$PR_NUMBER" --squash --delete-branch || {
echo "Immediate merge failed (likely due to checks/protection). Falling back to enable auto-merge."
gh pr merge "$PR_NUMBER" --auto --squash --delete-branch || echo "Auto-merge enable also failed."
}
- name: Trim old package versions
if: env.IS_TEST_MODE != 'true'
uses: actions/delete-package-versions@v5
with:
package-name: ${{ env.PACKAGE_NAME }}
package-type: rubygems
min-versions-to-keep: 5
token: ${{ secrets.GEM_PUSH_TOKEN }}