|
1 | 1 | name: gem |
2 | 2 |
|
3 | 3 | on: |
4 | | - release: |
5 | | - types: [published] |
| 4 | + push: |
| 5 | + branches: main |
| 6 | + |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + version_increment: |
| 10 | + description: 'Version to increment' |
| 11 | + required: true |
| 12 | + default: 'patch' |
| 13 | + options: |
| 14 | + - major |
| 15 | + - minor |
| 16 | + - patch |
6 | 17 |
|
7 | 18 | jobs: |
8 | 19 | build: |
9 | 20 | name: Build and publish |
| 21 | + |
| 22 | + permissions: |
| 23 | + contents: write |
| 24 | + id-token: write |
| 25 | + packages: write |
| 26 | + pages: write |
| 27 | + |
10 | 28 | runs-on: ubuntu-latest |
11 | 29 |
|
12 | 30 | steps: |
|
21 | 39 | bundle install |
22 | 40 | gem build *.gemspec |
23 | 41 |
|
| 42 | + - name: Update version |
| 43 | + env: |
| 44 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + run: | |
| 46 | + version_increment="${{ github.event.inputs.version_increment }}" |
| 47 | + version_increment="${version_increment:-patch}" |
| 48 | +
|
| 49 | + if [[ "$version_increment" == "patch" ]]; then |
| 50 | + commit_message=$(git log -1 --pretty=%B) |
| 51 | + if [[ "$commit_message" =~ Version-increment:\ (major|minor|patch) ]]; then |
| 52 | + version_increment="${BASH_REMATCH[1]}" |
| 53 | + fi |
| 54 | + fi |
| 55 | +
|
| 56 | + version=$(grep -E "Version = \[([0-9]+), ([0-9]+), ([0-9]+)\]" *.gemspec) |
| 57 | + major=$(echo "$version" | grep -Eo '\[[0-9]+' | tr -d '[') |
| 58 | + minor=$(echo "$version" | grep -Eo ', [0-9]+,' | tr -d ', ') |
| 59 | + patch=$(echo "$version" | grep -Eo ', [0-9]+\]' | tr -d ', ]') |
| 60 | +
|
| 61 | + case $version_increment in |
| 62 | + major) |
| 63 | + new_major=$((major + 1)) |
| 64 | + new_minor=0 |
| 65 | + new_patch=0 |
| 66 | + ;; |
| 67 | + minor) |
| 68 | + new_major=$((major)) |
| 69 | + new_minor=$((minor + 1)) |
| 70 | + new_patch=0 |
| 71 | + ;; |
| 72 | + patch) |
| 73 | + new_major=$((major)) |
| 74 | + new_minor=$((minor)) |
| 75 | + new_patch=$((patch + 1)) |
| 76 | + ;; |
| 77 | + esac |
| 78 | +
|
| 79 | + new_version="Version = [$new_major, $new_minor, $new_patch]" |
| 80 | + sed -i "s/^Version = \[.*\]/${new_version}/" *.gemspec |
| 81 | +
|
| 82 | + echo "version=$major.$minor.$patch" >> $GITHUB_ENV |
| 83 | + echo "new_version=$new_major.$new_minor.$new_patch" >> $GITHUB_ENV |
| 84 | +
|
| 85 | + git config --global user.email "rcvalle@users.noreply.github.com" |
| 86 | + git config --global user.name "Ramon de C Valle" |
| 87 | + git add *.gemspec |
| 88 | + git commit -m "Update version to $new_major.$new_minor.$new_patch" |
| 89 | + git push origin main |
| 90 | +
|
| 91 | + - name: Create new release |
| 92 | + env: |
| 93 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 94 | + run: | |
| 95 | + changelog=$(mktemp) |
| 96 | + if [[ "${{ env.version }}" != "0.0.0" ]]; then |
| 97 | + git fetch --all -t |
| 98 | + git log "v${{ env.version }}"..HEAD --pretty='format:* %s' > "$changelog" |
| 99 | + else |
| 100 | + git log --pretty='format:* %s' > "$changelog" |
| 101 | + fi |
| 102 | + gh release create "v${{ env.new_version }}" -F "$changelog" -t "v${{ env.new_version }}" |
| 103 | + rm "$changelog" |
| 104 | +
|
24 | 105 | - name: Publish to GitHub Packages |
25 | 106 | env: |
26 | 107 | GEM_HOST_API_KEY: "Bearer ${{secrets.GITHUB_TOKEN}}" |
|
0 commit comments