|
| 1 | +name: Release gem |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + tags: |
| 8 | + - 'v*' |
| 9 | + paths: |
| 10 | + - 'lib/cloudstack-cli/version.rb' |
| 11 | + workflow_dispatch: |
| 12 | + inputs: |
| 13 | + tag: |
| 14 | + description: 'Existing git tag to release, for example v1.6.12' |
| 15 | + required: true |
| 16 | + type: string |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: write |
| 20 | + |
| 21 | +concurrency: |
| 22 | + group: release-${{ github.event.inputs.tag || github.ref_name || github.sha }} |
| 23 | + cancel-in-progress: false |
| 24 | + |
| 25 | +jobs: |
| 26 | + release: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Check out repository |
| 31 | + uses: actions/checkout@v4 |
| 32 | + with: |
| 33 | + fetch-depth: 0 |
| 34 | + |
| 35 | + - name: Resolve release metadata |
| 36 | + id: release |
| 37 | + run: | |
| 38 | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then |
| 39 | + RELEASE_TAG='${{ github.event.inputs.tag }}' |
| 40 | + git fetch --force --tags origin |
| 41 | + git checkout "$RELEASE_TAG" |
| 42 | + VERSION="$(ruby -e 'require "./lib/cloudstack-cli/version"; print CloudstackCli::VERSION')" |
| 43 | + elif [ "$GITHUB_REF_TYPE" = "tag" ]; then |
| 44 | + RELEASE_TAG="$GITHUB_REF_NAME" |
| 45 | + VERSION="$(ruby -e 'require "./lib/cloudstack-cli/version"; print CloudstackCli::VERSION')" |
| 46 | + else |
| 47 | + VERSION="$(ruby -e 'require "./lib/cloudstack-cli/version"; print CloudstackCli::VERSION')" |
| 48 | + RELEASE_TAG="v$VERSION" |
| 49 | + fi |
| 50 | +
|
| 51 | + echo "release_tag=$RELEASE_TAG" >> "$GITHUB_OUTPUT" |
| 52 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 53 | +
|
| 54 | + - name: Set up Ruby |
| 55 | + uses: ruby/setup-ruby@v1 |
| 56 | + with: |
| 57 | + bundler-cache: true |
| 58 | + |
| 59 | + - name: Verify tag matches gem version |
| 60 | + run: | |
| 61 | + VERSION='${{ steps.release.outputs.version }}' |
| 62 | + TAG_VERSION='${{ steps.release.outputs.release_tag }}' |
| 63 | + TAG_VERSION="${TAG_VERSION#v}" |
| 64 | +
|
| 65 | + if [ "$TAG_VERSION" != "$VERSION" ]; then |
| 66 | + echo "Tag version ($TAG_VERSION) does not match gem version ($VERSION)." |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | +
|
| 70 | + - name: Create release tag from main |
| 71 | + if: github.ref_type == 'branch' |
| 72 | + run: | |
| 73 | + RELEASE_TAG='${{ steps.release.outputs.release_tag }}' |
| 74 | +
|
| 75 | + git fetch --force --tags origin |
| 76 | +
|
| 77 | + if git rev-parse "$RELEASE_TAG" >/dev/null 2>&1; then |
| 78 | + TAG_SHA="$(git rev-list -n 1 "$RELEASE_TAG")" |
| 79 | +
|
| 80 | + if [ "$TAG_SHA" != "$GITHUB_SHA" ]; then |
| 81 | + echo "Tag $RELEASE_TAG already exists on $TAG_SHA, expected $GITHUB_SHA." |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + else |
| 85 | + git config user.name 'github-actions[bot]' |
| 86 | + git config user.email '41898282+github-actions[bot]@users.noreply.github.com' |
| 87 | + git tag -a "$RELEASE_TAG" -m "Release $RELEASE_TAG" |
| 88 | + git push origin "$RELEASE_TAG" |
| 89 | + fi |
| 90 | +
|
| 91 | + git checkout "$RELEASE_TAG" |
| 92 | +
|
| 93 | + - name: Run test suite |
| 94 | + run: bundle exec rake test |
| 95 | + |
| 96 | + - name: Build gem |
| 97 | + run: gem build cloudstack-cli.gemspec |
| 98 | + |
| 99 | + - name: Check whether version already exists on RubyGems |
| 100 | + id: rubygems |
| 101 | + env: |
| 102 | + RELEASE_VERSION: ${{ steps.release.outputs.version }} |
| 103 | + run: | |
| 104 | + if ruby -rjson -ropen-uri -e 'versions = JSON.parse(URI.open("https://rubygems.org/api/v1/versions/cloudstack-cli.json", &:read)); exit(versions.any? { |item| item["number"] == ENV.fetch("RELEASE_VERSION") } ? 0 : 1)'; then |
| 105 | + echo "Version $RELEASE_VERSION already exists on RubyGems." |
| 106 | + echo 'exists=true' >> "$GITHUB_OUTPUT" |
| 107 | + else |
| 108 | + echo 'exists=false' >> "$GITHUB_OUTPUT" |
| 109 | + fi |
| 110 | +
|
| 111 | + - name: Publish to RubyGems |
| 112 | + if: steps.rubygems.outputs.exists != 'true' |
| 113 | + env: |
| 114 | + GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }} |
| 115 | + run: | |
| 116 | + if [ -z "$GEM_HOST_API_KEY" ]; then |
| 117 | + echo 'Missing RUBYGEMS_API_KEY secret.' |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | +
|
| 121 | + gem push "cloudstack-cli-${{ steps.release.outputs.version }}.gem" |
0 commit comments