|
| 1 | +name: Build, test & measure |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - develop |
| 7 | + # Include all release branches. |
| 8 | + - '*.*' |
| 9 | + pull_request: |
| 10 | + # Run workflow whenever a PR is opened, updated (synchronized), or marked ready for review. |
| 11 | + types: [opened, synchronize, ready_for_review] |
| 12 | + |
| 13 | +jobs: |
| 14 | + |
| 15 | + dev-zip: |
| 16 | + name: Build dev build ZIP and upload as GHA artifact |
| 17 | + # Only run if it is not a draft PR and the PR is not from a forked repository. |
| 18 | + if: github.event.pull_request.draft == false && github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name |
| 19 | + runs-on: ubuntu-latest |
| 20 | + outputs: |
| 21 | + branch-name: ${{ steps.retrieve-branch-name.outputs.branch_name }} |
| 22 | + git-sha-8: ${{ steps.retrieve-git-sha-8.outputs.sha8 }} |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Check out source files |
| 26 | + uses: actions/checkout@v2 |
| 27 | + |
| 28 | + - name: Get Composer Cache Directory |
| 29 | + id: composer-cache |
| 30 | + run: | |
| 31 | + echo "::set-output name=dir::$(composer config cache-files-dir)" |
| 32 | +
|
| 33 | + - name: Configure Composer cache |
| 34 | + uses: actions/cache@v1 |
| 35 | + with: |
| 36 | + path: ${{ steps.composer-cache.outputs.dir }} |
| 37 | + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} |
| 38 | + restore-keys: | |
| 39 | + ${{ runner.os }}-composer- |
| 40 | +
|
| 41 | + - name: Install Composer dependencies |
| 42 | + # Scripts are not ignored as they are needed to apply patches for the |
| 43 | + # `sabberworm/php-css-parser` dependency. |
| 44 | + run: composer install --prefer-dist --optimize-autoloader |
| 45 | + |
| 46 | + - name: Get npm cache directory |
| 47 | + id: npm-cache |
| 48 | + run: | |
| 49 | + echo "::set-output name=dir::$(npm config get cache)" |
| 50 | +
|
| 51 | + - name: Configure npm cache |
| 52 | + uses: actions/cache@v1 |
| 53 | + with: |
| 54 | + path: ${{ steps.npm-cache.outputs.dir }} |
| 55 | + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} |
| 56 | + restore-keys: | |
| 57 | + ${{ runner.os }}-node- |
| 58 | +
|
| 59 | + - name: Install Node dependencies |
| 60 | + # Prevent malicious scripts from being run with `--ignore-scripts` |
| 61 | + run: npm install --ignore-scripts |
| 62 | + |
| 63 | + - name: Create destination directories |
| 64 | + run: mkdir -p builds/dev |
| 65 | + |
| 66 | + - name: Build develop version |
| 67 | + run: | |
| 68 | + npm run build:dev |
| 69 | + mv amp.zip builds/dev/amp.zip |
| 70 | +
|
| 71 | + - name: Retrieve branch name |
| 72 | + id: retrieve-branch-name |
| 73 | + run: echo "::set-output name=branch_name::$(REF=${GITHUB_HEAD_REF:-$GITHUB_REF} && echo ${REF#refs/heads/} | sed 's/\//-/g')" |
| 74 | + |
| 75 | + - name: Retrieve git SHA-8 string |
| 76 | + id: retrieve-git-sha-8 |
| 77 | + run: echo "::set-output name=sha8::$(echo ${GITHUB_SHA} | cut -c1-8)" |
| 78 | + |
| 79 | + - name: Upload build as artifact |
| 80 | + uses: actions/upload-artifact@v2 |
| 81 | + with: |
| 82 | + name: amp-${{ steps.retrieve-branch-name.outputs.branch_name }}-${{ steps.retrieve-git-sha-8.outputs.sha8 }}-dev |
| 83 | + path: builds/dev |
| 84 | + |
| 85 | +#----------------------------------------------------------------------------------------------------------------------- |
| 86 | + |
| 87 | + prod-zip: |
| 88 | + name: Build prod build ZIP and upload as GHA artifact |
| 89 | + # Only run if it is not a draft PR and the PR is not from a forked repository. |
| 90 | + if: github.event.pull_request.draft == false && github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name |
| 91 | + runs-on: ubuntu-latest |
| 92 | + outputs: |
| 93 | + branch-name: ${{ steps.retrieve-branch-name.outputs.branch_name }} |
| 94 | + git-sha-8: ${{ steps.retrieve-git-sha-8.outputs.sha8 }} |
| 95 | + |
| 96 | + steps: |
| 97 | + - name: Check out source files |
| 98 | + uses: actions/checkout@v2 |
| 99 | + |
| 100 | + - name: Get Composer Cache Directory |
| 101 | + id: composer-cache |
| 102 | + run: | |
| 103 | + echo "::set-output name=dir::$(composer config cache-files-dir)" |
| 104 | +
|
| 105 | + - name: Configure Composer cache |
| 106 | + uses: actions/cache@v1 |
| 107 | + with: |
| 108 | + path: ${{ steps.composer-cache.outputs.dir }} |
| 109 | + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} |
| 110 | + restore-keys: | |
| 111 | + ${{ runner.os }}-composer- |
| 112 | +
|
| 113 | + - name: Install Composer dependencies |
| 114 | + # Scripts are not ignored as they are needed to apply patches for the |
| 115 | + # `sabberworm/php-css-parser` dependency. |
| 116 | + run: composer install --prefer-dist --optimize-autoloader |
| 117 | + |
| 118 | + - name: Get npm cache directory |
| 119 | + id: npm-cache |
| 120 | + run: | |
| 121 | + echo "::set-output name=dir::$(npm config get cache)" |
| 122 | +
|
| 123 | + - name: Configure npm cache |
| 124 | + uses: actions/cache@v1 |
| 125 | + with: |
| 126 | + path: ${{ steps.npm-cache.outputs.dir }} |
| 127 | + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} |
| 128 | + restore-keys: | |
| 129 | + ${{ runner.os }}-node- |
| 130 | +
|
| 131 | + - name: Install Node dependencies |
| 132 | + # Prevent malicious scripts from being run with `--ignore-scripts` |
| 133 | + run: npm install --ignore-scripts |
| 134 | + |
| 135 | + - name: Create destination directories |
| 136 | + run: mkdir -p builds/prod |
| 137 | + |
| 138 | + - name: Build production version |
| 139 | + run: | |
| 140 | + npm run build:prod |
| 141 | + mv amp.zip builds/prod/amp.zip |
| 142 | +
|
| 143 | + - name: Retrieve branch name |
| 144 | + id: retrieve-branch-name |
| 145 | + run: echo "::set-output name=branch_name::$(REF=${GITHUB_HEAD_REF:-$GITHUB_REF} && echo ${REF#refs/heads/} | sed 's/\//-/g')" |
| 146 | + |
| 147 | + - name: Retrieve git SHA-8 string |
| 148 | + id: retrieve-git-sha-8 |
| 149 | + run: echo "::set-output name=sha8::$(echo ${GITHUB_SHA} | cut -c1-8)" |
| 150 | + |
| 151 | + - name: Upload build as artifact |
| 152 | + uses: actions/upload-artifact@v2 |
| 153 | + with: |
| 154 | + name: amp-${{ steps.retrieve-branch-name.outputs.branch_name }}-${{ steps.retrieve-git-sha-8.outputs.sha8 }}-prod |
| 155 | + path: builds/prod |
| 156 | + |
| 157 | +#----------------------------------------------------------------------------------------------------------------------- |
| 158 | + |
| 159 | + upload-to-gcs: |
| 160 | + name: Upload plugin ZIPs to Google Cloud Storage |
| 161 | + runs-on: ubuntu-latest |
| 162 | + needs: |
| 163 | + - dev-zip |
| 164 | + - prod-zip |
| 165 | + steps: |
| 166 | + - name: Download dev build |
| 167 | + uses: actions/download-artifact@v2 |
| 168 | + with: |
| 169 | + name: amp-${{ needs.dev-zip.outputs.branch-name }}-${{ needs.dev-zip.outputs.git-sha-8 }}-dev |
| 170 | + path: builds/dev |
| 171 | + |
| 172 | + - name: Download prod build |
| 173 | + uses: actions/download-artifact@v2 |
| 174 | + with: |
| 175 | + name: amp-${{ needs.prod-zip.outputs.branch-name }}-${{ needs.prod-zip.outputs.git-sha-8 }}-prod |
| 176 | + path: builds/prod |
| 177 | + |
| 178 | + - name: Setup Google Cloud SDK |
| 179 | + uses: GoogleCloudPlatform/github-actions/setup-gcloud@master |
| 180 | + with: |
| 181 | + project_id: ${{ secrets.GCS_PROJECT_ID }} |
| 182 | + service_account_key: ${{ secrets.GCS_APPLICATION_CREDENTIALS }} |
| 183 | + |
| 184 | + - name: Upload dev build to bucket |
| 185 | + run: gsutil cp -r builds/dev/amp.zip gs://ampwp_github_artifacts/${{ github.ref }}/dev/amp.zip |
| 186 | + |
| 187 | + - name: Upload prod build to bucket |
| 188 | + run: gsutil cp -r builds/prod/amp.zip gs://ampwp_github_artifacts/${{ github.ref }}/prod/amp.zip |
| 189 | + |
| 190 | +#----------------------------------------------------------------------------------------------------------------------- |
| 191 | + |
| 192 | + comment-on-pr: |
| 193 | + name: Comment on PR with links to plugin ZIPs |
| 194 | + # Only run this job if it's a PR. One way to check for that is if `github.head_ref` is not empty. |
| 195 | + if: ${{ github.head_ref && github.head_ref != null }} |
| 196 | + runs-on: ubuntu-latest |
| 197 | + needs: upload-to-gcs |
| 198 | + |
| 199 | + outputs: |
| 200 | + pr_number: ${{ steps.get-pr-number.outputs.num }} |
| 201 | + comment_body: ${{ steps.get-comment-body.outputs.body }} |
| 202 | + |
| 203 | + steps: |
| 204 | + - name: Get PR number |
| 205 | + id: get-pr-number |
| 206 | + run: echo "::set-output name=num::$(echo $GITHUB_REF | awk 'BEGIN { FS = "/" } ; { print $3 }')" |
| 207 | + |
| 208 | + - name: Check if a comment was already made |
| 209 | + id: find-comment |
| 210 | + uses: peter-evans/find-comment@v1 |
| 211 | + with: |
| 212 | + issue-number: ${{ steps.get-pr-number.outputs.num }} |
| 213 | + comment-author: github-actions[bot] |
| 214 | + body-includes: Download [development build] |
| 215 | + |
| 216 | + - name: Get comment body |
| 217 | + id: get-comment-body |
| 218 | + # Setting a multi-line output requires escaping line-feeds. See <https://github.community/t/set-output-truncates-multiline-strings/16852/3>. |
| 219 | + run: | |
| 220 | + body="Plugin builds for ${{ github.event.pull_request.head.sha }} are ready :bellhop_bell:! |
| 221 | + - Download [development build](https://storage.googleapis.com/ampwp_github_artifacts/${{ github.ref }}/dev/amp.zip) |
| 222 | + - Download [production build](https://storage.googleapis.com/ampwp_github_artifacts/${{ github.ref }}/prod/amp.zip)" |
| 223 | + body="${body//$'\n'/'%0A'}" |
| 224 | + echo "::set-output name=body::$body" |
| 225 | +
|
| 226 | + - name: Create comment on PR with links to plugin builds |
| 227 | + if: ${{ steps.find-comment.outputs.comment-id == '' }} |
| 228 | + uses: peter-evans/create-or-update-comment@v1 |
| 229 | + with: |
| 230 | + issue-number: ${{ steps.get-pr-number.outputs.num }} |
| 231 | + body: ${{ steps.get-comment-body.outputs.body }} |
| 232 | + |
| 233 | + - name: Update comment on PR with links to plugin builds |
| 234 | + if: ${{ steps.find-comment.outputs.comment-id != '' }} |
| 235 | + uses: peter-evans/create-or-update-comment@v1 |
| 236 | + with: |
| 237 | + comment-id: ${{ steps.find-comment.outputs.comment-id }} |
| 238 | + edit-mode: replace |
| 239 | + body: ${{ steps.get-comment-body.outputs.body }} |
0 commit comments