From a250c3621eca0a587494cedde908f8d731247dca Mon Sep 17 00:00:00 2001 From: Peter Mbanugo Date: Tue, 10 May 2022 14:31:55 +0200 Subject: [PATCH] feat: use review-apps endpoint BREAKING CHANGE: switched to using the review-apps endpoint which requires a different set of parameters. The workflow logic has been rewritten and is not compatible with previous versions. --- README.md | 25 +++++++--------- action.yaml | 86 ++++++++++++----------------------------------------- 2 files changed, 30 insertions(+), 81 deletions(-) diff --git a/README.md b/README.md index 679f39d..1d25c3b 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,6 @@ This GitHub action manages Heroku review apps for your software delivery pipeline. This was born out of the frustration of not being able to use review apps for a long time and there's no timeline when Heroku will have this sorted. See [status.heroku.com/incidents/2413](https://status.heroku.com/incidents/2413) for more info. -This mimics 💯 how review apps work with the native Heroku integration. - ### Features - ⚙️ Focuses on automated preview deploy. You get the same automated preview deploy experience for your PRs. @@ -34,9 +32,8 @@ jobs: with: api-key: ${{ secrets.HEROKU_API_KEY }} pipeline-id: ${{ secrets.HEROKU_PIPELINE_ID }} - app-name-prefix: getting-started - region: us - use-app-json: true # Use this if your app contains app.json in the root directory + base-name: getting-started + file-glob: "* .prettierrc" # optional - name: Get the review app url # Use the output from the `deploy` step run: echo "The URL is ${{ steps.deploy.outputs.url }}" - name: Comment on commit # Optional: You can use any action to comment on the PR @@ -52,18 +49,18 @@ jobs: The action expects a few input parameters which are defined below. -- app-name-prefix (optional): Prefix for the app. This is should generally be the name of the pipeline e.g `airtable` prefix will produce `soludo-pr-PR_NUMBER.herokuapp.com`. -- api-key (required): Your Heroku API key -- pipeline-id (required): The id of the pipeline to deploy the review app to. -- region (optional): The region to deploy to. For example `eu` or `us`. Default: `eu` -- stack (optional): The Heroku stack to deploy and build with e.g heroku-18. Default: `heroku-20`. If the input `use-app-json` is `true`, the stack will be determined by what's in `app.json`, and if not present, the default on Heroku will be used. -- use-app-json (optional): Set up the initial build using the `app.json` in the root directory. Default: `false`. +- **api-key:** _(required)_ Your Heroku API key. +- **pipeline-id:** _(required)_ The id of the pipeline for the review apps. +- **files-glob:** _(Optional - Default `*`)_ The glob pattern for files to include in the deployment. +- **base-name:** _(required)_ The prefix used to generate review app name. This should be the same as what you specified for the review app URL pattern in Heroku Dashboard. + +## Difference between v1.x & v2.x -## What's next? +Earlier version from v1.0 created apps and placed them into the pipeline as a review app. This worked ok because you could control the review app name. However, it came at the cost of not being able to automatically use the config variables defined in the Review Apps settings. The workaround was to use something like what you see in this [page](https://help.heroku.com/RVEKYMZQ/how-to-copy-staging-dev-production-application-config-variable-to-review-app-config-vars). -I used a similar code for my private workflow since the removal of GitHub integration from Heroku. I decided to release this seeing that there's no clear indication as to when it'll be sorted. There are some edge cases that should be handled (proper error handling) but at the current release is not being done. I'll be making improvements in the coming days/weeks until Heroku restores their service (or until the community no longer needs this). +In order to utilise the config vars defined for review apps, I decided to use the _/review-apps_ Heroku API endpoint for v2 of this action. There are less things to configure, therefore the inputs are different and caused a breaking change. Both versions are stable and serves different use-cases. Bug fixes will still be treated for both, but with the most focus on v2. -This action works well with most cases at the moment. If something is broken or you would extra features or configuration, feel free to create an issue. +If you use v1.x and want to see the README for v1.x, switch to the v1.x [branch](/tree/v1.x). ## Sponsor diff --git a/action.yaml b/action.yaml index b531ecd..089f17a 100644 --- a/action.yaml +++ b/action.yaml @@ -2,28 +2,19 @@ name: "Manage Heroku Review Apps" description: Use Heroku API to replicate the review app integration author: Peter Mbanugo (@pmbanugo - GitHub) inputs: - app-name-prefix: - description: "Prefix for the app. This is can be the name of the pipeline e.g vercel prefix will produce vercel-pr-PR_NUMBER.herokuapp.com" - required: false - default: "" api-key: description: "Your Heroku API key" required: true + files-glob: + default: "*" + description: "The glob pattern for files to include in the deployment" + required: false pipeline-id: description: "The id of the pipeline to deploy review app for" required: true - region: - description: "Region to deploy. eu or us" - default: "eu" - required: false - stack: - description: "Heroku stack to deploy to e.g heroku-18. Default: heroku-20" - default: "heroku-20" - required: false - use-app-json: - description: "Set up the initial build using app.json. For more info: https://devcenter.heroku.com/articles/setting-up-apps-using-the-heroku-platform-api" - default: "false" - required: false + base-name: + description: "The prefix used to generate review app name. This should be the same as what you specified for the review app URL pattern in Heroku Dashboard" + required: true outputs: url: description: "The URL for the app" @@ -36,90 +27,51 @@ runs: steps: - name: Generate app name shell: bash - env: - APP_PREFIX: ${{ inputs.app-name-prefix }} run: | - #!/bin/bash - - if [[ $APP_PREFIX == '' ]]; then - echo "using default prefix" - echo "APP_NAME=review-actions-pr-${{ github.event.number }}" >> $GITHUB_ENV - else - echo "using app prefix" - echo "APP_NAME=${{ inputs.app-name-prefix }}-pr-${{ github.event.number }}" >> $GITHUB_ENV - fi + echo "APP_NAME=${{ inputs.base-name }}-pr-${{ github.event.number }}" >> $GITHUB_ENV - name: Create Source Endpoint if: ${{ github.event_name == 'pull_request' && github.event.action != 'closed' }} id: source_endpoint shell: bash run: | - echo ::set-output name=SOURCE_ENDPOINT::$(curl -X POST https://api.heroku.com/sources \ + export RESPONSE=$(curl -X POST https://api.heroku.com/sources \ -H "Content-Type: application/json" \ -H "Accept: application/vnd.heroku+json; version=3" \ - -H "Authorization: Bearer ${{ inputs.api-key }}" | \ - jq -r '{get: .source_blob.get_url, put: .source_blob.put_url}') + -H "Authorization: Bearer ${{ inputs.api-key }}") + echo ::set-output name=SOURCE_ENDPOINT::$(echo $RESPONSE | jq -r '{get: .source_blob.get_url, put: .source_blob.put_url}') - name: Compress Source Code if: ${{ github.event_name == 'pull_request' && github.event.action != 'closed' }} shell: bash run: | - tar -czvf source.tar.gz * + tar -czvf source.tar.gz ${{ inputs.files-glob }} - name: Upload Source Code if: ${{ github.event_name == 'pull_request' && github.event.action != 'closed' }} shell: bash - env: - SOURCE_ENDPOINT_GET: ${{ steps.source_endpoint_env.outputs.SOURCE_ENDPOINT_GET }} run: | export URL=$(echo ${{ toJSON(steps.source_endpoint.outputs.SOURCE_ENDPOINT) }} | jq -r '.put') curl $URL -X PUT -H 'Content-Type:' --data-binary @source.tar.gz - name: Create App - if: ${{ github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'reopened') && inputs.use-app-json == 'false' }} - shell: bash - run: | - curl -X POST https://api.heroku.com/apps \ - -d '{ - "name": "'"$APP_NAME"'", - "region": "${{ inputs.region }}", - "stack": "${{ inputs.stack }}" - }' \ - -H "Content-Type: application/json" \ - -H "Accept: application/vnd.heroku+json; version=3" \ - -H "Authorization: Bearer ${{ inputs.api-key }}" - - name: "Create App (Setup with app.json)" - if: ${{ github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'reopened') && inputs.use-app-json == 'true' }} + if: ${{ github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'reopened') }} shell: bash run: | export SOURCE_GET_URL=$(echo ${{ toJSON(steps.source_endpoint.outputs.SOURCE_ENDPOINT) }} | jq -r '.get') - curl -X POST https://api.heroku.com/app-setups \ + curl -X POST https://api.heroku.com/review-apps \ -d '{ "source_blob": {"url": "'"$SOURCE_GET_URL"'", "version": "'"${{ github.event.pull_request.head.sha }}"'"}, - "app": { - "name": "'"$APP_NAME"'", - "region": "${{ inputs.region }}" - } - }' \ - -H "Content-Type: application/json" \ - -H "Accept: application/vnd.heroku+json; version=3" \ - -H "Authorization: Bearer ${{ inputs.api-key }}" - - name: Update Pipeline - if: ${{ github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'reopened') }} - shell: bash - run: | - curl -X POST https://api.heroku.com/pipeline-couplings \ - -d '{ - "app": "'"$APP_NAME"'", - "pipeline": "'"${{ inputs.pipeline-id }}"'", - "stage": "review" + "branch": "${{ github.head_ref }}", + "pipeline": "${{ inputs.pipeline-id }}", + "pr_number": ${{ github.event.number }} }' \ -H "Content-Type: application/json" \ -H "Accept: application/vnd.heroku+json; version=3" \ -H "Authorization: Bearer ${{ inputs.api-key }}" - name: Create Build - if: ${{ github.event_name == 'pull_request' && (github.event.action == 'synchronize' || ((github.event.action == 'opened' || github.event.action == 'reopened') && inputs.use-app-json == 'false')) }} + if: ${{ github.event_name == 'pull_request' && github.event.action == 'synchronize' }} shell: bash run: | export SOURCE_GET_URL=$(echo ${{ toJSON(steps.source_endpoint.outputs.SOURCE_ENDPOINT) }} | jq -r '.get') export OUTPUT_STREAM_URL=$(curl -X POST https://api.heroku.com/apps/$APP_NAME/builds \ - -d '{"source_blob":{"url":"'"$SOURCE_GET_URL"'", "version": "'"${{ github.event.pull_request.head.sha }}"'"}}' \ + -d '{"source_blob":{"url":"'"$SOURCE_GET_URL"'", "version": "${{ github.event.pull_request.head.sha }}"}}' \ -H 'Accept: application/vnd.heroku+json; version=3' \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${{ inputs.api-key }}" | \