-
Notifications
You must be signed in to change notification settings - Fork 415
183 lines (170 loc) · 7.63 KB
/
deploy-draft.yml
File metadata and controls
183 lines (170 loc) · 7.63 KB
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
name: Deploy branch draft on Netlify
on:
pull_request:
types: [labeled, opened, synchronize]
workflow_dispatch:
concurrency:
group: ${{ github.head_ref }}
cancel-in-progress: true
jobs:
create-deployment:
if: |
(
(github.event.action == 'labeled' && github.event.label.name == 'deploy') ||
(contains(github.event.pull_request.labels.*.name, 'deploy'))
) && !github.event.pull_request.head.repo.fork
runs-on: ubuntu-22.04
outputs:
deployment-id: ${{ steps.create-deployment.outputs.deployment-id }}
steps:
- name: create deployment
id: create-deployment
run: |
deployment_id=`gh api \
--method POST \
/repos/EnterpriseDB/docs/deployments \
-f "ref=${{ github.event.pull_request.head.sha }}" \
-f "description=Deploy draft on Netlify" \
-F 'required_contexts[]=null' \
-F "auto_merge=false" \
-f "environment=deploy-preview" --jq '.id'`
if [ -z "$deployment_id" ]; then
echo "::error::Failed to create deployment"
exit 1
fi
echo "deployment-id=$deployment_id" >> "$GITHUB_OUTPUT"
gh api \
--method POST \
/repos/EnterpriseDB/docs/deployments/$deployment_id/statuses \
-f "state=in_progress" \
-f "environment_url=${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" \
-f "description=Building pages ${{ (github.event.pull_request && contains(github.event.pull_request.labels.*.name, 'deploy-pdfs')) && 'and PDFs' || '' }}" \
-F "auto_inactive=false"
env:
GH_TOKEN: ${{ github.token }}
build-pages:
needs: create-deployment
uses: ./.github/workflows/build-pages.yml
with:
ref: ${{ github.head_ref }}
sha: ${{ github.event.pull_request.head.sha }}
secrets: inherit
build-pdfs:
needs: create-deployment
if: |
(
(github.event.action == 'labeled' && github.event.label.name == 'deploy-pdfs') ||
(contains(github.event.pull_request.labels.*.name, 'deploy-pdfs'))
) && !github.event.pull_request.head.repo.fork
uses: ./.github/workflows/build-pdfs.yml
with:
ref: ${{ github.head_ref }}
sha: ${{ github.event.pull_request.head.sha }}
secrets: inherit
# this serves to allow building PDFs to be optional without preventing deployment,
# while still failing the workflow if we *try* to build PDFs and fail
# there are ways to do this in conditions on a single job, but this keeps things clear
# this job will fail only if build-pdfs failed (not if it was skipped)
# however, it will set an output - check-has-pdfs.steps.check.has-pdfs - true if PDFs were generated
check-has-pdfs:
runs-on: ubuntu-22.04
needs: [build-pdfs, create-deployment]
if: ${{ !cancelled() && needs.create-deployment.result != 'skipped' }}
outputs:
cache-key: ${{ steps.check.outputs.cache-key }}
has-pdfs: ${{ steps.check.outputs.has-pdfs }}
steps:
- name: check
id: check
run: |
echo "cache-key=${{ needs.build-pdfs.outputs.cache-key }}" >> "$GITHUB_OUTPUT"
echo "has-pdfs=${{ needs.build-pdfs.result == 'success' }}" >> "$GITHUB_OUTPUT"
exit ${{ needs.build-pdfs.result == 'failure' && 1 || 0 }}
update-deployment-status:
needs: [build-pages, check-has-pdfs, create-deployment]
# succeeded() (the default check) will be false at this point if PDFs were skipped
if: ${{ !cancelled() && needs.build-pages.result == 'success' && needs.check-has-pdfs.result == 'success' }}
runs-on: ubuntu-22.04
steps:
- name: update deployment status
run: |
gh api \
--method POST \
/repos/EnterpriseDB/docs/deployments/${{ needs.create-deployment.outputs.deployment-id }}/statuses \
-f "state=in_progress" \
-f "environment_url=${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" \
-f "description=Deploying to Netlify" \
-F "auto_inactive=false"
env:
GH_TOKEN: ${{ github.token }}
deploy-to-netlify:
needs: [build-pages, check-has-pdfs]
# succeeded() (the default check) will be false at this point if PDFs were skipped
if: ${{ !cancelled() && needs.build-pages.result == 'success' && needs.check-has-pdfs.result == 'success' }}
uses: ./.github/workflows/deploy-to-netlify.yml
with:
sha: ${{ github.sha }}
pages-cache-key: ${{ needs.build-pages.outputs.cache-key }}
pdf-cache-key: ${{ needs.check-has-pdfs.outputs.cache-key }}
enable-commit-comment: false
alias: deploy-preview-${{ github.event.number }}
secrets:
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_DEVELOP_SITE_ID }}
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
update-pull-request:
needs: deploy-to-netlify
if: ${{ !cancelled() && needs.deploy-to-netlify.result == 'success' && !!github.event.pull_request.number }}
runs-on: ubuntu-22.04
steps:
- name: pull tools repo
uses: actions/checkout@v3
- name: compose subdomain
run: |
subdomain="deploy-preview-${{ github.event.pull_request.number }}--edb-docs-staging"
echo "SUBDOMAIN=$subdomain" >> $GITHUB_ENV
- name: Update pull request with deployment info
uses: ./.github/actions/update-description
with:
pr-number: ${{ github.event.pull_request.number }}
repo: docs
repo-owner: EnterpriseDB
github-token: ${{ github.token }}
message: |
> [!TIP]
> Draft deployment: https://${{ env.SUBDOMAIN }}.netlify.app/docs/
finalize-deployment-status:
needs: [check-has-pdfs, build-pages, deploy-to-netlify, create-deployment]
# only need to update the deployment status if we actually created a deployment
if: always()
runs-on: ubuntu-22.04
steps:
- name: compose deployment status
run: |
if [ "${{ needs.check-has-pdfs.result == 'success' && needs.build-pages.result == 'success' && needs.deploy-to-netlify.result }}" == "success" ]; then
state=success
subdomain="deploy-preview-${{ github.event.pull_request.number }}--edb-docs-staging"
log_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
environment_url="https://${subdomain}.netlify.app/docs/"
elif [ -z "${{ (needs.check-has-pdfs.result == 'failure' || needs.build-pages.result == 'failure' || needs.deploy-to-netlify.result == 'failure') && '' }}" ]; then
state=failure
log_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
environment_url=""
else
state=inactive
log_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
environment_url=""
fi
echo "state=$state" >> $GITHUB_ENV
echo "log_url=$log_url" >> $GITHUB_ENV
echo "environment_url=$environment_url" >> $GITHUB_ENV
- name: update deployment status
if: ${{ needs.create-deployment.result == 'success' }}
run: |
gh api \
--method POST \
/repos/EnterpriseDB/docs/deployments/${{ needs.create-deployment.outputs.deployment-id }}/statuses \
-f "state=${{ env.state }}" \
-f "log_url=${{ env.log_url }}" \
-f "environment_url=${{ env.environment_url }}" \
env:
GH_TOKEN: ${{ github.token }}