diff --git a/cloudflare-pages/README.md b/cloudflare-pages/README.md new file mode 100644 index 00000000..9aea3007 --- /dev/null +++ b/cloudflare-pages/README.md @@ -0,0 +1,31 @@ +# Cloudflare Pages Action + +This GitHub Action will create a GitHub deployment from your Cloudflare Pages one, thus triggering Meticulous to run tests. + +To use this Action please create a GitHub Actions workflow `.github/workflows/cloudflare-pages.yaml` with the contents: + +```yaml +name: Trigger Meticulous +on: [push] +permissions: + deployments: write +jobs: + trigger-meticulous: + name: Trigger Meticulous + runs-on: ubuntu-latest + steps: + - name: Trigger Meticulous + uses: alwaysmeticulous/report-diffs-action/cloudflare-pages@v1 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + cloudflare-api-token: ${{ secrets.CLOUDFLARE_API_TOKEN }} + cloudflare-account-id: << FILL THIS IN >> + cloudflare-project-name: << FILL THIS IN >> +``` + +You should pass in: + +- `github-token`: This is automatically created for your workflow by GitHub but still needs to be passed in to our step as shown above. +- `cloudflare-api-token`: This should be created as documented [here](https://developers.cloudflare.com/pages/configuration/api/) with the `Read` permission for `Cloudflare Pages` and stored in a [GitHub Actions secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions#creating-secrets-for-a-repository) named `CLOUDFLARE_API_TOKEN`. Then it can be passed into our step as shown above. +- `cloudflare-account-id`: You can find this by following the instructions [here](https://developers.cloudflare.com/fundamentals/setup/find-account-and-zone-ids/#find-account-id-workers-and-pages), then paste it into the workflow file (it is not confidential). +- `cloudflare-project-name`: You can find this on the same dashboard your account ID is on, then paste it into the workflow file (it is not confidential). diff --git a/cloudflare-pages/action.yml b/cloudflare-pages/action.yml new file mode 100644 index 00000000..16d4dc1e --- /dev/null +++ b/cloudflare-pages/action.yml @@ -0,0 +1,65 @@ +name: 'Meticulous - Create a GitHub deployment from a Cloudflare Pages deployment' +description: 'Create a GitHub deployment from a Cloudflare Pages deployment thus triggering Meticulous tests' +inputs: + github-token: + description: 'A GitHub Actions token (should have write permission on deployments)' + required: true + cloudflare-api-token: + description: 'API token for Cloudflare (should have Read permission for Cloudflare Pages)' + required: true + cloudflare-account-id: + description: 'Account ID for Cloudflare Pages (from the Workers & Pages section of the dashboard)' + required: true + cloudflare-project-name: + description: 'Name of your Cloudflare Pages project (from the Workers & Pages section of the dashboard)' + required: true + sleep-seconds: + description: 'Time to sleep before starting to poll deployment status (in seconds)' + required: false + default: "300" + deployment-poll-seconds: + description: 'Time to wait between API calls to poll for the deployment to be ready (in seconds)' + required: false + default: "30" + wait-till-ready-seconds: + description: 'Maximum time to wait for the deployment to be ready (in seconds)' + required: false + default: "3600" +runs: + using: "composite" + steps: + - name: Sleep a bit to wait for the deployment to exist + if: ${{ inputs.sleep-time != '0' }} + shell: bash + run: sleep ${{ inputs.sleep-time }} + - name: Wait for deployment to be ready and get URL + shell: bash + id: get-deployment + run: | + STATUS="unknown" + LAST_RESULT="" + SLEPT=0 + while [[ "$STATUS" != "success" ]]; do + echo "Checking deployment status..." + LAST_RESULT=$(curl -s -X GET "https://api.cloudflare.com/client/v4/accounts/${{ inputs.cloudflare-account-id }}/pages/projects/${{ inputs.cloudflare-project-name }}/deployments" -H "Authorization: Bearer ${{ inputs.cloudflare-api-token }}" -H "Content-Type:application/json") + STATUS=$(echo "$LAST_RESULT" | jq -c 'first(.result[] | select(.deployment_trigger.metadata.commit_hash == "${{ github.sha }}") | .latest_stage.status)' --raw-output | tr -d '\n') + if [[ "$STATUS" != "success" ]]; then + if [[ $SLEPT -gt ${{ inputs.wait-till-ready-seconds }} ]]; then + echo "Deployment is still not ready, failing..." + exit 1 + fi + echo "Deployment is not ready yet, sleeping..." + sleep ${{ inputs.deployment-poll-seconds }} + SLEPT=$((SLEPT+${{ inputs.deployment-poll-seconds }})) + fi + done + echo "Deployment is ready!" + echo "url=`echo "$LAST_RESULT" | jq -c 'first(.result[] | select(.deployment_trigger.metadata.commit_hash == "${{ github.sha }}") | .url)' --raw-output | tr -d '\n'`" >> $GITHUB_OUTPUT + echo "environment=`echo "$LAST_RESULT" | jq -c 'first(.result[] | select(.deployment_trigger.metadata.commit_hash == "${{ github.sha }}") | .environment)' --raw-output | tr -d '\n'`" >> $GITHUB_OUTPUT + - name: Create GitHub deployment from Cloudflare Pages deployment + uses: altinukshini/deployment-action@releases/v1 + with: + token: "${{ inputs.github-token }}" + target_url: "${{ steps.get-deployment.outputs.url }}" + initial_status: "success" + environment: "Cloudflare Pages: ${{ inputs.cloudflare-project-name }} (${{ steps.get-deployment.outputs.environment }})"