|
| 1 | +name: Update phplist-web-frontend OpenAPI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - dev |
| 7 | + - main |
| 8 | + pull_request: |
| 9 | + branches: |
| 10 | + - main |
| 11 | +jobs: |
| 12 | + generate-openapi: |
| 13 | + runs-on: ubuntu-22.04 |
| 14 | + outputs: |
| 15 | + source_branch: ${{ steps.branch.outputs.source_branch }} |
| 16 | + steps: |
| 17 | + - name: Determine source branch |
| 18 | + id: branch |
| 19 | + run: | |
| 20 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 21 | + echo "source_branch=${{ github.head_ref }}" >> "$GITHUB_OUTPUT" |
| 22 | + else |
| 23 | + echo "source_branch=${{ github.ref_name }}" >> "$GITHUB_OUTPUT" |
| 24 | + fi |
| 25 | +
|
| 26 | + - name: Checkout Source Repository |
| 27 | + uses: actions/checkout@v3 |
| 28 | + |
| 29 | + - name: Setup PHP with Composer and Extensions |
| 30 | + uses: shivammathur/setup-php@v2 |
| 31 | + with: |
| 32 | + php-version: 8.1 |
| 33 | + extensions: mbstring, dom, fileinfo, mysql |
| 34 | + |
| 35 | + - name: Cache Composer Dependencies |
| 36 | + uses: actions/cache@v3 |
| 37 | + with: |
| 38 | + path: ~/.composer/cache |
| 39 | + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} |
| 40 | + restore-keys: | |
| 41 | + ${{ runner.os }}-composer- |
| 42 | +
|
| 43 | + - name: Install Composer Dependencies |
| 44 | + run: composer install --no-interaction --prefer-dist |
| 45 | + |
| 46 | + - name: Generate OpenAPI Specification JSON |
| 47 | + run: vendor/bin/openapi -o docs/latest-restapi.json --format json src |
| 48 | + |
| 49 | + - name: Upload OpenAPI Artifact |
| 50 | + uses: actions/upload-artifact@v4 |
| 51 | + with: |
| 52 | + name: openapi-json |
| 53 | + path: docs/latest-restapi.json |
| 54 | + |
| 55 | + update-web-frontend: |
| 56 | + runs-on: ubuntu-22.04 |
| 57 | + needs: generate-openapi |
| 58 | + env: |
| 59 | + TARGET_BRANCH: ${{ needs.generate-openapi.outputs.source_branch }} |
| 60 | + steps: |
| 61 | + - name: Checkout phplist-web-frontend Repository |
| 62 | + uses: actions/checkout@v3 |
| 63 | + with: |
| 64 | + repository: phplist/phplist-web-frontend |
| 65 | + token: ${{ secrets.PUSH_WEB_FRONTEND }} |
| 66 | + fetch-depth: 0 |
| 67 | + |
| 68 | + - name: Prepare target branch |
| 69 | + run: | |
| 70 | + git fetch origin |
| 71 | +
|
| 72 | + if git ls-remote --exit-code --heads origin "$TARGET_BRANCH" >/dev/null 2>&1; then |
| 73 | + git checkout "$TARGET_BRANCH" |
| 74 | + git pull --rebase origin "$TARGET_BRANCH" |
| 75 | + else |
| 76 | + git checkout -b "$TARGET_BRANCH" |
| 77 | + fi |
| 78 | +
|
| 79 | + - name: Download Generated OpenAPI JSON |
| 80 | + uses: actions/download-artifact@v4 |
| 81 | + with: |
| 82 | + name: openapi-json |
| 83 | + path: ./new-openapi |
| 84 | + |
| 85 | + - name: Compare and Check for Differences |
| 86 | + id: diff |
| 87 | + run: | |
| 88 | + # Compare the openapi files if old exists, else always deploy |
| 89 | + if [ -f openapi.json ]; then |
| 90 | + diff openapi.json new-openapi/latest-restapi.json > openapi-diff.txt || true |
| 91 | + if [ -s openapi-diff.txt ]; then |
| 92 | + echo "diff=true" >> "$GITHUB_OUTPUT" |
| 93 | + else |
| 94 | + echo "diff=false" >> "$GITHUB_OUTPUT" |
| 95 | + fi |
| 96 | + else |
| 97 | + echo "No previous openapi.json, will add." |
| 98 | + echo "diff=true" >> "$GITHUB_OUTPUT" |
| 99 | + fi |
| 100 | +
|
| 101 | + - name: Update and Commit OpenAPI File |
| 102 | + if: steps.diff.outputs.diff == 'true' |
| 103 | + run: | |
| 104 | + set -euo pipefail |
| 105 | + cp new-openapi/latest-restapi.json openapi.json |
| 106 | + git config user.name "github-actions" |
| 107 | + git config user.email "github-actions@web-frontend.workflow" |
| 108 | + git add openapi.json |
| 109 | + if git diff --cached --quiet; then |
| 110 | + echo "No changes to commit" |
| 111 | + exit 0 |
| 112 | + fi |
| 113 | + git commit -m "Update openapi.json from web frontend workflow $(date -u +"%Y-%m-%dT%H:%M:%SZ")" |
| 114 | + git fetch origin "$TARGET_BRANCH" |
| 115 | + git rebase "origin/$TARGET_BRANCH" |
| 116 | + git push origin HEAD:"$TARGET_BRANCH" |
| 117 | +
|
| 118 | + - name: Skip Commit if No Changes |
| 119 | + if: steps.diff.outputs.diff == 'false' |
| 120 | + run: echo "No changes to openapi.json, skipping commit." |
0 commit comments