Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

feat: use review-apps endpoint #16

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
86 changes: 19 additions & 67 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 }}" | \
Expand Down