diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b695029..5940df8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -228,3 +228,12 @@ jobs: env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: gh workflow run update-pages.yml || true + + + push-to-wordpress: + needs: release + if: ${{ needs.release.result == 'success' && (github.event_name != 'workflow_dispatch' || inputs.dry_run != true) }} + uses: ./.github/workflows/push-releases.yml + secrets: + WP_PUSH_TOKEN: ${{ secrets.WP_PUSH_TOKEN }} + WP_SITE_URL: ${{ secrets.WP_SITE_URL }} diff --git a/.github/workflows/push-releases.yml b/.github/workflows/push-releases.yml new file mode 100644 index 0000000..81666b2 --- /dev/null +++ b/.github/workflows/push-releases.yml @@ -0,0 +1,57 @@ +name: Push releases to WordPress + +on: + workflow_call: + secrets: + WP_PUSH_TOKEN: + required: true + WP_SITE_URL: + required: true + +jobs: + push: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - name: Validate required secrets + env: + WP_TOKEN: ${{ secrets.WP_PUSH_TOKEN }} + WP_URL: ${{ secrets.WP_SITE_URL }} + run: | + test -n "$WP_TOKEN" || { echo "Missing WP_PUSH_TOKEN"; exit 1; } + test -n "$WP_URL" || { echo "Missing WP_SITE_URL"; exit 1; } + + - name: Push releases to WordPress (with retry) + env: + GH_TOKEN: ${{ github.token }} + WP_TOKEN: ${{ secrets.WP_PUSH_TOKEN }} + WP_URL: ${{ secrets.WP_SITE_URL }} + REPO: ${{ github.repository }} + run: | + set -euo pipefail + + attempt() { + gh api "repos/$REPO/releases/latest" -H "Accept: application/vnd.github+json" > /tmp/latest.json 2>/dev/null || echo "null" > /tmp/latest.json + gh api "repos/$REPO/releases?per_page=20" -H "Accept: application/vnd.github+json" > /tmp/all.json + + PAYLOAD=$(jq -c -n \ + --arg repo "$REPO" \ + --slurpfile all /tmp/all.json \ + --slurpfile latest /tmp/latest.json \ + '{repo:$repo, releases:($all[0][0:20]), latest:(if ($latest[0] | type)=="object" and ($latest[0].tag_name != null) then $latest[0] else null end)}') + + RESPONSE=$(curl -sS -f \ + -H "Content-Type: application/json" \ + -H "X-OGR-Token: $WP_TOKEN" \ + -X POST \ + -d "$PAYLOAD" \ + "${WP_URL%/}/wp-json/ogr/v1/push-releases") + + echo "$RESPONSE" | jq -r '"Pushed \(.count) releases for \(.repo)"' + } + + attempt || { + echo "First push attempt failed, retrying in 10s..." + sleep 10 + attempt + } diff --git a/.github/workflows/update-wordpress.yml b/.github/workflows/update-wordpress.yml index 71b625c..f601430 100644 --- a/.github/workflows/update-wordpress.yml +++ b/.github/workflows/update-wordpress.yml @@ -3,10 +3,11 @@ name: Update WordPress on release on: release: types: [published] + workflow_dispatch: jobs: push-to-wordpress: - uses: Orinks/orinks-github-releases/.github/workflows/push-releases.yml@master + uses: ./.github/workflows/push-releases.yml secrets: WP_PUSH_TOKEN: ${{ secrets.WP_PUSH_TOKEN }} WP_SITE_URL: ${{ secrets.WP_SITE_URL }}