|
| 1 | +name: Publish gem |
| 2 | + |
| 3 | +# TODO: Implement a dry-run mode to verify the checks without publishing |
| 4 | +on: workflow_dispatch |
| 5 | + |
| 6 | +concurrency: "rubygems" # Only one publish job at a time |
| 7 | + |
| 8 | +jobs: |
| 9 | + verify-checks: |
| 10 | + name: Verify commit status checks |
| 11 | + runs-on: ubuntu-24.04 |
| 12 | + permissions: |
| 13 | + checks: read |
| 14 | + outputs: |
| 15 | + version: ${{ steps.version.outputs.version }} |
| 16 | + steps: |
| 17 | + - uses: actions/checkout@v4 |
| 18 | + - uses: ruby/setup-ruby@v1 |
| 19 | + with: |
| 20 | + ruby-version: '3.3.7' |
| 21 | + |
| 22 | + - id: version |
| 23 | + run: echo "version=$(ruby -e 'puts Gem::Specification::load(Dir.glob("*.gemspec").first).version')" >> $GITHUB_OUTPUT |
| 24 | + |
| 25 | + # Check if the gem version is already published |
| 26 | + - name: Verify gem version |
| 27 | + env: |
| 28 | + GEM_VERSION: ${{ steps.version.outputs.version }} |
| 29 | + run: | |
| 30 | + if gem search datadog --exact --remote --version "$GEM_VERSION" | grep -q "($GEM_VERSION)"; then |
| 31 | + echo "::error::Version $GEM_VERSION is already published" |
| 32 | + exit 1 |
| 33 | + else |
| 34 | + echo "Version $GEM_VERSION is not published yet" |
| 35 | + fi |
| 36 | +
|
| 37 | + # TODO: Verify draft release |
| 38 | + # TODO: Verify milestone |
| 39 | + |
| 40 | + # Check if the commit has passed all Github checks |
| 41 | + # API: https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#list-check-runs-for-a-git-reference |
| 42 | + - name: Verify check runs |
| 43 | + uses: actions/github-script@v7 |
| 44 | + with: |
| 45 | + script: | |
| 46 | + const checkRuns = await github.paginate(github.rest.checks.listForRef, { |
| 47 | + owner: context.repo.owner, |
| 48 | + repo: context.repo.repo, |
| 49 | + ref: context.sha, |
| 50 | + per_page: 100 |
| 51 | + }); |
| 52 | +
|
| 53 | + const failedChecks = checkRuns.filter(check => |
| 54 | + check.status === 'completed' && |
| 55 | + check.conclusion !== 'success' && |
| 56 | + check.conclusion !== 'skipped' |
| 57 | + ); |
| 58 | +
|
| 59 | + if (failedChecks.length > 0) { |
| 60 | + const failedNames = failedChecks.map(c => c.name).join(', '); |
| 61 | + core.setFailed(`Check runs failed: ${failedNames}`); |
| 62 | + } |
| 63 | +
|
| 64 | + # Check if the commit has passed external CI checks |
| 65 | + # API: https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#get-the-combined-status-for-a-specific-reference |
| 66 | + - name: Verify commit status |
| 67 | + uses: actions/github-script@v7 |
| 68 | + with: |
| 69 | + script: | |
| 70 | + const { data: status } = await github.rest.repos.getCombinedStatusForRef({ |
| 71 | + owner: context.repo.owner, |
| 72 | + repo: context.repo.repo, |
| 73 | + ref: context.sha |
| 74 | + }); |
| 75 | +
|
| 76 | + if (status.state !== 'success') { |
| 77 | + core.setFailed(`Commit status is ${status.state}`); |
| 78 | + } |
| 79 | +
|
| 80 | + # Check if the commit has all the checks passed |
| 81 | + - name: Verify deferred commit data |
| 82 | + # NOTE: |
| 83 | + # |
| 84 | + # This step uses Github's internal API (for rendering the status of the checks in UI), |
| 85 | + # which includes Github check runs and external CI statuses and possibly more. |
| 86 | + # |
| 87 | + # Although Github check runs and external CI statuses are already covered by the previous steps, |
| 88 | + # it is still useful to have a double-check and also possibly unearth missing validations. |
| 89 | + # |
| 90 | + # However, not depending on Github's public API (REST/GraphQL) suggested that this might change in the future. |
| 91 | + run: | |
| 92 | + COMMIT_URL="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/commit/$GITHUB_SHA" |
| 93 | + STATUS=$(curl -sS --fail --retry 3 --retry-delay 5 "$COMMIT_URL/deferred_commit_data" | jq -r ".data.statusCheckStatus.state") |
| 94 | + if [ "$STATUS" != "success" ]; then |
| 95 | + echo "::error::Status check state is '$STATUS'. See: $COMMIT_URL" |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | +
|
| 99 | +
|
| 100 | + rubygems-release: |
| 101 | + name: Build and push gem to RubyGems.org |
| 102 | + runs-on: ubuntu-24.04 |
| 103 | + environment: "rubygems.org" # see: https://github.com/DataDog/dd-trace-rb/settings/environments |
| 104 | + needs: verify-checks # Make sure to release from a healthy commit |
| 105 | + permissions: |
| 106 | + id-token: write |
| 107 | + contents: write |
| 108 | + env: |
| 109 | + SKIP_SIMPLECOV: 1 |
| 110 | + steps: |
| 111 | + - uses: actions/checkout@v4 |
| 112 | + - name: Set up Ruby |
| 113 | + uses: ruby/setup-ruby@v1 |
| 114 | + with: |
| 115 | + ruby-version: '3.3.7' |
| 116 | + - uses: rubygems/release-gem@v1 |
| 117 | + with: |
| 118 | + attestations: false # PENDING decision for attestations |
| 119 | + |
| 120 | + github-release: |
| 121 | + name: Attach gem to GitHub release and publish |
| 122 | + runs-on: ubuntu-24.04 |
| 123 | + needs: |
| 124 | + - verify-checks |
| 125 | + - rubygems-release |
| 126 | + env: |
| 127 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 128 | + GH_REPO: ${{ github.repository }} |
| 129 | + GEM_VERSION: ${{ needs.verify-checks.outputs.version }} |
| 130 | + permissions: |
| 131 | + contents: write |
| 132 | + steps: |
| 133 | + - name: Download from RubyGems |
| 134 | + run: | |
| 135 | + gem fetch datadog --version ${GEM_VERSION} --verbose |
| 136 | + - name: Attach to existing release draft |
| 137 | + run: | |
| 138 | + gh release upload "v${GEM_VERSION}" *.gem --clobber |
| 139 | + gh release edit "v${GEM_VERSION}" --draft=false |
| 140 | +
|
| 141 | + # TODO: Close existing milestone and create next milestone |
0 commit comments