forked from pmbanugo/heroku-review-app-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yaml
153 lines (152 loc) · 6.32 KB
/
action.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
name: "Heroku Review App Action"
description: Use Heroku API to replicate the review app integration
author: Original - @pmbanugo / Modified by YNAB
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
team:
description: "The Heroku team the app belongs to."
required: true
outputs:
url:
description: "The URL for the app"
value: ${{ steps.output-url.outputs.url }}
app_name:
description: "The Heroku review app name"
value: ${{ steps.output-app_name.outputs.app_name }}
branding:
icon: "play-circle"
color: "purple"
runs:
using: "composite"
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
- name: Create Source Endpoint
if: ${{ github.event_name == 'pull_request' && github.event.action != 'closed' }}
id: source_endpoint
shell: bash
run: |
echo 'Creating a source endpoint...'
SOURCE_ENDPOINT=$(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}')
echo ::set-output name=SOURCE_ENDPOINT::$SOURCE_ENDPOINT
echo "SOURCE_ENDPOINT=$SOURCE_ENDPOINT"
- name: Compress Source Code
if: ${{ github.event_name == 'pull_request' && github.event.action != 'closed' }}
shell: bash
run: |
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 Team App (Setup with app.json)"
if: ${{ github.event_name == 'pull_request' }}
shell: bash
run: |
echo 'Create Team App (Setup with app.json)'
export SOURCE_GET_URL=$(echo ${{ toJSON(steps.source_endpoint.outputs.SOURCE_ENDPOINT) }} | jq -r '.get')
curl -X POST https://api.heroku.com/app-setups \
-d '{
"source_blob": {"url": "'"$SOURCE_GET_URL"'", "version": "'"${{ github.event.pull_request.head.sha }}"'"},
"app": {
"name": "'"$APP_NAME"'",
"region": "${{ inputs.region }}",
"organization": "${{ inputs.team }}"
}
}' \
-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' }}
shell: bash
run: |
curl -X POST https://api.heroku.com/pipeline-couplings \
-d '{
"app": "'"$APP_NAME"'",
"pipeline": "'"${{ inputs.pipeline-id }}"'",
"stage": "review"
}' \
-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'))) }}
shell: bash
run: |
echo 'Create Build'
export SOURCE_GET_URL=$(echo ${{ toJSON(steps.source_endpoint.outputs.SOURCE_ENDPOINT) }} | jq -r '.get')
export NEW_BUILD_OUTPUT=$(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 }}"'"}}' \
-H 'Accept: application/vnd.heroku+json; version=3' \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${{ inputs.api-key }}")
echo "NEW_BUILD_OUTPUT=$NEW_BUILD_OUTPUT"
export NEW_BUILD_ID=$(echo $NEW_BUILD_OUTPUT | jq -r '.id')
export OUTPUT_STREAM_URL=$(echo $NEW_BUILD_OUTPUT | jq -r '.output_stream_url')
echo "OUTPUT_STREAM_URL=$OUTPUT_STREAM_URL"
echo "Getting build output..."
curl --silent $OUTPUT_STREAM_URL
NEW_BUILD_STATUS=$(curl --silent https://api.heroku.com/apps/$APP_NAME/builds/$NEW_BUILD_ID \
-H 'Accept: application/vnd.heroku+json; version=3' \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${{ inputs.api-key }}" | \
jq -r '.status')
echo "Build status: $NEW_BUILD_STATUS"
if [ "$NEW_BUILD_STATUS" != "succeeded" ]; then exit 1; fi
- name: Set Output:app_name
id: output-app_name
shell: bash
run: echo "::set-output name=app_name::$APP_NAME"
- name: Set Output:url
id: output-url
shell: bash
run: echo "::set-output name=url::$(echo https://$APP_NAME.herokuapp.com)"
- name: Delete Review App
if: ${{ github.event_name == 'pull_request' && github.event.action == 'closed' }}
shell: bash
run: |
curl -X DELETE https://api.heroku.com/apps/$APP_NAME \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.heroku+json; version=3" \
-H "Authorization: Bearer ${{ inputs.api-key }}"