forked from astronomer/deploy-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yaml
331 lines (285 loc) · 13.2 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
name: "Deploy Apache Airflow DAGs to Astro"
description: "Test your DAGs and deploy your Astro project to a Deployment on Astro, Astronomer's managed Airflow service."
branding:
icon: 'upload-cloud'
color: 'purple'
inputs:
root-folder:
required: false
default: ./
description: "Path to to Astro project folder that contains that 'dags' folder"
parse:
required: false
default: false
description: "If true DAGs will be parsed before deploying to Astro."
pytest:
required: false
default: false
description: "if true custom Pytests will be ran before deploying to Astro."
pytest-file:
required: false
description: "Specify custom Pytest files to run with the pytest command."
force:
required: false
default: false
description: "If true your code will be force deployedto Astornomer. Mostly uesd to skip parse test on image deploys."
image-name:
required: false
default: no-custom-image
description: "Specify a custom built image to deploy to an Asto Deployment."
action:
required: false
default: deploy
description: "Specify what action you would like to take. Use this option to create or delete deployment previews. Specify either 'create-deployment-preview', 'delete-deployment-preview' or 'deploy-deployment-preview'. Don't sepcify anything if you are deploying to a regular deployment."
deployment-name:
required: false
default: false
description: "The name of the deployment you want to make preview from or are deploying too."
deployment-id:
required: false
default: false
description: "The id of the deployment you to make a preview from or are deploying too."
workspace:
required: false
default: false
description: "If you are using an organization token you will need to provide a workspace-name or id."
preview-name:
required: false
default: false
description: "Custom preview name. By default this is branch name “_” deployment name."
copy-connections:
required: false
default: true
description: "Copy connections from the original deployment to the new deployment preview."
copy-airflow-variables:
required: false
default: true
descrioption: "Copy Airflow variables from the original deployment to the new deployment preivew."
copy-pools:
required: false
default: true
descrioption: "Copy pools from the original deployment to the new deployment preivew."
outputs:
preview-id:
description: "The ID of the created deployment preview. Only works when action=create-deployment-preview"
value: ${{ steps.deployment-preview.outputs.FINAL_DEPLOYMENT_ID }}
runs:
using: "composite"
steps:
- name: checkout repo
uses: actions/checkout@v3
with:
fetch-depth: 2
clean: false
# Determine if only DAGs have changes
- name: Install Astro
run: |
curl -sSL https://install.astronomer.io | sudo bash -s
shell: bash
- name: Deployment Preview action
run: |
# set action
ACTION=nothing
# switch workspace if specified
if [[ ${{ inputs.workspace }} != false ]]; then
astro workspace switch ${{ inputs.workspace }}
fi
# error if both deployment name and id are you used
if [[ "${{ inputs.deployment-name }}" != false && ${{ inputs.deployment-id }} != false ]]; then
echo ERROR: cannot specify both a Deployment ID and Name
exit 1 # terminate and indicate error
fi
# figure out deployment id
if [[ "${{ inputs.deployment-name }}" != false ]]; then
# get deployment-id
DEPLOYMENT_ID="$(astro deployment inspect --clean-output -n "${{ inputs.deployment-name }}" --key metadata.deployment_id)"
fi
if [[ ${{ inputs.deployment-id }} != false ]]; then
DEPLOYMENT_ID=${{ inputs.deployment-id }}
fi
# create deployment preview if action is create-deployment-preview
if [[ ${{ inputs.action }} == create-deployment-preview ]]; then
if [[ "${{ inputs.deployment-name }}" == false && ${{ inputs.deployment-id }} == false ]]; then
echo ERROR: cannot create a deployment preview without specifying a deployment name or id
exit 1 # terminate and indicate error
fi
if [[ ${{ inputs.preview-name }} != false ]]; then
BRANCH_DEPLOYMENT_NAME=${{ inputs.preview-name }}
else
# get branch name
DEPLOYMENT_NAME="$(astro deployment inspect $DEPLOYMENT_ID --clean-output --key configuration.name)"
if [[ ${GITHUB_HEAD_REF##*/} != "" ]]; then
BRANCH_DEPLOYMENT_NAME=${GITHUB_HEAD_REF##*/}_$DEPLOYMENT_NAME
else
branch_ref="${{ github.ref }}"
branch_name="${branch_ref##*/}"
BRANCH_DEPLOYMENT_NAME=${branch_name}_$DEPLOYMENT_NAME
fi
BRANCH_DEPLOYMENT_NAME="${BRANCH_DEPLOYMENT_NAME// /_}"
echo $BRANCH_DEPLOYMENT_NAME
fi
# Create template of deployment to be copied with
astro deployment inspect $DEPLOYMENT_ID --clean-output --template > deployment-preview-template.yaml # autmatically creates deployment-preview-template.yaml file
# Add name to deployment template file
sed -i "s| name:.*| name: ${BRANCH_DEPLOYMENT_NAME}|g" deployment-preview-template.yaml
# Create new deploymeent preview based on the deployment template file
astro deployment create --deployment-file deployment-preview-template.yaml
# Get final deployment ID
echo "FINAL_DEPLOYMENT_ID=$(astro deployment inspect --clean-output -n $BRANCH_DEPLOYMENT_NAME --key metadata.deployment_id)" >> $GITHUB_OUTPUT
# Get original deployment ID
echo "ORIGINAL_DEPLOYMENT_ID=$DEPLOYMENT_ID" >> $GITHUB_OUTPUT
# don't skip deploy
echo "SKIP_DEPLOY=false" >> $GITHUB_OUTPUT
# image deploy only
echo "IMAGE_DEPLOY_ONLY=true" >> $GITHUB_OUTPUT
# set action
ACTION=create
fi
# delete deployment preview and skip deploy if action is delete-deployment-preview
if [[ ${{ inputs.action }} == delete-deployment-preview ]]; then
if [[ "${{ inputs.deployment-name }}" == false && ${{ inputs.deployment-id }} == false ]]; then
echo ERROR: cannot delete a deployment preview without specifying a deployment name or id
exit 1 # terminate and indicate error
fi
if [[ ${{ inputs.preview-name }} != false ]]; then
BRANCH_DEPLOYMENT_NAME=${{ inputs.preview-name }}
else
DEPLOYMENT_NAME="$(astro deployment inspect $DEPLOYMENT_ID --clean-output --key configuration.name)"
BRANCH_DEPLOYMENT_NAME=${{ github.event.ref }}_$DEPLOYMENT_NAME
BRANCH_DEPLOYMENT_NAME="${BRANCH_DEPLOYMENT_NAME// /_}"
fi
# delete branch deployment
astro deployment delete -n $BRANCH_DEPLOYMENT_NAME -f
# skip deploy
echo "SKIP_DEPLOY=true" >> $GITHUB_OUTPUT
# not image deploy only
echo "IMAGE_DEPLOY_ONLY=false" >> $GITHUB_OUTPUT
# set action
ACTION=delete
fi
# # deploy to deployment preview if action is cdeploy-deployment-preview
if [[ ${{ inputs.action }} == deploy-deployment-preview ]]; then
if [[ "${{ inputs.deployment-name }}" == false && ${{ inputs.deployment-id }} == false ]]; then
echo ERROR: cannot deploy to a deployment preview without specifying a deployment name or id
exit 1 # terminate and indicate error
fi
if [[ ${{ inputs.preview-name }} != false ]]; then
BRANCH_DEPLOYMENT_NAME=${{ inputs.preview-name }}
else
DEPLOYMENT_NAME="$(astro deployment inspect $DEPLOYMENT_ID --clean-output --key configuration.name)"
if [[ ${GITHUB_HEAD_REF##*/} != "" ]]; then
BRANCH_DEPLOYMENT_NAME=${GITHUB_HEAD_REF##*/}_$DEPLOYMENT_NAME
else
# BRANCH_DEPLOYMENT_NAME=${{ github.event.ref }}_$DEPLOYMENT_NAME
branch_ref="${{ github.ref }}"
branch_name="${branch_ref##*/}"
BRANCH_DEPLOYMENT_NAME=${branch_name}_$DEPLOYMENT_NAME
fi
BRANCH_DEPLOYMENT_NAME="${BRANCH_DEPLOYMENT_NAME// /_}"
fi
# Get deployment ID
echo "FINAL_DEPLOYMENT_ID=$(astro deployment inspect --clean-output -n $BRANCH_DEPLOYMENT_NAME --key metadata.deployment_id)" >> $GITHUB_OUTPUT
# don't skip deploy
echo "SKIP_DEPLOY=false" >> $GITHUB_OUTPUT
# not image deploy only
echo "IMAGE_DEPLOY_ONLY=false" >> $GITHUB_OUTPUT
# set action
ACTION=deploy-preview
fi
# if action is deploy set final deployment id to deployment id
if [[ ${{ inputs.action }} == deploy ]]; then
echo "FINAL_DEPLOYMENT_ID=$DEPLOYMENT_ID" >> $GITHUB_OUTPUT
# don't skip deploy
echo "SKIP_DEPLOY=false" >> $GITHUB_OUTPUT
# not image deploy only
echo "IMAGE_DEPLOY_ONLY=false" >> $GITHUB_OUTPUT
# set action
ACTION=deploy
fi
if [[ $ACTION == nothing ]]; then
echo ERROR: you specfied an improper action input. Action must be deploy, deploy-deployment-preview, create-deployment-preview, or delete-deployment-preview.
exit 1 # terminate and indicate error
fi
shell: bash
id: deployment-preview
- name: Determine if DAG Deploy is enabled
run: |
if [[ ${{steps.deployment-preview.outputs.SKIP_DEPLOY}} == false && ${{steps.deployment-preview.outputs.IMAGE_DEPLOY_ONLY}} == false ]]; then
echo "DAG_DEPLOY_ENABLED=$(astro deployment inspect ${{steps.deployment-preview.outputs.FINAL_DEPLOYMENT_ID}} --clean-output --key configuration.dag_deploy_enabled)" >> $GITHUB_OUTPUT
else
echo "DAG_DEPLOY_ENABLED=false" >> $GITHUB_OUTPUT
fi
shell: bash
id: dag-deploy-enabled
- name: Get Deployment Type
run: |
cd ${{ inputs.root-folder }}
files=$(git diff --name-only HEAD^..HEAD)
dags_only=1
for file in $files; do
if [[ $file != *"dags/"* ]]; then
echo $file is not a dag, triggering a full image build
dags_only=0
break
fi
done
if [[ ${{steps.dag-deploy-enabled.outputs.DAG_DEPLOY_ENABLED}} == false ]]; then
dags_only=0
fi
if [[ ${{steps.deployment-preview.outputs.SKIP_DEPLOY}} == true ]]; then
# skip all deploy steps
dags_only=2
fi
echo "DAGS_ONLY=$dags_only" >> $GITHUB_OUTPUT
shell: bash
id: deployment-type
# If only DAGs changed and dag deploys is enabled, do a DAG-only deploy
- name: setup deploy options
run: |
options=""
# add parse option
if [[ ${{ inputs.parse }} == true ]]; then
options="--parse"
fi
# add pytest option
if [[ ${{ inputs.pytest }} == true ]]; then
options="$options --pytest --test ${{ inputs.pytest-file }}"
fi
# add custom image option
if [[ ${{ inputs.image-name }} != no-custom-image ]]; then
options="$options --image-name ${{ inputs.image-name }}"
fi
# add force option
if [[ ${{ inputs.force }} == true ]]; then
options="$options --force"
fi
echo "OPTIONS=$options" >> $GITHUB_OUTPUT
shell: bash
id: deploy-options
- name: DAG Deploy to Astro
if: steps.deployment-type.outputs.DAGS_ONLY == 1
run: |
cd ${{ inputs.root-folder }}
astro deploy ${{steps.deployment-preview.outputs.FINAL_DEPLOYMENT_ID}} --dags ${{steps.deploy-options.outputs.OPTIONS}}
shell: bash
# If any other files changed or dag deploys is disabled, deploy the entire Astro project
- name: Image and DAG Deploy to Astro
if: steps.deployment-type.outputs.DAGS_ONLY == 0
run: |
cd ${{ inputs.root-folder }}
astro deploy ${{steps.deployment-preview.outputs.FINAL_DEPLOYMENT_ID}} ${{steps.deploy-options.outputs.OPTIONS}}
shell: bash
- name: Copy Airflow Connections, Variables, and Pools
run: |
if [[ ${{ inputs.action }} == create-deployment-preview ]]; then
if [[ ${{ inputs.copy-connections }} == true ]]; then
astro deployment connection copy --source-id ${{steps.deployment-preview.outputs.ORIGINAL_DEPLOYMENT_ID}} --target-id ${{steps.deployment-preview.outputs.FINAL_DEPLOYMENT_ID}}
fi
if [[ ${{ inputs.copy-airflow-variables }} == true ]]; then
astro deployment airflow-variable copy --source-id ${{steps.deployment-preview.outputs.ORIGINAL_DEPLOYMENT_ID}} --target-id ${{steps.deployment-preview.outputs.FINAL_DEPLOYMENT_ID}}
fi
if [[ ${{ inputs.copy-pools }} == true ]]; then
astro deployment pool copy --source-id ${{steps.deployment-preview.outputs.ORIGINAL_DEPLOYMENT_ID}} --target-id ${{steps.deployment-preview.outputs.FINAL_DEPLOYMENT_ID}}
fi
fi
shell: bash