From 8f8ec581d50dca8a8216c3989cad260ce6641cbf Mon Sep 17 00:00:00 2001 From: david ruiz Date: Mon, 9 Nov 2026 15:43:25 +0100 Subject: [PATCH 1/4] Release worflow security enhance --- .github/scripts/check_lockfile_checksums.rb | 50 ++++ .github/scripts/update-lockfiles.sh | 43 ++++ .github/scripts/verify_gem.rb | 69 ++++++ .github/workflows/build-master.yml | 30 ++- .github/workflows/build-pull-request.yml | 30 ++- .github/workflows/build-release.yml | 254 +++++++++++++++++--- .github/workflows/codeql-analysis.yml | 17 +- .gitignore | 1 - Gemfile.lock | 146 +++++++++++ checkout_sdk.gemspec | 5 +- gemfiles/ruby_2_7.gemfile | 7 + gemfiles/ruby_2_7.gemfile.lock | 98 ++++++++ 12 files changed, 704 insertions(+), 46 deletions(-) create mode 100644 .github/scripts/check_lockfile_checksums.rb create mode 100755 .github/scripts/update-lockfiles.sh create mode 100644 .github/scripts/verify_gem.rb create mode 100644 Gemfile.lock create mode 100644 gemfiles/ruby_2_7.gemfile create mode 100644 gemfiles/ruby_2_7.gemfile.lock diff --git a/.github/scripts/check_lockfile_checksums.rb b/.github/scripts/check_lockfile_checksums.rb new file mode 100644 index 0000000..bf45fcc --- /dev/null +++ b/.github/scripts/check_lockfile_checksums.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +# Fails unless the lockfile only uses rubygems.org and the local gemspec, and every gem from +# rubygems.org has a sha256 in its CHECKSUMS section. +# Bundler verifies downloads against CHECKSUMS but installs without complaint when the section or an +# entry is missing, so CI checks the coverage first. Standard library only. +# Usage: ruby .github/scripts/check_lockfile_checksums.rb Gemfile.lock + +def fail_check(message) + warn "check_lockfile_checksums: #{message}. Regenerate with .github/scripts/update-lockfiles.sh" + exit 1 +end + +fail_check('usage: check_lockfile_checksums.rb ') unless ARGV.size == 1 +path = ARGV.first +fail_check("#{path} is not a regular file") unless File.file?(path) && !File.symlink?(path) + +sections = Hash.new { |hash, key| hash[key] = [] } +current = nil +File.foreach(path, chomp: true) do |line| + line = line.delete_suffix("\r") + if line.match?(/\A[A-Z][A-Z ]*\z/) + current = line + elsif current + sections[current] << line + end +end + +foreign = sections.keys & ['GIT', 'PLUGIN SOURCE', 'GEM SOURCE'] +fail_check("#{path} has #{foreign.join(' and ')} sources; only rubygems.org and the local gemspec are allowed") unless foreign.empty? +remotes = (sections['GEM'] + sections['PATH']).filter_map { |line| line[/\A remote: (.+)\z/, 1] } +fail_check("#{path} has unexpected remotes: #{remotes.inspect}") unless remotes.sort == ['.', 'https://rubygems.org/'] + +gems = sections['GEM'].filter_map { |line| line[/\A {4}(\S+ \([^)]+\))\z/, 1] } +fail_check("#{path} lists no gems") if gems.empty? +fail_check("#{path} has no CHECKSUMS section") unless sections.key?('CHECKSUMS') + +checksums = sections['CHECKSUMS'].each_with_object({}) do |line, found| + match = line.match(/\A {2}(\S+ \([^)]+\))(?: sha256=([0-9a-f]{64}))?\z/) + found[match[1]] = match[2] if match +end + +missing = gems.reject { |gem| checksums[gem] } +fail_check("no sha256 for #{missing.join(', ')}") unless missing.empty? + +path_gems = sections['PATH'].filter_map { |line| line[/\A {4}(\S+ \([^)]+\))\z/, 1] } +unknown = checksums.keys - gems - path_gems +fail_check("CHECKSUMS names gems that are not locked: #{unknown.join(', ')}") unless unknown.empty? + +puts "check_lockfile_checksums: #{gems.size} gems in #{path}, all with sha256" diff --git a/.github/scripts/update-lockfiles.sh b/.github/scripts/update-lockfiles.sh new file mode 100755 index 0000000..5654038 --- /dev/null +++ b/.github/scripts/update-lockfiles.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# Regenerates the committed lockfiles in linux/amd64 containers, so they resolve for the CI +# runners and not for the local machine. Needs Docker. +# +# Gemfile.lock Ruby 3.1, Bundler 2.6.9, with gem checksums. +# Ruby 3.1 and 3.3 CI legs and the release workflow. +# gemfiles/ruby_2_7.gemfile.lock Ruby 2.7, Bundler 2.4.22 (the last Bundler for Ruby 2.7, +# which cannot record checksums). Ruby 2.7 CI leg only. +# +# CI installs with frozen lockfiles and fails when they are missing or out of date, so run this +# after changing the Gemfile, the gemspec or lib/checkout_sdk/version.rb (the lock records the +# gem's own version). Without arguments it keeps every locked version that still satisfies the +# Gemfile; `--update` re-resolves all gems to the newest allowed versions. +# +# Usage: .github/scripts/update-lockfiles.sh [--update] +set -euo pipefail + +cd "$(dirname "$0")/../.." + +update='' +case "${1:-}" in + '') ;; + --update) update='--update' ;; + *) echo "usage: $0 [--update]" >&2; exit 2 ;; +esac + +# ruby_version bundler_version gemfile extra_lock_args +lock() { + docker run --rm --platform linux/amd64 \ + -v "$PWD:/src" -w /src \ + -e BUNDLE_GEMFILE="$3" -e UPDATE="$update" -e BUNDLER_VERSION="$2" -e EXTRA="$4" \ + "ruby:$1" bash -euc ' + git config --global --add safe.directory /src + gem install bundler -v "$BUNDLER_VERSION" --no-document --silent + # shellcheck disable=SC2086 + bundle "_${BUNDLER_VERSION}_" lock $UPDATE --add-platform ruby x86_64-linux $EXTRA + ' +} + +lock 3.1 2.6.9 Gemfile --add-checksums +lock 2.7 2.4.22 gemfiles/ruby_2_7.gemfile '' + +echo 'Lockfiles updated. Review the diff before committing.' diff --git a/.github/scripts/verify_gem.rb b/.github/scripts/verify_gem.rb new file mode 100644 index 0000000..d6e3e9a --- /dev/null +++ b/.github/scripts/verify_gem.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +# Checks the gem built by the release workflow before it is uploaded for publishing. +# Standard library only. Usage: ruby .github/scripts/verify_gem.rb +# +# Fails unless the working directory holds exactly checkout_sdk-.gem, and that gem: +# - is named checkout_sdk and carries ; +# - has no native extensions and no executables (nothing runs when merchants install it); +# - packages only regular files (no symlinks or links), only under lib/, sig/ and the known +# top-level files (no lockfiles, CI files or specs). + +require 'rubygems/package' +require 'zlib' + +VERSION_PATTERN = /\A\d+\.\d+\.\d+(\.(alpha|beta|rc|pre)\.?\d+)?\z/.freeze +ALLOWED_TOP_LEVEL = %w[ + .overcommit.yml .rspec .rubocop.yml .ruby-version CODE_OF_CONDUCT.md Gemfile LICENSE.md README.md Rakefile +].freeze +ALLOWED_DIRECTORIES = %r{\A(?:lib|sig)/}.freeze + +# Names of the entries in the gem's data archive that are not regular files. +def non_regular_entries(gem_path) + found = [] + File.open(gem_path, 'rb') do |io| + Gem::Package::TarReader.new(io).each do |outer| + next unless outer.full_name == 'data.tar.gz' + + Zlib::GzipReader.wrap(outer) do |gzip| + Gem::Package::TarReader.new(gzip).each { |entry| found << entry.full_name unless entry.file? } + end + end + end + found +end + +def fail_check(message) + warn "verify_gem: #{message}" + exit 1 +end + +fail_check('usage: verify_gem.rb ') unless ARGV.size == 1 +version = ARGV.first +fail_check('the version is not a release or pre-release version') unless version.match?(VERSION_PATTERN) + +expected = "checkout_sdk-#{version}.gem" +built = Dir.children(Dir.pwd).select { |name| name.end_with?('.gem') } +fail_check("expected only #{expected}, found #{built.sort.inspect}") unless built == [expected] + +package = Gem::Package.new(expected) +spec = package.spec +fail_check("gem name is #{spec.name}") unless spec.name == 'checkout_sdk' +fail_check("gem version is #{spec.version}") unless spec.version.to_s == version +fail_check('the gem declares native extensions') unless spec.extensions.empty? +fail_check('the gem declares executables') unless spec.executables.empty? + +contents = package.contents +fail_check('the gem file list does not match its contents') unless contents.sort == spec.files.sort +fail_check('lib/checkout_sdk/version.rb is missing') unless contents.include?('lib/checkout_sdk/version.rb') + +links = non_regular_entries(expected) +fail_check("entries that are not regular files: #{links.sort.inspect}") unless links.empty? + +unsafe = contents.select { |path| path.start_with?('/') || path.split('/').include?('..') } +fail_check("unsafe paths in the gem: #{unsafe.sort.inspect}") unless unsafe.empty? + +unexpected = contents.reject { |path| ALLOWED_DIRECTORIES.match?(path) || ALLOWED_TOP_LEVEL.include?(path) } +fail_check("unexpected files in the gem: #{unexpected.sort.inspect}") unless unexpected.empty? + +puts "verify_gem: #{expected} OK (#{contents.size} files)" diff --git a/.github/workflows/build-master.yml b/.github/workflows/build-master.yml index 5352f04..2ab9397 100644 --- a/.github/workflows/build-master.yml +++ b/.github/workflows/build-master.yml @@ -5,6 +5,9 @@ on: branches: - "master" +permissions: + contents: read + jobs: build: if: "!contains(github.event.commits[0].message, 'Release')" @@ -20,13 +23,34 @@ jobs: '3.3', ] name: Ruby ${{ matrix.ruby-version }} + env: + # Ruby 2.7 cannot install every gem in Gemfile.lock (resolved for Ruby 3.1): it has its own lock. + BUNDLE_GEMFILE: ${{ matrix.ruby-version == '2.7' && 'gemfiles/ruby_2_7.gemfile' || 'Gemfile' }} + BUNDLE_FROZEN: 'true' steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + # setup-ruby would generate a fresh lock if none were committed: refuse instead. + - id: require-lockfile + run: | + if [ ! -f "${BUNDLE_GEMFILE}.lock" ]; then + echo "::error::${BUNDLE_GEMFILE}.lock is missing. Run .github/scripts/update-lockfiles.sh and commit it." + exit 1 + fi - id: setup-ruby-and-bundle - uses: ruby/setup-ruby@v1 + uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0 with: ruby-version: ${{ matrix.ruby-version }} - bundler-cache: true # runs 'bundle install' and caches installed gems automatically + # No gem cache: a restored cache is not checked against the lock's checksums. + bundler-cache: false + # The Ruby 2.7 lock cannot carry checksums (Bundler 2.4): only legs that install from + # Gemfile.lock check them. + - id: check-lockfile-checksums + if: env.BUNDLE_GEMFILE == 'Gemfile' + run: ruby .github/scripts/check_lockfile_checksums.rb Gemfile.lock + - id: install-dependencies + run: bundle install - id: begin-rubocop-scan run: bundle exec rubocop lib - id: build-and-analyse diff --git a/.github/workflows/build-pull-request.yml b/.github/workflows/build-pull-request.yml index 18b6531..e2e36fd 100644 --- a/.github/workflows/build-pull-request.yml +++ b/.github/workflows/build-pull-request.yml @@ -6,6 +6,9 @@ on: - "master" - "feature/**" +permissions: + contents: read + jobs: build: runs-on: ubuntu-latest @@ -20,13 +23,34 @@ jobs: '3.3', ] name: Ruby ${{ matrix.ruby-version }} + env: + # Ruby 2.7 cannot install every gem in Gemfile.lock (resolved for Ruby 3.1): it has its own lock. + BUNDLE_GEMFILE: ${{ matrix.ruby-version == '2.7' && 'gemfiles/ruby_2_7.gemfile' || 'Gemfile' }} + BUNDLE_FROZEN: 'true' steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + # setup-ruby would generate a fresh lock if none were committed: refuse instead. + - id: require-lockfile + run: | + if [ ! -f "${BUNDLE_GEMFILE}.lock" ]; then + echo "::error::${BUNDLE_GEMFILE}.lock is missing. Run .github/scripts/update-lockfiles.sh and commit it." + exit 1 + fi - id: setup-ruby-and-bundle - uses: ruby/setup-ruby@v1 + uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0 with: ruby-version: ${{ matrix.ruby-version }} - bundler-cache: true # runs 'bundle install' and caches installed gems automatically + # No gem cache: a restored cache is not checked against the lock's checksums. + bundler-cache: false + # The Ruby 2.7 lock cannot carry checksums (Bundler 2.4): only legs that install from + # Gemfile.lock check them. + - id: check-lockfile-checksums + if: env.BUNDLE_GEMFILE == 'Gemfile' + run: ruby .github/scripts/check_lockfile_checksums.rb Gemfile.lock + - id: install-dependencies + run: bundle install - id: begin-rubocop-scan run: bundle exec rubocop lib - id: build-and-analyse diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index abce6e3..759915f 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -1,26 +1,128 @@ name: build-release + +# Publishes the gem when lib/checkout_sdk/version.rb changes on master. +# +# Manual run (workflow_dispatch, master only) is the dry run of the whole pipeline: it publishes +# the pre-release version given as input (for example 2.9.0.rc.1, never installed unless asked +# for) and creates the GitHub release as a pre-release. +# +# Jobs: build (no secrets) -> test (sandbox secrets) -> publish (RubyGems trusted publishing, +# environment "rubygems") -> release (gh release create). Only publish can obtain a RubyGems key, +# and the key only exists inside its publish step. on: push: branches: - master paths: - lib/checkout_sdk/version.rb + workflow_dispatch: + inputs: + prerelease-version: + description: 'Pre-release version to publish, for example 2.9.0.rc.1' + required: true + type: string + +permissions: + contents: read + +concurrency: + group: build-release + cancel-in-progress: false jobs: - deploy: + build: if: github.ref == 'refs/heads/master' runs-on: ubuntu-latest - # The repository's default GITHUB_TOKEN is read-only. create-release needs - # write access to contents to create the version tag and GitHub release. - permissions: - contents: write + outputs: + version: ${{ steps.version.outputs.version }} + prerelease: ${{ steps.version.outputs.prerelease }} + sha256: ${{ steps.build-gem.outputs.sha256 }} steps: - - uses: actions/checkout@v3 - - id: setup-ruby-and-bundle - uses: ruby/setup-ruby@v1 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - id: setup-ruby + uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0 with: - ruby-version: 3.1 - bundler-cache: true # runs 'bundle install' and caches installed gems automatically + ruby-version: '3.1' + bundler: none + - id: version + env: + EVENT_NAME: ${{ github.event_name }} + PRERELEASE_VERSION: ${{ inputs.prerelease-version }} + run: | + set -euo pipefail + release_pattern='^[0-9]+\.[0-9]+\.[0-9]+$' + prerelease_pattern='^[0-9]+\.[0-9]+\.[0-9]+\.(alpha|beta|rc|pre)\.?[0-9]+$' + read_version() { ruby -e 'load "lib/checkout_sdk/version.rb"; print CheckoutSdk::VERSION'; } + if [ "$EVENT_NAME" = 'workflow_dispatch' ]; then + version="$PRERELEASE_VERSION" + if ! [[ "$version" =~ $prerelease_pattern ]]; then + echo '::error::A manual run only publishes a pre-release version such as 2.9.0.rc.1' + exit 1 + fi + sed -i -E "s/^ VERSION = '[^']*'$/ VERSION = '${version}'/" lib/checkout_sdk/version.rb + else + version="$(read_version)" + if ! [[ "$version" =~ $release_pattern || "$version" =~ $prerelease_pattern ]]; then + echo '::error::lib/checkout_sdk/version.rb does not hold a valid version' + exit 1 + fi + fi + if [ "$(read_version)" != "$version" ]; then + echo '::error::lib/checkout_sdk/version.rb does not match the version to publish' + exit 1 + fi + prerelease=false + if [[ "$version" =~ $prerelease_pattern ]]; then prerelease=true; fi + echo "Releasing $version (pre-release: $prerelease)" + echo "version=$version" >> "$GITHUB_OUTPUT" + echo "prerelease=$prerelease" >> "$GITHUB_OUTPUT" + - id: build-gem + env: + VERSION: ${{ steps.version.outputs.version }} + run: | + set -euo pipefail + gem build checkout_sdk.gemspec --output "checkout_sdk-${VERSION}.gem" + ruby .github/scripts/verify_gem.rb "$VERSION" + sha256="$(sha256sum "checkout_sdk-${VERSION}.gem" | cut -d ' ' -f 1)" + echo "sha256 of checkout_sdk-${VERSION}.gem: $sha256" + echo "sha256=$sha256" >> "$GITHUB_OUTPUT" + - id: upload-gem + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: gem + path: checkout_sdk-${{ steps.version.outputs.version }}.gem + if-no-files-found: error + retention-days: 7 + + test: + needs: build + runs-on: ubuntu-latest + env: + BUNDLE_FROZEN: 'true' + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - id: require-lockfile + run: | + if [ ! -f Gemfile.lock ]; then + echo '::error::Gemfile.lock is missing. Run .github/scripts/update-lockfiles.sh and commit it.' + exit 1 + fi + - id: setup-ruby + uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0 + with: + ruby-version: '3.1' + bundler-cache: false + # Frozen: fails when Gemfile.lock is out of date with the Gemfile, the gemspec or the + # version, and when a downloaded gem does not match its checksum in the lock. No cache: + # every gem is downloaded and checked. + - id: check-lockfile-checksums + run: ruby .github/scripts/check_lockfile_checksums.rb Gemfile.lock + - id: install-dependencies + run: bundle install - id: build-and-analyse env: CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }} @@ -38,27 +140,119 @@ jobs: CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET }} CHECKOUT_MERCHANT_SUBDOMAIN: ${{ secrets.IT_CHECKOUT_MERCHANT_SUBDOMAIN }} run: bundle exec rspec spec - - id: read-version - run: echo "CURRENT_VERSION=$( grep "VERSION" lib/checkout_sdk/version.rb | awk '{ print $3 }' | tr -d "'")" >> $GITHUB_ENV - - id: print-version - run: echo "Releasing $CURRENT_VERSION" + + publish: + needs: [build, test] + # The image's own Ruby runs the publish step: no downloaded interpreter or action other than + # download-artifact runs in this job. + runs-on: ubuntu-24.04 + # Protected environment: deployment branch rule "master" and the RubyGems trusted publisher + # are both bound to it. + environment: rubygems + permissions: + id-token: write # OIDC token for RubyGems trusted publishing + steps: + # No checkout and no bundle install: nothing from the repository or from RubyGems runs + # next to the publishing key. The gem is the artifact built and checked by the build job, + # and it must have the sha256 the build job recorded. + - id: download-gem + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: gem + path: dist + # RubyGems trusted publishing: the job's OIDC token is exchanged for a short-lived key scoped + # to this gem. The key is held only in this step's memory and the child gem process + # environment: never written to disk or to $GITHUB_ENV. - id: publish-gem - run: | - mkdir -p $HOME/.gem - touch $HOME/.gem/credentials - chmod 0600 $HOME/.gem/credentials - printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials - gem build *.gemspec - gem push *.gem env: - GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}" + VERSION: ${{ needs.build.outputs.version }} + EXPECTED_SHA256: ${{ needs.build.outputs.sha256 }} + # Only Ruby's own default libraries load, in an emptied environment: env -i with an explicit + # allowlist (no RUBYOPT, RUBYLIB, GEMRC, LD_PRELOAD, SSL_* from earlier steps), an empty HOME + # (no ~/.gemrc), --disable-gems and an empty GEM_HOME/GEM_PATH (the image installs newer + # json/net-http gems that would otherwise shadow the default ones). The gem push child + # inherits this environment. + run: | + set -euo pipefail + empty_gem_dir="$(/usr/bin/mktemp -d)" + empty_home="$(/usr/bin/mktemp -d)" + /usr/bin/env -i \ + PATH=/usr/bin:/bin LANG=C.UTF-8 HOME="$empty_home" \ + GEM_HOME="$empty_gem_dir" GEM_PATH="$empty_gem_dir" EMPTY_GEM_DIR="$empty_gem_dir" \ + VERSION="$VERSION" EXPECTED_SHA256="$EXPECTED_SHA256" \ + ACTIONS_ID_TOKEN_REQUEST_URL="${ACTIONS_ID_TOKEN_REQUEST_URL:-}" \ + ACTIONS_ID_TOKEN_REQUEST_TOKEN="${ACTIONS_ID_TOKEN_REQUEST_TOKEN:-}" \ + /usr/bin/ruby --disable-gems <<'RUBY' + require 'digest' + require 'json' + require 'net/http' + require 'uri' + + def json_field(response, field) + JSON.parse(response.body).fetch(field) + rescue JSON::ParserError, KeyError, TypeError + abort "Unexpected response body (HTTP #{response.code}); not printed" + end + + version = ENV.fetch('VERSION') + unless version.match?(/\A\d+\.\d+\.\d+(\.(alpha|beta|rc|pre)\.?\d+)?\z/) + abort 'Unexpected version from the build job' + end + gem_file = "dist/checkout_sdk-#{version}.gem" + unless Dir.glob('dist/*', File::FNM_DOTMATCH).reject { |f| %w[. ..].include?(File.basename(f)) } == [gem_file] && + File.file?(gem_file) && !File.symlink?(gem_file) + abort 'The build artifact must hold exactly the gem to publish' + end + expected_sha256 = ENV.fetch('EXPECTED_SHA256') + abort 'Missing sha256 from the build job' unless expected_sha256.match?(/\A[0-9a-f]{64}\z/) + actual_sha256 = Digest::SHA256.file(gem_file).hexdigest + abort "The gem is not the one the build job produced (sha256 #{actual_sha256})" unless actual_sha256 == expected_sha256 + puts "sha256 of #{gem_file}: #{actual_sha256} (matches the build job)" + + if ENV['ACTIONS_ID_TOKEN_REQUEST_URL'].to_s.empty? || ENV['ACTIONS_ID_TOKEN_REQUEST_TOKEN'].to_s.empty? + abort 'No OIDC token available: the publish job needs permissions id-token: write' + end + id_uri = URI("#{ENV.fetch('ACTIONS_ID_TOKEN_REQUEST_URL')}&audience=rubygems.org") + id_request = Net::HTTP::Get.new(id_uri) + id_request['Authorization'] = "Bearer #{ENV.fetch('ACTIONS_ID_TOKEN_REQUEST_TOKEN')}" + id_response = Net::HTTP.start(id_uri.host, id_uri.port, use_ssl: true) { |http| http.request(id_request) } + abort "OIDC token request failed (HTTP #{id_response.code})" unless id_response.is_a?(Net::HTTPSuccess) + jwt = json_field(id_response, 'value') + puts "::add-mask::#{jwt}" + + exchange_response = Net::HTTP.post( + URI('https://rubygems.org/api/v1/oidc/trusted_publisher/exchange_token'), + JSON.generate(jwt: jwt), + 'Content-Type' => 'application/json', 'Accept' => 'application/json' + ) + unless exchange_response.is_a?(Net::HTTPSuccess) + abort "RubyGems trusted publisher exchange failed (HTTP #{exchange_response.code}). " \ + 'Check the trusted publisher of checkout_sdk on rubygems.org.' + end + api_key = json_field(exchange_response, 'rubygems_api_key') + puts "::add-mask::#{api_key}" + + gem_env = { + 'GEM_HOST_API_KEY' => api_key, 'GEM_HOME' => ENV.fetch('EMPTY_GEM_DIR'), 'GEM_PATH' => ENV.fetch('EMPTY_GEM_DIR'), + 'RUBYOPT' => nil, 'RUBYLIB' => nil + } + system(gem_env, '/usr/bin/gem', 'push', gem_file, '--host', 'https://rubygems.org', exception: true) + RUBY + + release: + needs: [build, publish] + runs-on: ubuntu-latest + permissions: + contents: write # create the version tag and the GitHub release + steps: - id: create-release - uses: actions/create-release@v1 env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ env.CURRENT_VERSION }} - release_name: ${{ env.CURRENT_VERSION }} - body: ${{ github.event.head_commit.message }} - draft: false - prerelease: false + GH_TOKEN: ${{ github.token }} + VERSION: ${{ needs.build.outputs.version }} + PRERELEASE: ${{ needs.build.outputs.prerelease }} + NOTES: ${{ github.event.head_commit.message }} + run: | + set -euo pipefail + args=(--repo "$GITHUB_REPOSITORY" --target "$GITHUB_SHA" --title "$VERSION" --notes "${NOTES:-Pre-release $VERSION}") + if [ "$PRERELEASE" = 'true' ]; then args+=(--prerelease); fi + gh release create "$VERSION" "${args[@]}" diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index fa9a732..406cb8f 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -20,14 +20,16 @@ on: schedule: - cron: '34 2 * * 0' +permissions: + contents: read + jobs: analyze: name: Analyze runs-on: ubuntu-latest permissions: - actions: read - contents: read - security-events: write + contents: read # check out the code + security-events: write # upload the analysis strategy: fail-fast: false @@ -38,13 +40,14 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: submodules: recursive + persist-credentials: false # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3 with: languages: ${{ matrix.language }} queries: security-and-quality @@ -56,7 +59,7 @@ jobs: # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs # queries: security-extended,security-and-quality - name: Autobuild - uses: github/codeql-action/autobuild@v2 + uses: github/codeql-action/autobuild@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3 - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3 diff --git a/.gitignore b/.gitignore index 6ae28b6..5c23c89 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ *.DS_Store -*.lock *.gem /vendor/ diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..4898164 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,146 @@ +PATH + remote: . + specs: + checkout_sdk (2.8.2) + csv (>= 3.0) + faraday (>= 2.0, < 3) + faraday-follow_redirects (~> 0.5) + faraday-multipart (~> 1.2) + mime-types (~> 3.0) + +GEM + remote: https://rubygems.org/ + specs: + ast (2.4.3) + childprocess (5.1.0) + logger (~> 1.5) + csv (3.3.7) + diff-lcs (1.6.2) + faraday (2.15.1) + faraday-net_http (>= 2.0, < 3.6) + json + logger + faraday-follow_redirects (0.5.0) + faraday (>= 1, < 3) + faraday-multipart (1.2.0) + multipart-post (~> 2.0) + faraday-net_http (3.5.0) + net-http (~> 0.5) + iniparse (1.5.0) + json (3.1.0) + language_server-protocol (3.17.0.6) + lint_roller (1.1.0) + logger (1.7.0) + mime-types (3.7.0) + logger + mime-types-data (~> 3.2025, >= 3.2025.0507) + mime-types-data (3.2026.1103) + multipart-post (2.4.1) + net-http (0.10.0) + net-protocol (>= 0.3.0) + uri (>= 0.11.1) + net-protocol (0.4.0) + timeout + overcommit (0.75.0) + childprocess (>= 5.0.0, < 6) + iniparse (~> 1.4) + rexml (>= 3.4.2) + parallel (1.28.0) + parser (3.3.13.0) + ast (~> 2.4.1) + racc + prism (1.9.0) + racc (1.8.1) + rainbow (3.1.1) + rake (13.4.2) + regexp_parser (2.13.1) + rexml (3.4.4) + rspec (3.13.2) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.7) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.6) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.9) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.8) + rubocop (1.92.2) + json (>= 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) + parallel (>= 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.11.3, < 3.0) + rubocop-ast (>= 1.49.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.50.0) + parser (>= 3.3.7.2) + prism (~> 1.7) + ruby-progressbar (1.13.0) + timeout (0.6.1) + unicode-display_width (3.3.0) + unicode-emoji (~> 4.3) + unicode-emoji (4.3.0) + uri (1.1.2) + +PLATFORMS + ruby + x86_64-linux + +DEPENDENCIES + checkout_sdk! + overcommit (~> 0.60) + rake (>= 13.0) + rspec (>= 3.0) + rubocop (~> 1.82) + +CHECKSUMS + ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383 + checkout_sdk (2.8.2) + childprocess (5.1.0) sha256=9a8d484be2fd4096a0e90a0cd3e449a05bc3aa33f8ac9e4d6dcef6ac1455b6ec + csv (3.3.7) sha256=15a3f16f6628da8664d455eb634e7f22031bcf53044923caf56975109e71b9f8 + diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962 + faraday (2.15.1) sha256=8708154deda739c5e86cc42b74960e23e8491fce0eba9e11639a6a34b1ac288f + faraday-follow_redirects (0.5.0) sha256=5cde93c894b30943a5d2b93c2fe9284216a6b756f7af406a1e55f211d97d10ad + faraday-multipart (1.2.0) sha256=7d89a949693714176f612323ca13746a2ded204031a6ba528adee788694ef757 + faraday-net_http (3.5.0) sha256=2e7094c34e38754ee048cc3df47376f8657005d9375f8473bce1c3b526a22e36 + iniparse (1.5.0) sha256=36a165e98d8a250b7631c4a7f9afba32af78f089970cd6446a39771189c761f1 + json (3.1.0) sha256=3ce7ccf8fde58960177a3a756c31f292a4c1e1dedee884547096a5b0604e3bd8 + language_server-protocol (3.17.0.6) sha256=5ef2c0c138f8267e1bc631d3328347d354f96724b0af22f2c79516120443b7f0 + lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87 + logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203 + mime-types (3.7.0) sha256=dcebf61c246f08e15a4de34e386ebe8233791e868564a470c3fe77c00eed5e56 + mime-types-data (3.2026.1103) sha256=cda7af94d895f1fd33e000acea800e6ed31e4c861e3d6c1a27c8b74f9e44dc64 + multipart-post (2.4.1) sha256=9872d03a8e552020ca096adadbf5e3cb1cd1cdd6acd3c161136b8a5737cdb4a8 + net-http (0.10.0) sha256=1a2ff6e37a05724973c43f2bf7bf105f59c36a4cd0a7e9b1c021a0a566987c27 + net-protocol (0.4.0) sha256=f9f90c8c6f64794a45160bf422177a336db12809de3beeeaa26b88fcd2e8c853 + overcommit (0.75.0) sha256=dc0330a76e4c7b11d82e93e6011d181d2f08bc109a3bfbd21f91dea51269e522 + parallel (1.28.0) sha256=33e6de1484baf2524792d178b0913fc8eb94c628d6cfe45599ad4458c638c970 + parser (3.3.13.0) sha256=e02f9b63c2fe05b0c68f692ba4df89b3cb113b663964f75d988fbebfd6c3deca + prism (1.9.0) sha256=7b530c6a9f92c24300014919c9dcbc055bf4cdf51ec30aed099b06cd6674ef85 + racc (1.8.1) sha256=4a7f6929691dbec8b5209a0b373bc2614882b55fc5d2e447a21aaa691303d62f + rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a + rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701 + regexp_parser (2.13.1) sha256=5aedb6b7c35688f51e86eef17a15374dfd287c83f4ca644b2c5f3ec50a33b44b + rexml (3.4.4) sha256=19e0a2c3425dfbf2d4fc1189747bdb2f849b6c5e74180401b15734bc97b5d142 + rspec (3.13.2) sha256=206284a08ad798e61f86d7ca3e376718d52c0bc944626b2349266f239f820587 + rspec-core (3.13.7) sha256=c64ff16d9788119805cbc6a3f4b6d87cc22417c2d25976a3330de136896b5469 + rspec-expectations (3.13.6) sha256=bf538ca6d367ff5b0d85a87fdb5c0c6038f2cd1308a0eeba07476caa02d22f35 + rspec-mocks (3.13.9) sha256=ea5c7ff3054a6098ca6039aacef80aec69d5a8a3ef80be8fc88337359b672a30 + rspec-support (3.13.8) sha256=e4160030309b7e3e9d9863dfe720a35dfcc577162f5402cd68fc8c5258a0ffd6 + rubocop (1.92.2) sha256=14fb31eed143a0dc6f78186c550c1715e81cd38f59d05d1165050e445c1f5382 + rubocop-ast (1.50.0) sha256=b9ca88300da0803ee222ad20cdb30494c0a784eed06fdc35d254b06d662788db + ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33 + timeout (0.6.1) sha256=78f57368a7e7bbadec56971f78a3f5ecbcfb59b7fcbb0a3ed6ddc08a5094accb + unicode-display_width (3.3.0) sha256=4b7aa66a4b11db50f6f7e98411215cd0dd10eaecb665d36eec5bcacb3fa0b613 + unicode-emoji (4.3.0) sha256=11c02fa73290378c066bb0562cd4c87d8e1b706fbbe0059ca12746a4244de8ce + uri (1.1.2) sha256=89292e98eabbef6248b0a0ed5e7e398e152f83f17560023a6d94a7f492b169fb + +BUNDLED WITH + 2.6.9 diff --git a/checkout_sdk.gemspec b/checkout_sdk.gemspec index 7ae5f4f..da4a106 100644 --- a/checkout_sdk.gemspec +++ b/checkout_sdk.gemspec @@ -22,7 +22,8 @@ sellers and service providers." # The `git ls-files -z` loads the files in the RubyGem that have been added into git. spec.files = Dir.chdir(__dir__) do `git ls-files -z`.split("\x0").reject do |f| - (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)}) + (f == __FILE__) || f == 'Gemfile.lock' || + f.match(%r{\A(?:(?:bin|test|spec|features|gemfiles)/|\.(?:git|travis|circleci)|appveyor)}) end end spec.bindir = 'exe' @@ -33,7 +34,7 @@ sellers and service providers." spec.add_development_dependency 'rubocop', '~> 1.82' spec.add_development_dependency 'overcommit', '~> 0.60' spec.add_dependency 'csv', '>= 3.0' - spec.add_dependency 'faraday', '>= 2.0' + spec.add_dependency 'faraday', '>= 2.0', '< 3' spec.add_dependency 'faraday-follow_redirects', '~> 0.5' spec.add_dependency 'faraday-multipart', '~> 1.2' spec.add_dependency 'mime-types', '~> 3.0' diff --git a/gemfiles/ruby_2_7.gemfile b/gemfiles/ruby_2_7.gemfile new file mode 100644 index 0000000..fa55f75 --- /dev/null +++ b/gemfiles/ruby_2_7.gemfile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# Used only by the Ruby 2.7 CI leg (BUNDLE_GEMFILE=gemfiles/ruby_2_7.gemfile). Same dependencies +# as ../Gemfile, locked for Ruby 2.7 in ruby_2_7.gemfile.lock, because Gemfile.lock is resolved for +# Ruby 3.1 and some of its gems need Ruby 3.0 or later. Regenerate both locks with +# .github/scripts/update-lockfiles.sh. +eval_gemfile File.expand_path('../Gemfile', __dir__) diff --git a/gemfiles/ruby_2_7.gemfile.lock b/gemfiles/ruby_2_7.gemfile.lock new file mode 100644 index 0000000..cad648c --- /dev/null +++ b/gemfiles/ruby_2_7.gemfile.lock @@ -0,0 +1,98 @@ +PATH + remote: .. + specs: + checkout_sdk (2.8.2) + csv (>= 3.0) + faraday (>= 2.0, < 3) + faraday-follow_redirects (~> 0.5) + faraday-multipart (~> 1.2) + mime-types (~> 3.0) + +GEM + remote: https://rubygems.org/ + specs: + ast (2.4.3) + base64 (0.3.0) + childprocess (5.1.0) + logger (~> 1.5) + csv (3.3.7) + diff-lcs (1.6.2) + faraday (2.8.1) + base64 + faraday-net_http (>= 2.0, < 3.1) + ruby2_keywords (>= 0.0.4) + faraday-follow_redirects (0.5.0) + faraday (>= 1, < 3) + faraday-multipart (1.2.0) + multipart-post (~> 2.0) + faraday-net_http (3.0.2) + iniparse (1.5.0) + json (3.1.0) + language_server-protocol (3.17.0.6) + lint_roller (1.1.0) + logger (1.7.0) + mime-types (3.7.0) + logger + mime-types-data (~> 3.2025, >= 3.2025.0507) + mime-types-data (3.2026.1103) + multipart-post (2.4.1) + overcommit (0.75.0) + childprocess (>= 5.0.0, < 6) + iniparse (~> 1.4) + rexml (>= 3.4.2) + parallel (1.28.0) + parser (3.3.13.0) + ast (~> 2.4.1) + racc + prism (1.9.0) + racc (1.8.1) + rainbow (3.1.1) + rake (13.4.2) + regexp_parser (2.13.1) + rexml (3.4.4) + rspec (3.13.2) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.7) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.6) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.9) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.8) + rubocop (1.92.2) + json (>= 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) + parallel (>= 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.11.3, < 3.0) + rubocop-ast (>= 1.49.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.50.0) + parser (>= 3.3.7.2) + prism (~> 1.7) + ruby-progressbar (1.13.0) + ruby2_keywords (0.0.5) + unicode-display_width (3.3.0) + unicode-emoji (~> 4.3) + unicode-emoji (4.3.0) + +PLATFORMS + ruby + x86_64-linux + +DEPENDENCIES + checkout_sdk! + overcommit (~> 0.60) + rake (>= 13.0) + rspec (>= 3.0) + rubocop (~> 1.82) + +BUNDLED WITH + 2.4.22 From 9f0958fe68e345208ad3e2191e72e19fb7ba768e Mon Sep 17 00:00:00 2001 From: david ruiz Date: Mon, 9 Nov 2026 17:21:38 +0100 Subject: [PATCH 2/4] Comments fine tune --- .github/scripts/check_lockfile_checksums.rb | 3 +-- .github/workflows/build-master.yml | 7 +++-- .github/workflows/build-pull-request.yml | 7 +++-- .github/workflows/build-release.yml | 30 +++++++-------------- 4 files changed, 16 insertions(+), 31 deletions(-) diff --git a/.github/scripts/check_lockfile_checksums.rb b/.github/scripts/check_lockfile_checksums.rb index bf45fcc..6cfd844 100644 --- a/.github/scripts/check_lockfile_checksums.rb +++ b/.github/scripts/check_lockfile_checksums.rb @@ -2,8 +2,7 @@ # Fails unless the lockfile only uses rubygems.org and the local gemspec, and every gem from # rubygems.org has a sha256 in its CHECKSUMS section. -# Bundler verifies downloads against CHECKSUMS but installs without complaint when the section or an -# entry is missing, so CI checks the coverage first. Standard library only. +# Run by CI before bundle install. Standard library only. # Usage: ruby .github/scripts/check_lockfile_checksums.rb Gemfile.lock def fail_check(message) diff --git a/.github/workflows/build-master.yml b/.github/workflows/build-master.yml index 2ab9397..f18be4f 100644 --- a/.github/workflows/build-master.yml +++ b/.github/workflows/build-master.yml @@ -31,7 +31,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - # setup-ruby would generate a fresh lock if none were committed: refuse instead. + # Requires the committed lockfile. - id: require-lockfile run: | if [ ! -f "${BUNDLE_GEMFILE}.lock" ]; then @@ -42,10 +42,9 @@ jobs: uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0 with: ruby-version: ${{ matrix.ruby-version }} - # No gem cache: a restored cache is not checked against the lock's checksums. + # Gems are installed fresh on every run. bundler-cache: false - # The Ruby 2.7 lock cannot carry checksums (Bundler 2.4): only legs that install from - # Gemfile.lock check them. + # Checksum check for the legs that install from Gemfile.lock. - id: check-lockfile-checksums if: env.BUNDLE_GEMFILE == 'Gemfile' run: ruby .github/scripts/check_lockfile_checksums.rb Gemfile.lock diff --git a/.github/workflows/build-pull-request.yml b/.github/workflows/build-pull-request.yml index e2e36fd..827ea32 100644 --- a/.github/workflows/build-pull-request.yml +++ b/.github/workflows/build-pull-request.yml @@ -31,7 +31,7 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - # setup-ruby would generate a fresh lock if none were committed: refuse instead. + # Requires the committed lockfile. - id: require-lockfile run: | if [ ! -f "${BUNDLE_GEMFILE}.lock" ]; then @@ -42,10 +42,9 @@ jobs: uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0 with: ruby-version: ${{ matrix.ruby-version }} - # No gem cache: a restored cache is not checked against the lock's checksums. + # Gems are installed fresh on every run. bundler-cache: false - # The Ruby 2.7 lock cannot carry checksums (Bundler 2.4): only legs that install from - # Gemfile.lock check them. + # Checksum check for the legs that install from Gemfile.lock. - id: check-lockfile-checksums if: env.BUNDLE_GEMFILE == 'Gemfile' run: ruby .github/scripts/check_lockfile_checksums.rb Gemfile.lock diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index 759915f..be58073 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -6,9 +6,8 @@ name: build-release # the pre-release version given as input (for example 2.9.0.rc.1, never installed unless asked # for) and creates the GitHub release as a pre-release. # -# Jobs: build (no secrets) -> test (sandbox secrets) -> publish (RubyGems trusted publishing, -# environment "rubygems") -> release (gh release create). Only publish can obtain a RubyGems key, -# and the key only exists inside its publish step. +# Jobs: build -> test -> publish (RubyGems trusted publishing, environment "rubygems") -> release +# (gh release create). on: push: branches: @@ -116,9 +115,8 @@ jobs: with: ruby-version: '3.1' bundler-cache: false - # Frozen: fails when Gemfile.lock is out of date with the Gemfile, the gemspec or the - # version, and when a downloaded gem does not match its checksum in the lock. No cache: - # every gem is downloaded and checked. + # Frozen install from Gemfile.lock: fails when the lock is out of date with the Gemfile, the + # gemspec or the version. - id: check-lockfile-checksums run: ruby .github/scripts/check_lockfile_checksums.rb Gemfile.lock - id: install-dependencies @@ -143,35 +141,25 @@ jobs: publish: needs: [build, test] - # The image's own Ruby runs the publish step: no downloaded interpreter or action other than - # download-artifact runs in this job. + # Publishes with the runner image's Ruby. runs-on: ubuntu-24.04 - # Protected environment: deployment branch rule "master" and the RubyGems trusted publisher - # are both bound to it. + # Deployment environment for publishing (also configured in the RubyGems trusted publisher). environment: rubygems permissions: id-token: write # OIDC token for RubyGems trusted publishing steps: - # No checkout and no bundle install: nothing from the repository or from RubyGems runs - # next to the publishing key. The gem is the artifact built and checked by the build job, - # and it must have the sha256 the build job recorded. + # Publishes the gem artifact from the build job after checking its sha256. - id: download-gem uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: name: gem path: dist - # RubyGems trusted publishing: the job's OIDC token is exchanged for a short-lived key scoped - # to this gem. The key is held only in this step's memory and the child gem process - # environment: never written to disk or to $GITHUB_ENV. + # RubyGems trusted publishing (OIDC). - id: publish-gem env: VERSION: ${{ needs.build.outputs.version }} EXPECTED_SHA256: ${{ needs.build.outputs.sha256 }} - # Only Ruby's own default libraries load, in an emptied environment: env -i with an explicit - # allowlist (no RUBYOPT, RUBYLIB, GEMRC, LD_PRELOAD, SSL_* from earlier steps), an empty HOME - # (no ~/.gemrc), --disable-gems and an empty GEM_HOME/GEM_PATH (the image installs newer - # json/net-http gems that would otherwise shadow the default ones). The gem push child - # inherits this environment. + # Runs with a minimal environment and Ruby's default libraries only. run: | set -euo pipefail empty_gem_dir="$(/usr/bin/mktemp -d)" From 240475af6e31c84443aadb83280d9ba5f62f7a54 Mon Sep 17 00:00:00 2001 From: david ruiz Date: Tue, 10 Nov 2026 12:06:53 +0100 Subject: [PATCH 3/4] Pipeline fixes --- .github/scripts/update-lockfiles.sh | 9 ++-- .github/workflows/build-release.yml | 71 ++++++++++++++++++++++------- README.md | 13 ++++++ 3 files changed, 74 insertions(+), 19 deletions(-) diff --git a/.github/scripts/update-lockfiles.sh b/.github/scripts/update-lockfiles.sh index 5654038..2dad424 100755 --- a/.github/scripts/update-lockfiles.sh +++ b/.github/scripts/update-lockfiles.sh @@ -24,12 +24,15 @@ case "${1:-}" in *) echo "usage: $0 [--update]" >&2; exit 2 ;; esac -# ruby_version bundler_version gemfile extra_lock_args lock() { + local ruby_version="$1" + local bundler_version="$2" + local gemfile="$3" + local extra_lock_args="$4" docker run --rm --platform linux/amd64 \ -v "$PWD:/src" -w /src \ - -e BUNDLE_GEMFILE="$3" -e UPDATE="$update" -e BUNDLER_VERSION="$2" -e EXTRA="$4" \ - "ruby:$1" bash -euc ' + -e BUNDLE_GEMFILE="$gemfile" -e UPDATE="$update" -e BUNDLER_VERSION="$bundler_version" -e EXTRA="$extra_lock_args" \ + "ruby:${ruby_version}" bash -euc ' git config --global --add safe.directory /src gem install bundler -v "$BUNDLER_VERSION" --no-document --silent # shellcheck disable=SC2086 diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index be58073..46ac2f7 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -4,7 +4,9 @@ name: build-release # # Manual run (workflow_dispatch, master only) is the dry run of the whole pipeline: it publishes # the pre-release version given as input (for example 2.9.0.rc.1, never installed unless asked -# for) and creates the GitHub release as a pre-release. +# for) and creates the GitHub release as a pre-release. The input must be greater than the +# current version. Its tag points at the master commit, whose version.rb still holds the current +# version: only the built gem carries the pre-release version. # # Jobs: build -> test -> publish (RubyGems trusted publishing, environment "rubygems") -> release # (gh release create). @@ -31,7 +33,8 @@ concurrency: jobs: build: if: github.ref == 'refs/heads/master' - runs-on: ubuntu-latest + # Builds with the runner image's Ruby, like the publish job. + runs-on: ubuntu-24.04 outputs: version: ${{ steps.version.outputs.version }} prerelease: ${{ steps.version.outputs.prerelease }} @@ -40,11 +43,6 @@ jobs: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - id: setup-ruby - uses: ruby/setup-ruby@b3aaceaef7e0ee231b499e7b5f49c03fc1d32199 # v1.338.0 - with: - ruby-version: '3.1' - bundler: none - id: version env: EVENT_NAME: ${{ github.event_name }} @@ -53,13 +51,22 @@ jobs: set -euo pipefail release_pattern='^[0-9]+\.[0-9]+\.[0-9]+$' prerelease_pattern='^[0-9]+\.[0-9]+\.[0-9]+\.(alpha|beta|rc|pre)\.?[0-9]+$' - read_version() { ruby -e 'load "lib/checkout_sdk/version.rb"; print CheckoutSdk::VERSION'; } + read_version() { /usr/bin/ruby --disable-gems -e 'load "lib/checkout_sdk/version.rb"; print CheckoutSdk::VERSION'; } if [ "$EVENT_NAME" = 'workflow_dispatch' ]; then version="$PRERELEASE_VERSION" if ! [[ "$version" =~ $prerelease_pattern ]]; then echo '::error::A manual run only publishes a pre-release version such as 2.9.0.rc.1' exit 1 fi + current="$(read_version)" + if ! [[ "$current" =~ $release_pattern || "$current" =~ $prerelease_pattern ]]; then + echo '::error::lib/checkout_sdk/version.rb does not hold a valid version' + exit 1 + fi + if ! /usr/bin/ruby -e 'exit(Gem::Version.new(ARGV[0]) > Gem::Version.new(ARGV[1]))' "$version" "$current"; then + echo "::error::The pre-release version must be greater than the current version ${current}" + exit 1 + fi sed -i -E "s/^ VERSION = '[^']*'$/ VERSION = '${version}'/" lib/checkout_sdk/version.rb else version="$(read_version)" @@ -80,10 +87,17 @@ jobs: - id: build-gem env: VERSION: ${{ steps.version.outputs.version }} + # Runs with a minimal environment and Ruby's default libraries only. run: | set -euo pipefail - gem build checkout_sdk.gemspec --output "checkout_sdk-${VERSION}.gem" - ruby .github/scripts/verify_gem.rb "$VERSION" + empty_gem_dir="$(/usr/bin/mktemp -d)" + empty_home="$(/usr/bin/mktemp -d)" + isolated() { + /usr/bin/env -i PATH=/usr/bin:/bin LANG=C.UTF-8 HOME="$empty_home" \ + GEM_HOME="$empty_gem_dir" GEM_PATH="$empty_gem_dir" "$@" + } + isolated /usr/bin/gem build checkout_sdk.gemspec --output "checkout_sdk-${VERSION}.gem" + isolated /usr/bin/ruby .github/scripts/verify_gem.rb "$VERSION" sha256="$(sha256sum "checkout_sdk-${VERSION}.gem" | cut -d ' ' -f 1)" echo "sha256 of checkout_sdk-${VERSION}.gem: $sha256" echo "sha256=$sha256" >> "$GITHUB_OUTPUT" @@ -182,6 +196,13 @@ jobs: abort "Unexpected response body (HTTP #{response.code}); not printed" end + # Network failures abort with the error class only. + def request(name) + yield + rescue StandardError => e + abort "#{name} failed (#{e.class})" + end + version = ENV.fetch('VERSION') unless version.match?(/\A\d+\.\d+\.\d+(\.(alpha|beta|rc|pre)\.?\d+)?\z/) abort 'Unexpected version from the build job' @@ -203,16 +224,20 @@ jobs: id_uri = URI("#{ENV.fetch('ACTIONS_ID_TOKEN_REQUEST_URL')}&audience=rubygems.org") id_request = Net::HTTP::Get.new(id_uri) id_request['Authorization'] = "Bearer #{ENV.fetch('ACTIONS_ID_TOKEN_REQUEST_TOKEN')}" - id_response = Net::HTTP.start(id_uri.host, id_uri.port, use_ssl: true) { |http| http.request(id_request) } + id_response = request('OIDC token request') do + Net::HTTP.start(id_uri.host, id_uri.port, use_ssl: true) { |http| http.request(id_request) } + end abort "OIDC token request failed (HTTP #{id_response.code})" unless id_response.is_a?(Net::HTTPSuccess) jwt = json_field(id_response, 'value') puts "::add-mask::#{jwt}" - exchange_response = Net::HTTP.post( - URI('https://rubygems.org/api/v1/oidc/trusted_publisher/exchange_token'), - JSON.generate(jwt: jwt), - 'Content-Type' => 'application/json', 'Accept' => 'application/json' - ) + exchange_response = request('RubyGems trusted publisher exchange') do + Net::HTTP.post( + URI('https://rubygems.org/api/v1/oidc/trusted_publisher/exchange_token'), + JSON.generate(jwt: jwt), + 'Content-Type' => 'application/json', 'Accept' => 'application/json' + ) + end unless exchange_response.is_a?(Net::HTTPSuccess) abort "RubyGems trusted publisher exchange failed (HTTP #{exchange_response.code}). " \ 'Check the trusted publisher of checkout_sdk on rubygems.org.' @@ -241,6 +266,20 @@ jobs: NOTES: ${{ github.event.head_commit.message }} run: | set -euo pipefail + # gh release create reuses an existing tag and ignores --target: the version tag must be + # absent or already point at this commit. + refs="$(gh api "repos/${GITHUB_REPOSITORY}/git/matching-refs/tags/${VERSION}")" + tag="$(jq -c --arg ref "refs/tags/${VERSION}" '[.[] | select(.ref == $ref)] | first // empty' <<< "$refs")" + if [ -n "$tag" ]; then + tag_sha="$(jq -r '.object.sha' <<< "$tag")" + if [ "$(jq -r '.object.type' <<< "$tag")" = 'tag' ]; then + tag_sha="$(gh api "repos/${GITHUB_REPOSITORY}/git/tags/${tag_sha}" --jq '.object.sha')" + fi + if [ "$tag_sha" != "$GITHUB_SHA" ]; then + echo "::error::Tag ${VERSION} already exists at ${tag_sha}, not at ${GITHUB_SHA}" + exit 1 + fi + fi args=(--repo "$GITHUB_REPOSITORY" --target "$GITHUB_SHA" --title "$VERSION" --notes "${NOTES:-Pre-release $VERSION}") if [ "$PRERELEASE" = 'true' ]; then args+=(--prerelease); fi gh release create "$VERSION" "${args[@]}" diff --git a/README.md b/README.md index b1f216c..537854f 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,19 @@ The execution of integration tests require the following environment variables s ## Development Setup +### Lockfiles + +CI installs dependencies only from the committed lockfiles: `Gemfile.lock` (Ruby 3.1 and later) +and `gemfiles/ruby_2_7.gemfile.lock` (Ruby 2.7). It fails when a lockfile is missing or out of date. +After changing the `Gemfile`, the gemspec dependencies or `lib/checkout_sdk/version.rb`, regenerate +both with Docker running: + +```bash +.github/scripts/update-lockfiles.sh +``` + +Add `--update` to move every gem to the newest allowed version. + ### Git Hooks with Overcommit This project uses [Overcommit](https://github.com/sds/overcommit) to manage Git hooks and ensure code quality. From 4931bd9841a594e55de0134779844eeab8c487a0 Mon Sep 17 00:00:00 2001 From: david ruiz Date: Tue, 10 Nov 2026 12:54:47 +0100 Subject: [PATCH 4/4] Pipeline fixes --- .github/scripts/check_lockfile_checksums.rb | 4 +- .github/scripts/set_version.rb | 41 +++++++++++++++++++++ .github/scripts/update-lockfiles.sh | 20 ++++++++-- .github/workflows/build-release.yml | 39 +++++++++++++++++--- README.md | 12 ++++-- 5 files changed, 102 insertions(+), 14 deletions(-) create mode 100644 .github/scripts/set_version.rb diff --git a/.github/scripts/check_lockfile_checksums.rb b/.github/scripts/check_lockfile_checksums.rb index 6cfd844..e85b3fd 100644 --- a/.github/scripts/check_lockfile_checksums.rb +++ b/.github/scripts/check_lockfile_checksums.rb @@ -26,7 +26,9 @@ def fail_check(message) end foreign = sections.keys & ['GIT', 'PLUGIN SOURCE', 'GEM SOURCE'] -fail_check("#{path} has #{foreign.join(' and ')} sources; only rubygems.org and the local gemspec are allowed") unless foreign.empty? +unless foreign.empty? + fail_check("#{path} has #{foreign.join(' and ')} sources; only rubygems.org and the local gemspec are allowed") +end remotes = (sections['GEM'] + sections['PATH']).filter_map { |line| line[/\A remote: (.+)\z/, 1] } fail_check("#{path} has unexpected remotes: #{remotes.inspect}") unless remotes.sort == ['.', 'https://rubygems.org/'] diff --git a/.github/scripts/set_version.rb b/.github/scripts/set_version.rb new file mode 100644 index 0000000..f19f8bf --- /dev/null +++ b/.github/scripts/set_version.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +# Sets the gem version in lib/checkout_sdk/version.rb and in the checkout_sdk lines of the committed +# lockfiles, so a version bump needs neither Docker nor a dependency resolve. Dependency changes still +# need .github/scripts/update-lockfiles.sh. Standard library only; run from the repository root. +# Usage: ruby .github/scripts/set_version.rb + +VERSION_PATTERN = /\A\d+\.\d+\.\d+(\.(alpha|beta|rc|pre)\.?\d+)?\z/.freeze +VERSION_FILE = 'lib/checkout_sdk/version.rb' +# Lockfile => number of "checkout_sdk (X.Y.Z)" lines it holds (PATH specs, plus CHECKSUMS when present). +LOCKFILES = { 'Gemfile.lock' => 2, 'gemfiles/ruby_2_7.gemfile.lock' => 1 }.freeze + +def fail_check(message) + warn "set_version: #{message}" + exit 1 +end + +# Replaces every match of pattern in path, after checking it matches exactly expected times. +def rewrite(path, pattern, expected) + fail_check("#{path} is not a regular file") unless File.file?(path) && !File.symlink?(path) + text = File.read(path) + found = text.scan(pattern).size + fail_check("expected #{expected} version line(s) in #{path}, found #{found}") unless found == expected + File.write(path, yield(text)) +end + +fail_check('usage: set_version.rb ') unless ARGV.size == 1 +version = ARGV.first +fail_check('the version is not a release or pre-release version') unless version.match?(VERSION_PATTERN) + +version_line = /^ VERSION = '[^']*'$/ +rewrite(VERSION_FILE, version_line, 1) { |text| text.sub(version_line, " VERSION = '#{version}'") } + +lock_line = /^( {2}| {4})checkout_sdk \([^)]+\)$/ +LOCKFILES.each do |path, expected| + rewrite(path, lock_line, expected) do |text| + text.gsub(lock_line) { "#{Regexp.last_match(1)}checkout_sdk (#{version})" } + end +end + +puts "set_version: #{version} in #{VERSION_FILE} and #{LOCKFILES.keys.join(', ')}" diff --git a/.github/scripts/update-lockfiles.sh b/.github/scripts/update-lockfiles.sh index 2dad424..5732b26 100755 --- a/.github/scripts/update-lockfiles.sh +++ b/.github/scripts/update-lockfiles.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash # Regenerates the committed lockfiles in linux/amd64 containers, so they resolve for the CI -# runners and not for the local machine. Needs Docker. +# runners and not for the local machine. Needs Docker, except with --version-only. # # Gemfile.lock Ruby 3.1, Bundler 2.6.9, with gem checksums. # Ruby 3.1 and 3.3 CI legs and the release workflow. @@ -12,7 +12,10 @@ # gem's own version). Without arguments it keeps every locked version that still satisfies the # Gemfile; `--update` re-resolves all gems to the newest allowed versions. # -# Usage: .github/scripts/update-lockfiles.sh [--update] +# After a version bump only, `--version-only` copies the version from lib/checkout_sdk/version.rb +# into both lockfiles with any local Ruby, no Docker and no dependency resolve. +# +# Usage: .github/scripts/update-lockfiles.sh [--update | --version-only] set -euo pipefail cd "$(dirname "$0")/../.." @@ -21,9 +24,20 @@ update='' case "${1:-}" in '') ;; --update) update='--update' ;; - *) echo "usage: $0 [--update]" >&2; exit 2 ;; + --version-only) + version="$(ruby -e 'load "lib/checkout_sdk/version.rb"; print CheckoutSdk::VERSION')" + ruby .github/scripts/set_version.rb "$version" + echo 'Lockfiles updated. Review the diff before committing: only checkout_sdk lines may change.' + exit 0 + ;; + *) echo "usage: $0 [--update | --version-only]" >&2; exit 2 ;; esac +if ! docker info > /dev/null 2>&1; then + echo 'Docker is not running. After a version bump only, use --version-only instead.' >&2 + exit 1 +fi + lock() { local ruby_version="$1" local bundler_version="$2" diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index 46ac2f7..baa22b0 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -2,11 +2,12 @@ name: build-release # Publishes the gem when lib/checkout_sdk/version.rb changes on master. # -# Manual run (workflow_dispatch, master only) is the dry run of the whole pipeline: it publishes -# the pre-release version given as input (for example 2.9.0.rc.1, never installed unless asked -# for) and creates the GitHub release as a pre-release. The input must be greater than the -# current version. Its tag points at the master commit, whose version.rb still holds the current -# version: only the built gem carries the pre-release version. +# Manual run (workflow_dispatch, master only) is the dry run of the whole pipeline: it tests and +# publishes the pre-release version given as input (for example 2.9.0.rc.1, never installed unless +# asked for; it must be greater than the current version) and creates the GitHub release as a draft +# pre-release, which has no git tag. Delete the draft afterwards. +# +# A failed run can be re-run: publishing skips a version already on RubyGems with the same sha256. # # Jobs: build -> test -> publish (RubyGems trusted publishing, environment "rubygems") -> release # (gh release create). @@ -67,7 +68,7 @@ jobs: echo "::error::The pre-release version must be greater than the current version ${current}" exit 1 fi - sed -i -E "s/^ VERSION = '[^']*'$/ VERSION = '${version}'/" lib/checkout_sdk/version.rb + /usr/bin/ruby --disable-gems .github/scripts/set_version.rb "$version" else version="$(read_version)" if ! [[ "$version" =~ $release_pattern || "$version" =~ $prerelease_pattern ]]; then @@ -129,6 +130,12 @@ jobs: with: ruby-version: '3.1' bundler-cache: false + # A manual run tests the pre-release version it publishes. + - id: set-dry-run-version + if: github.event_name == 'workflow_dispatch' + env: + VERSION: ${{ needs.build.outputs.version }} + run: ruby .github/scripts/set_version.rb "$VERSION" # Frozen install from Gemfile.lock: fails when the lock is out of date with the Gemfile, the # gemspec or the version. - id: check-lockfile-checksums @@ -218,6 +225,23 @@ jobs: abort "The gem is not the one the build job produced (sha256 #{actual_sha256})" unless actual_sha256 == expected_sha256 puts "sha256 of #{gem_file}: #{actual_sha256} (matches the build job)" + # Re-runs: a version already published with the same sha256 is not pushed again. + lookup = request('RubyGems version lookup') do + Net::HTTP.get_response(URI("https://rubygems.org/api/v2/rubygems/checkout_sdk/versions/#{version}.json")) + end + case lookup + when Net::HTTPNotFound + nil + when Net::HTTPSuccess + published_sha256 = json_field(lookup, 'sha') + abort "checkout_sdk #{version} is already on RubyGems with another sha256 (#{published_sha256})" \ + unless published_sha256 == actual_sha256 + puts "checkout_sdk #{version} is already on RubyGems with this sha256: nothing to push" + exit 0 + else + abort "RubyGems version lookup failed (HTTP #{lookup.code})" + end + if ENV['ACTIONS_ID_TOKEN_REQUEST_URL'].to_s.empty? || ENV['ACTIONS_ID_TOKEN_REQUEST_TOKEN'].to_s.empty? abort 'No OIDC token available: the publish job needs permissions id-token: write' end @@ -261,6 +285,7 @@ jobs: - id: create-release env: GH_TOKEN: ${{ github.token }} + EVENT_NAME: ${{ github.event_name }} VERSION: ${{ needs.build.outputs.version }} PRERELEASE: ${{ needs.build.outputs.prerelease }} NOTES: ${{ github.event.head_commit.message }} @@ -282,4 +307,6 @@ jobs: fi args=(--repo "$GITHUB_REPOSITORY" --target "$GITHUB_SHA" --title "$VERSION" --notes "${NOTES:-Pre-release $VERSION}") if [ "$PRERELEASE" = 'true' ]; then args+=(--prerelease); fi + # A manual run is a dry run: draft release, no git tag. + if [ "$EVENT_NAME" = 'workflow_dispatch' ]; then args+=(--draft); fi gh release create "$VERSION" "${args[@]}" diff --git a/README.md b/README.md index 537854f..2ff7f6a 100644 --- a/README.md +++ b/README.md @@ -257,14 +257,18 @@ The execution of integration tests require the following environment variables s CI installs dependencies only from the committed lockfiles: `Gemfile.lock` (Ruby 3.1 and later) and `gemfiles/ruby_2_7.gemfile.lock` (Ruby 2.7). It fails when a lockfile is missing or out of date. -After changing the `Gemfile`, the gemspec dependencies or `lib/checkout_sdk/version.rb`, regenerate -both with Docker running: +After a version bump in `lib/checkout_sdk/version.rb`, update both with any local Ruby: ```bash -.github/scripts/update-lockfiles.sh +.github/scripts/update-lockfiles.sh --version-only ``` -Add `--update` to move every gem to the newest allowed version. +After changing the `Gemfile` or the gemspec dependencies, regenerate them with Docker running +(`--update` moves every gem to the newest allowed version): + +```bash +.github/scripts/update-lockfiles.sh +``` ### Git Hooks with Overcommit