forked from platformsh/platformsh-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
219 lines (188 loc) · 9.85 KB
/
get-pr-info.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
name: Get info on PR
on:
pull_request_target:
branches: [main]
types: [labeled,opened,reopened,synchronize]
env:
PLATFORMSH_CLI_NO_INTERACTION: 1
PLATFORM_PROJECT: 652soceglkw4u
PLATFORMSH_CLI_DEFAULT_TIMEOUT: 60 # Increase timeout for CLI commands
BRANCH_TITLE: ${{ vars.BFF_PREFIX }}-${{ github.event.number }}
jobs:
get_pr_info:
runs-on: ubuntu-latest
name: Get info on build from PR
# Run only for PRs from default repo and approved PRs from forks
if: github.event.pull_request.head.repo.id == 21975579 || contains(github.event.pull_request.labels.*.name, 'build-fork')
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
- name: Set branch name
id: branch_name
run: |
# For PRs from non-forked repos
if [ ${{ github.event.pull_request.head.repo.id }} == 21975579 ]; then
branch_name="${{ github.event.pull_request.head.ref }}"
# For PRs from forks
else
branch_name="${{ env.BRANCH_TITLE }}"
fi
echo "::notice::Setting branch name to ${branch_name}."
echo "branch_name=${branch_name}" >> $GITHUB_OUTPUT
# Set up workflow environment to use the Platform.sh CLI
- name: Set up Platform.sh CLI
uses: ./.github/actions/set-up-cli
# Get the URL for the deployed environment
- name: Get environment URL
env:
PLATFORMSH_CLI_TOKEN: ${{ secrets.PLATFORMSH_CLI_TOKEN }}
id: get_env_url
run: |
COMMIT_STATUS="pending"
# Get the state of the most recent deployment activity
# run commands in workflows are are automatically launched with shell: /usr/bin/bash -e {0}. If for some
# reason the psh cli errors (e.g. branch hasn't reached psh and therefore doesnt exist, api times out, etc)
# this will cause our workflow to exit. Instead, call the cli with an | true so we can attempt to query again
STATE=$(platform activities --format plain --type "environment.push environment.activate environment.redeploy environment.branch" --no-header --columns "state" --limit 1 --environment ${{ steps.branch_name.outputs.branch_name }} || true)
echo "::notice::Attempting to retrieve deployed environment URL. Current state is: ${STATE}"
# Set tries to 0 to start countdown to timeout
STATUS_TRY=1
# We'll try up to 20 minutes, so (SLEEP_TIME*NUM_ATTEMPTS)/60 = 10*
SLEEP_TIME=10
NUM_ATTEMPTS=120
# Wait until state is complete or 20 minutes has passed
until [ "${STATE}" = "complete" ] || [ ${STATUS_TRY} = ${NUM_ATTEMPTS} ]; do
sleep ${SLEEP_TIME}
echo "::notice::Environment is not deployed. Attempting to retrieve deployed status. Attempt ${STATUS_TRY} of ${NUM_ATTEMPTS}."
STATE=$(platform activities --format plain --type "environment.push environment.activate environment.redeploy environment.branch" --no-header --columns "state" --limit 1 --environment ${{ steps.branch_name.outputs.branch_name }} || true)
echo "::notice::Status retrieved: ${STATE}"
if [ "${STATE}" = "complete" ]; then
echo "::notice::Activity is complete."
fi
# we want to clear the cli cache every 3rd attempt in case it is caching too heavily
if [ $(expr ${STATUS_TRY} % 3) = 0 ]; then
echo "Clearing the cli cache..."
platform clear-cache
fi
STATUS_TRY=$((++STATUS_TRY))
done
# Get the URL of the environment
ENV_URL=$(platform url --primary --pipe --environment ${{ steps.branch_name.outputs.branch_name }})
# Get the status of the build activity
COMMIT_STATUS=$(platform activities --format plain --type "environment.push environment.activate environment.redeploy environment.branch" --no-header --columns "result" --limit 1 --environment ${{ steps.branch_name.outputs.branch_name }})
# Report findings
echo "The Platform.sh environment is:"
echo "$COMMIT_STATUS"
if [ "$COMMIT_STATUS" = "success" ]; then
echo "Environment deployed to:"
echo "$ENV_URL"
fi
# Report the final statuses to be used in future jobs
echo "env_status=$COMMIT_STATUS" >> $GITHUB_OUTPUT
echo "env_url=$ENV_URL" >> $GITHUB_OUTPUT
- name: Make artifact files
run: |
touch environment_url.txt
touch pr_comment.txt
- name: Save PR number
if: ${{ always() }}
run: echo ${{ github.event.number }} > pr_number.txt
- name: Save PR SHA
if: ${{ always() }}
run: echo "${{ github.event.pull_request.head.sha }}" > pr_sha.txt
# If no environment URL, create a report of that
- name: Create failure report
if: steps.get_env_url.outputs.env_status == 'failure'
run: |
echo -e "The environment on Platform.sh failed to deploy. :slightly_frowning_face:\n\nCheck the logs: https://console.platform.sh/projects/652soceglkw4u/${{ steps.branch_name.outputs.branch_name }}" > pr_comment.txt
# Create a list of relevant changed pages
- name: Report environment URL
if: steps.get_env_url.outputs.env_status == 'success'
run: |
echo ${{ steps.get_env_url.outputs.env_url }} > environment_url.txt
- name: Get changed files
if: steps.get_env_url.outputs.env_status == 'success'
id: changed-files
uses: tj-actions/changed-files@v29
with:
sha: ${{ github.event.pull_request.head.sha }}
base_sha: ${{ github.event.pull_request.base.sha }}
# Create a list of relevant changed pages
- name: Get list of changed files
if: steps.get_env_url.outputs.env_status == 'success'
id: get-files
env:
ENV_URL: ${{ steps.get_env_url.outputs.env_url }}
run: |
files=()
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
# Rewrite all Markdown files in the source to be links in the final environment
if [[ $file = "sites/platform/src/"*".md" ]]; then
# Remove file extension
page=${file/.md/.html}
# Remove initial directory
page=${page/"sites/platform/src/"/}
# Shift index pages up a level
page=${page/"/_index.html"/".html"}
files+=("$ENV_URL$page")
fi
done
# If there are changed files, return a list
# Using "\n" here creates problems in passing data to the next step, so replace there instead
if (( ${#files[@]} != 0 )); then
echo $(printf "||%s" "${files[@]}")
echo changed_files=$(printf "||%s" "${files[@]}") >> $GITHUB_OUTPUT
else
echo changed_files="" >> $GITHUB_OUTPUT
fi
# If there are changed pages, create a comment with a checklist
- name: Comment with links
if: steps.get-files.outputs.changed_files != ''
env:
PAGES: ${{ steps.get-files.outputs.changed_files }}
run: |
echo -e "Your Platform.sh environment has successfully deployed. :rocket:\n\nSee the changed pages:\n${PAGES//||/'\n- [ ] '}" > pr_comment.txt
# If there are no changed pages, create a comment
# with a link to general URL
- name: Comment without links
if: steps.get-files.outputs.changed_files == '' && steps.get_env_url.outputs.env_status == 'success'
env:
ENV_URL: ${{ steps.get_env_url.outputs.env_url }}
run: |
echo -e "Your Platform.sh environment has successfully deployed. :rocket:\n\nSee the site:\n$ENV_URL" > pr_comment.txt
- uses: actions/upload-artifact@v2
with:
name: pr-info
path: |
environment_url.txt
pr_comment.txt
pr_number.txt
pr_sha.txt
retention-days: 1
# If environment failed to create, fail the workflow
- name: Fail workflow
env:
PLATFORMSH_CLI_TOKEN: ${{ secrets.PLATFORMSH_CLI_TOKEN }}
if: steps.get_env_url.outputs.env_status != 'success'
run: |
echo "::error::The environment for pull request ${{ github.event.number }} failed to deploy. Please rerun this workflow."
echo "The last status we received was ${{ steps.get_env_url.outputs.env_status }}"
echo "List of activities that we ask for, but not limited to the last one:"
platform activities --type "environment.push environment.activate environment.redeploy environment.branch" --environment ${{ steps.branch_name.outputs.branch_name }}
echo "Now a list of the (up to) last 20 activities for branch ${{ steps.branch_name.outputs.branch_name }}:"
platform activities --environment ${{ steps.branch_name.outputs.branch_name }} --limit 20
# Get the ID of failed activity and output its log:
failedID=$(platform activities --format plain --type "environment.push environment.activate environment.redeploy environment.branch" --no-header --columns "ID" --limit 1 --environment ${{ steps.branch_name.outputs.branch_name }} --result=failure)
echo "Log for the failed activity, ID ${failedID}"
platform activity:log ${failedID} --environment ${{ steps.branch_name.outputs.branch_name }}
exit 1
dont_run_nonlabeled_forks:
name: Warn about non-labeled PRs from forks
if: github.event.pull_request.head.repo.id != 21975579 && !contains(github.event.pull_request.labels.*.name, 'build-fork')
runs-on: ubuntu-latest
steps:
- name: Warn the environment will not be built
run: |
echo "::warning::Pull Requests from forks will not have an environment built until they are given the appropriate label."