diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..fdf1951 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,86 @@ +name: Publish to RubyGems + +on: + workflow_call: + inputs: + version: + required: true + type: string + +jobs: + publish: + if: >- + github.repository == 'htmlcsstoimage/ruby-client' && + github.event_name == 'push' && github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + concurrency: rubygems-${{ inputs.version }} + permissions: + contents: write + id-token: write + env: + VERSION: ${{ inputs.version }} + TAG: v${{ inputs.version }} + GEM_NAME: htmlcsstoimage-api + + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + ref: ${{ github.sha }} + fetch-depth: 0 + - uses: ruby/setup-ruby@78c62f2208e480ce3d5ae4dfd9b9e181acf48b63 # v1 + with: + ruby-version: '3.3' + + - name: Check the tag and RubyGems version + id: check + run: | + set -euo pipefail + git check-ref-format "refs/tags/$TAG" + tagged=false + if git show-ref --verify --quiet "refs/tags/$TAG"; then + if [[ "$(git rev-parse "refs/tags/$TAG^{commit}")" != "$GITHUB_SHA" ]]; then + echo "::error::$TAG already points to a different commit." + exit 1 + fi + tagged=true + fi + status=$(curl --silent --show-error --retry 3 --output "$RUNNER_TEMP/gem.json" \ + --write-out '%{http_code}' "https://rubygems.org/api/v2/rubygems/$GEM_NAME/versions/$VERSION.json") + case "$status" in + 404) echo "push_gem=true" >> "$GITHUB_OUTPUT" ;; + 200) + if [[ "$tagged" != true ]] || ! jq -e '.yanked == false' "$RUNNER_TEMP/gem.json" > /dev/null; then + echo "::error::RubyGems already has $VERSION. Check its status and the matching Git tag." + exit 1 + fi + ;; + *) echo "::error::RubyGems version lookup returned HTTP $status."; exit 1 ;; + esac + echo "tagged=$tagged" >> "$GITHUB_OUTPUT" + + - name: Build the gem + if: steps.check.outputs.push_gem == 'true' + run: | + gem build htmlcsstoimage.gemspec + test -f "$GEM_NAME-$VERSION.gem" + + - name: Create the version tag + if: steps.check.outputs.tagged == 'false' + run: | + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git tag --annotate "$TAG" "$GITHUB_SHA" --message "Release $TAG" + git push origin "refs/tags/$TAG" + + - name: Authenticate with RubyGems + if: steps.check.outputs.push_gem == 'true' + uses: rubygems/configure-rubygems-credentials@c6ca7f4bac0e29c9bde8eeae0a0a986b27efa05f # v2.2.0 + - name: Publish the gem + if: steps.check.outputs.push_gem == 'true' + run: gem push --host https://rubygems.org "$GEM_NAME-$VERSION.gem" + + - name: Create GitHub Release + uses: softprops/action-gh-release@8a0cd651e5b2365bd640854a3fa5dfc70e4875f3 # v3 + with: + tag_name: ${{ env.TAG }} + generate_release_notes: true diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index f4bc70b..b1ff0c4 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -8,16 +8,48 @@ on: jobs: test: - runs-on: ubuntu-latest + permissions: + contents: read + outputs: + release_version: ${{ steps.version.outputs.version }} steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + fetch-depth: 0 - name: Set up Ruby - uses: ruby/setup-ruby@v1 + uses: ruby/setup-ruby@78c62f2208e480ce3d5ae4dfd9b9e181acf48b63 # v1 with: ruby-version: '3.3' - name: Install dependencies run: bundle install - name: Run tests run: bundle exec rake + - name: Check for a version bump + id: version + if: >- + github.repository == 'htmlcsstoimage/ruby-client' && + github.event_name == 'push' && github.ref == 'refs/heads/main' + env: + BEFORE_SHA: ${{ github.event.before }} + run: | + set -euo pipefail + version_file=lib/htmlcsstoimage/version.rb + version=$(ruby -r "./$version_file" -e 'print HTMLCSSToImage::VERSION') + if [[ "$BEFORE_SHA" != "0000000000000000000000000000000000000000" ]]; then + git show "$BEFORE_SHA:$version_file" > "$RUNNER_TEMP/previous_version.rb" + previous=$(ruby -r "$RUNNER_TEMP/previous_version.rb" -e 'print HTMLCSSToImage::VERSION') + if [[ "$previous" == "$version" ]]; then exit 0; fi + fi + echo "version=$version" >> "$GITHUB_OUTPUT" + + publish: + needs: test + if: needs.test.outputs.release_version != '' + permissions: + contents: write + id-token: write + uses: ./.github/workflows/publish.yml + with: + version: ${{ needs.test.outputs.release_version }} diff --git a/README.md b/README.md index ecc3676..5432cf1 100644 --- a/README.md +++ b/README.md @@ -368,7 +368,11 @@ and, for URL screenshots, `headers`, `additional_header_origins`, After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). +To install the gem locally, run `bundle exec rake install`. + +To release, bump `lib/htmlcsstoimage/version.rb`, run `bundle install` to update `Gemfile.lock`, and merge both changes into `main`. After the tests pass, GitHub Actions tags the tested commit as `v`, publishes the gem to RubyGems.org, and creates a GitHub Release. If a step fails, rerun the original workflow for that commit. + +The RubyGems trusted publisher for `htmlcsstoimage-api` uses the GitHub repository `htmlcsstoimage/ruby-client` and workflow filename `publish.yml`. Leave Environment, Workflow Repository Owner, and Workflow Repository Name blank. [RubyGems trusted publishing guide](https://guides.rubygems.org/trusted-publishing/). To generate the Yard docs, run `yard doc -o docs` and commit the changes. diff --git a/docs/file.README.html b/docs/file.README.html index 0d28223..82b58d2 100644 --- a/docs/file.README.html +++ b/docs/file.README.html @@ -330,7 +330,9 @@

Available parameters

block_consent_banners.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

-

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

+

To install the gem locally, run bundle exec rake install.

+

To release, bump lib/htmlcsstoimage/version.rb, run bundle install to update Gemfile.lock, and merge both changes into main. After the tests pass, GitHub Actions tags the tested commit as v<version>, publishes the gem to RubyGems.org, and creates a GitHub Release. If a step fails, rerun the original workflow for that commit.

+

The RubyGems trusted publisher for htmlcsstoimage-api uses the GitHub repository htmlcsstoimage/ruby-client and workflow filename publish.yml. Leave Environment, Workflow Repository Owner, and Workflow Repository Name blank. RubyGems trusted publishing guide.

To generate the Yard docs, run yard doc -o docs and commit the changes.

Support

For help with the API, you can also contact support@htmlcsstoimage.com.

diff --git a/docs/index.html b/docs/index.html index 400627e..83d8657 100644 --- a/docs/index.html +++ b/docs/index.html @@ -330,7 +330,9 @@

Available parameters

block_consent_banners.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

-

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

+

To install the gem locally, run bundle exec rake install.

+

To release, bump lib/htmlcsstoimage/version.rb, run bundle install to update Gemfile.lock, and merge both changes into main. After the tests pass, GitHub Actions tags the tested commit as v<version>, publishes the gem to RubyGems.org, and creates a GitHub Release. If a step fails, rerun the original workflow for that commit.

+

The RubyGems trusted publisher for htmlcsstoimage-api uses the GitHub repository htmlcsstoimage/ruby-client and workflow filename publish.yml. Leave Environment, Workflow Repository Owner, and Workflow Repository Name blank. RubyGems trusted publishing guide.

To generate the Yard docs, run yard doc -o docs and commit the changes.

Support

For help with the API, you can also contact support@htmlcsstoimage.com.