|
| 1 | +name: Release |
| 2 | +run-name: Checking convertapi/convertapi-php ${{ github.event.release.tag_name }} on Packagist |
| 3 | + |
| 4 | +# A release is a GitHub Release, never a merge. Merging to main only builds (build.yml). Publishing the |
| 5 | +# Release v{semver} creates the tag, and Packagist publishes every v* tag it sees through the repository's |
| 6 | +# GitHub hook - there is no upload step and no registry key. This workflow checks that the release is |
| 7 | +# legitimate and complete: who published it, that the tag is SemVer and on main, that ConvertApi::VERSION |
| 8 | +# matches it, that the tests pass against the API, and that Packagist serves the version from this commit. |
| 9 | +# |
| 10 | +# gh release create v3.0.1 --target main --generate-notes -> convertapi/convertapi-php v3.0.1 |
| 11 | +# gh release create v3.1.0-beta.1 --target main --prerelease ... -> convertapi/convertapi-php v3.1.0-beta.1 |
| 12 | +# |
| 13 | +# Packagist versions are public as soon as the tag exists, so a failure here does not stop a release; it |
| 14 | +# says the version needs a fix-forward release (see RELEASING.md). Nothing here retries or moves a tag. |
| 15 | +on: |
| 16 | + release: |
| 17 | + types: [published] |
| 18 | + |
| 19 | +permissions: |
| 20 | + contents: read |
| 21 | + |
| 22 | +env: |
| 23 | + # Only this account may publish a Release. GitHub cannot express this as a permission on the Team plan, |
| 24 | + # so it is checked here; the real control is the v* tag ruleset (only repository admins create v* tags). |
| 25 | + PUBLISHER: tomasr78 |
| 26 | + PACKAGE: convertapi/convertapi-php |
| 27 | + |
| 28 | +jobs: |
| 29 | + release: |
| 30 | + name: release |
| 31 | + runs-on: ubuntu-latest |
| 32 | + timeout-minutes: 30 |
| 33 | + environment: |
| 34 | + name: packagist |
| 35 | + url: https://packagist.org/packages/convertapi/convertapi-php#${{ github.event.release.tag_name }} |
| 36 | + concurrency: |
| 37 | + group: release-packagist |
| 38 | + cancel-in-progress: false |
| 39 | + steps: |
| 40 | + - name: Check who published the release |
| 41 | + run: | |
| 42 | + # A re-run keeps GITHUB_ACTOR as the original user; GITHUB_TRIGGERING_ACTOR is whoever pressed |
| 43 | + # "Re-run", so both must be the publisher or anyone with write access could replay a release. |
| 44 | + if [ "$GITHUB_ACTOR" != "$PUBLISHER" ] || [ "${GITHUB_TRIGGERING_ACTOR:-$GITHUB_ACTOR}" != "$PUBLISHER" ]; then |
| 45 | + echo "::error::Only $PUBLISHER may publish a release - this release was published by $GITHUB_ACTOR and run by ${GITHUB_TRIGGERING_ACTOR:-$GITHUB_ACTOR}." |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + echo "Published by $GITHUB_ACTOR" |
| 49 | +
|
| 50 | + # The release event runs this file from the tag, so the workflow and the code come from the same commit. |
| 51 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 52 | + with: |
| 53 | + fetch-depth: 0 |
| 54 | + persist-credentials: false |
| 55 | + |
| 56 | + - name: Check the tag and the version |
| 57 | + id: tag |
| 58 | + env: |
| 59 | + TAG: ${{ github.event.release.tag_name }} |
| 60 | + PRERELEASE: ${{ github.event.release.prerelease }} |
| 61 | + run: | |
| 62 | + set -euo pipefail |
| 63 | + # SemVer 2.0 without build metadata (Composer ignores "+..."). |
| 64 | + SEMVER='^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$' |
| 65 | + if ! [[ "$TAG" =~ $SEMVER ]]; then |
| 66 | + echo "::error::Tag '$TAG' is not v{major}.{minor}.{patch} or v{major}.{minor}.{patch}-{pre-release}, e.g. v3.0.1 or v3.1.0-beta.1" |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + if [ "$GITHUB_REF" != "refs/tags/$TAG" ]; then |
| 70 | + echo "::error::The run is on $GITHUB_REF, not on the release tag refs/tags/$TAG" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + VERSION="${TAG#v}" |
| 74 | + SHA=$(git rev-parse "refs/tags/$TAG^{commit}") |
| 75 | + if [ "$(git rev-parse HEAD)" != "$SHA" ]; then |
| 76 | + echo "::error::Checked out $(git rev-parse HEAD), but $TAG points at $SHA" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | +
|
| 80 | + # The GitHub "pre-release" tick and the version must agree, so a preview cannot pass for the |
| 81 | + # latest stable release on GitHub, and a stable version cannot hide as a pre-release. |
| 82 | + if [[ "$VERSION" == *-* ]]; then |
| 83 | + if [ "$PRERELEASE" != true ]; then |
| 84 | + echo "::error::$TAG is a pre-release version, so the GitHub Release must be marked as a pre-release (gh release create --prerelease)" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + elif [ "$PRERELEASE" = true ]; then |
| 88 | + echo "::error::$TAG is a stable version, but the GitHub Release is marked as a pre-release. Use a pre-release version such as ${TAG}-beta.1, or untick pre-release." |
| 89 | + exit 1 |
| 90 | + fi |
| 91 | + if ! git merge-base --is-ancestor "$SHA" origin/main; then |
| 92 | + echo "::error::$TAG ($SHA) is not on main. Releases, pre-releases included, are tagged on main only." |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | +
|
| 96 | + # The library has no build step - Packagist serves the tagged tree as it is - so the version the |
| 97 | + # client reports in its User-Agent is the constant in the code, and it must be the tag's. |
| 98 | + CONSTANT=$(sed -n "s/^ *const VERSION = '\(.*\)';.*/\1/p" lib/ConvertApi/ConvertApi.php) |
| 99 | + if [ "$CONSTANT" != "$VERSION" ]; then |
| 100 | + echo "::error file=lib/ConvertApi/ConvertApi.php::ConvertApi::VERSION is '$CONSTANT' at $TAG, not '$VERSION'. Packagist already serves $TAG; release the next version with the constant bumped." |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | +
|
| 104 | + { |
| 105 | + echo "version=$VERSION" |
| 106 | + echo "sha=$SHA" |
| 107 | + } >> "$GITHUB_OUTPUT" |
| 108 | + echo "Releasing $PACKAGE $TAG from $SHA" |
| 109 | +
|
| 110 | + - uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 |
| 111 | + with: |
| 112 | + php-version: '8.5' |
| 113 | + tools: composer:v2 |
| 114 | + coverage: none |
| 115 | + |
| 116 | + - name: Install |
| 117 | + run: | |
| 118 | + composer validate --strict |
| 119 | + composer install --no-interaction --no-progress --prefer-dist |
| 120 | +
|
| 121 | + # Every test, the API ones included. They run against CONVERT_API_BASE (the API the repository is |
| 122 | + # configured to test against) with CONVERT_API_SECRET, both set in the repository's Actions settings. |
| 123 | + - name: Tests |
| 124 | + env: |
| 125 | + CONVERT_API_SECRET: ${{ secrets.CONVERT_API_SECRET }} |
| 126 | + CONVERT_API_BASE: ${{ vars.CONVERT_API_BASE }} |
| 127 | + run: | |
| 128 | + if [ -z "$CONVERT_API_SECRET" ]; then |
| 129 | + echo "::error::The CONVERT_API_SECRET repository secret is not set, so the API tests cannot run." |
| 130 | + exit 1 |
| 131 | + fi |
| 132 | + bin/phpunit |
| 133 | +
|
| 134 | + # The GitHub hook tells Packagist about the new tag within seconds; Packagist then crawls the |
| 135 | + # repository and adds the version, usually within a few minutes. The tag must be served from the |
| 136 | + # tagged commit, or Packagist has an older copy of the tag. |
| 137 | + - name: Wait for Packagist to serve the version |
| 138 | + env: |
| 139 | + TAG: ${{ github.event.release.tag_name }} |
| 140 | + SHA: ${{ steps.tag.outputs.sha }} |
| 141 | + run: | |
| 142 | + set -uo pipefail |
| 143 | + for _ in $(seq 1 30); do |
| 144 | + REF=$(curl -fsS -H 'Cache-Control: no-cache' "https://repo.packagist.org/p2/$PACKAGE.json" \ |
| 145 | + | jq -r --arg p "$PACKAGE" --arg v "$TAG" '.packages[$p][] | select(.version == $v) | .source.reference // empty') |
| 146 | + if [ "$REF" = "$SHA" ]; then |
| 147 | + echo "$PACKAGE $TAG is on Packagist from $SHA" |
| 148 | + exit 0 |
| 149 | + elif [ -n "$REF" ]; then |
| 150 | + echo "::error::Packagist serves $TAG from $REF, not from $SHA. Check https://packagist.org/packages/$PACKAGE" |
| 151 | + exit 1 |
| 152 | + fi |
| 153 | + sleep 30 |
| 154 | + done |
| 155 | + echo "::error::Packagist has not served $PACKAGE $TAG after 15 minutes. Check the GitHub hook (Settings > Webhooks) and https://packagist.org/packages/$PACKAGE, then press Update there." |
| 156 | + exit 1 |
| 157 | +
|
| 158 | + - name: Summary |
| 159 | + env: |
| 160 | + TAG: ${{ github.event.release.tag_name }} |
| 161 | + SHA: ${{ steps.tag.outputs.sha }} |
| 162 | + API: ${{ vars.CONVERT_API_BASE || 'https://v2.convertapi.com/' }} |
| 163 | + run: | |
| 164 | + { |
| 165 | + echo "| | |" |
| 166 | + echo "|---|---|" |
| 167 | + echo "| Package | [\`$PACKAGE $TAG\`](https://packagist.org/packages/$PACKAGE#$TAG) |" |
| 168 | + echo "| Commit | \`$SHA\` |" |
| 169 | + echo "| Tested against | $API (PHP 8.5) |" |
| 170 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments