-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaction.yaml
227 lines (208 loc) · 9.03 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
name: 'Deploy static site to AWS (S3+CDN+R53)'
description: 'Deploy a website to an S3 bucket. Option to add Cloudfront, and deploy to a Route53 managed domain with certs.'
branding:
icon: upload-cloud
color: red
inputs:
# Checkout
checkout:
description: 'Specifies if this action should checkout the code'
required: false
default: 'true'
# AWS
aws_access_key_id:
description: 'AWS access key ID'
required: true
aws_secret_access_key:
description: 'AWS secret access key'
required: true
aws_default_region:
description: 'AWS default region'
default: us-east-1
required: false
aws_role_to_assume:
description: 'AWS Role to assume.'
required: false
aws_resource_identifier:
description: 'Set to override the AWS resource identifier for the deployment. Defaults to `${org}-{repo}-{branch}`. Use with destroy to destroy specific resources.'
required: false
aws_additional_tags:
description: 'A JSON object of additional tags that will be included on created resources. Example: `{"key1": "value1", "key2": "value2"}`'
required: false
# Terraform options
tf_action:
description: 'Option to run Terraform apply / destroy option. Will run plan if nothing defined.'
required: false
tf_state_file_name:
description: 'Change this to be anything you want to. Carefull to be consistent here. A missing file could trigger recreation, or stepping over destruction of non-defined objects.'
required: false
tf_state_file_name_append:
description: 'Append a string to the tf-state-file. Setting this to `unique` will generate `tf-state-aws-unique`. Can co-exist with the tf_state_file_name variable. '
required: false
tf_state_bucket:
description: 'AWS S3 bucket to use for Terraform state. Defaults to `${org}-${repo}-{branch}-tf-state`'
required: false
tf_state_bucket_destroy:
description: 'Force purge and deletion of S3 bucket defined if terraform destroy action succeded.'
required: false
# Site Settings
aws_site_source_folder:
description: 'Source folder for files to be published. Will ignore any hidden file. Defaults to root folder of the calling repo if nothing defined.'
required: true
aws_site_root_object:
description: 'Root object to be served as entry-point. Defaults to `index.html`'
required: false
aws_site_error_document:
description: 'Error document set to S3 website config. Defaults to none.'
required: false
aws_site_bucket_name:
description: ' AWS S3 bucket name to use for the public files. Defaults to `${org}-${repo}-{branch}-sp`'
required: false
aws_site_cdn_enabled:
description: 'Enable or disables the use of CDN. Defaults to `false`.'
required: false
aws_site_cdn_custom_error_codes:
description: 'Customize error codes, Definition done with JSON variables'
required: false
# AWS Route53 Domains and Certificates
aws_r53_domain_name:
description: 'Define the root domain name for the application. e.g. `bitovi.com`'
required: false
aws_r53_sub_domain_name:
description: 'Define the sub-domain part of the URL. Defaults to `${GITHUB_ORG_NAME}-${GITHUB_REPO_NAME}-${GITHUB_BRANCH_NAME}`.'
aws_r53_root_domain_deploy:
description: 'Deploy application to root domain. Will create root and www records. Default is `false`.'
required: false
aws_r53_cert_arn:
description: Define the certificate ARN to use for the application.'
required: false
aws_r53_create_root_cert:
description: 'Generates and manage the root cert for the application. Default is `false`.'
required: false
aws_r53_create_sub_cert:
description: 'Generates and manage the sub-domain certificate for the application. Default is `false`.'
required: false
outputs:
public_url:
description: "The URL of the generated app"
value: ${{ steps.apply.outputs.public_url }}
runs:
using: 'composite'
steps:
- name: Checkout
if: ${{ inputs.checkout == 'true' }}
uses: actions/checkout@v4
- name: Configure AWS Credentials
if: ${{ inputs.aws_access_key_id != '' }}
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ inputs.aws_access_key_id }}
aws-secret-access-key: ${{ inputs.aws_secret_access_key }}
aws-region: ${{ inputs.aws_default_region }}
role-to-assume: ${{ inputs.aws_role_to_assume }}
- name: Deploy
id: deploy
shell: bash
env:
# AWS
AWS_DEFAULT_REGION: ${{ inputs.aws_default_region }}
AWS_RESOURCE_IDENTIFIER: ${{ inputs.aws_resource_identifier }}
AWS_ADDITIONAL_TAGS: ${{ inputs.aws_additional_tags }}
# TF
TF_STATE_FILE_NAME: ${{ inputs.tf_state_file_name }}
TF_STATE_FILE_NAME_APPEND: ${{ inputs.tf_state_file_name_append }}
TF_STATE_BUCKET: ${{ inputs.tf_state_bucket }}
TF_STATE_BUCKET_DESTROY: ${{ inputs.tf_state_bucket_destroy }}
# Site
AWS_SITE_SOURCE_FOLDER: ${{ inputs.aws_site_source_folder }}
AWS_SITE_ROOT_OBJECT: ${{ inputs.aws_site_root_object }}
AWS_SITE_ERROR_DOCUMENT: ${{ inputs.aws_site_error_document }}
AWS_SITE_BUCKET_NAME: ${{ inputs.aws_site_bucket_name }}
AWS_SITE_CDN_ENABLED: ${{ inputs.aws_site_cdn_enabled }}
AWS_SITE_CDN_CUSTOM_ERROR_CODES: ${{ inputs.aws_site_cdn_custom_error_codes }}
# AWS Route53 Domains abd Certificates
AWS_R53_DOMAIN_NAME: ${{ inputs.aws_r53_domain_name }}
AWS_R53_SUB_DOMAIN_NAME: ${{ inputs.aws_r53_sub_domain_name }}
AWS_R53_ROOT_DOMAIN_DEPLOY: ${{ inputs.aws_r53_root_domain_deploy }}
AWS_R53_CERT_ARN: ${{ inputs.aws_r53_cert_arn }}
AWS_R53_CREATE_ROOT_CERT: ${{ inputs.aws_r53_create_root_cert }}
AWS_R53_CREATE_SUB_CERT: ${{ inputs.aws_r53_create_sub_cert }}
run: |
echo "::group::Generating Terraform pre-requisites" > /dev/null
echo "running scripts/generate_deploy.sh"
$GITHUB_ACTION_PATH/scripts/generate_deploy.sh
echo "::endgroup::"
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
- name: Terraform Init
id: init
shell: bash
run: |
echo "::group::Terraform Init"
terraform -chdir=$GITHUB_ACTION_PATH/terraform_code init
echo "::endgroup::"
- name: Terraform Validate
id: validate
shell: bash
run: |
echo "::group::Terraform Validate"
terraform -chdir=$GITHUB_ACTION_PATH/terraform_code validate
echo "::endgroup::"
- name: Terraform Plan
if: ${{ success () && inputs.tf_action != 'destroy' }}
id: plan
shell: bash
run: |
echo "::group::Terraform Plan"
terraform -chdir=$GITHUB_ACTION_PATH/terraform_code plan -input=false
echo "::endgroup::"
- name: Terraform Apply
id: apply
if: ${{ success() && inputs.tf_action == 'apply' }}
shell: bash
run: |
echo "::group::Terraform Apply"
terraform -chdir=$GITHUB_ACTION_PATH/terraform_code apply -auto-approve
echo "::endgroup::"
terraform -chdir=$GITHUB_ACTION_PATH/terraform_code output | grep public_url | sed -e 's/ *= */=/g' -e 's/"//g' >> $GITHUB_OUTPUT
- name: Terraform Destroy
if: ${{ success() && inputs.tf_action == 'destroy' }}
shell: bash
run: |
echo "::group::Terraform Destroy"
terraform -chdir=$GITHUB_ACTION_PATH/terraform_code destroy -auto-approve
echo "::endgroup::"
- name: Purge and delete tf-state bucket
if: ${{ success() && inputs.tf_action == 'destroy' && inputs.tf_state_bucket_destroy == 'true' }}
shell: bash
env:
TF_STATE_BUCKET: ${{ inputs.tf_state_bucket }}
run: |
echo "::group::Purge and delete tf-state bucket"
$GITHUB_ACTION_PATH/scripts/destroy_tf_state_bucket.sh
echo "::endgroup::"
- name: Print result
if: ${{ success() && steps.apply.outputs.public_url != '' }}
shell: bash
run: |
echo "## It's published! :rocket:" >> $GITHUB_STEP_SUMMARY
echo ${{ steps.apply.outputs.public_url }} >> $GITHUB_STEP_SUMMARY
- name: Print result
if: ${{ success() && inputs.tf_action == 'destroy' && inputs.tf_state_bucket_destroy == 'true' }}
shell: bash
run: |
echo "## Deploy Destroyed! :boom:" >> $GITHUB_STEP_SUMMARY
echo "Infrastructure should be gone now!" >> $GITHUB_STEP_SUMMARY
- name: Print result
if: ${{ success() && inputs.tf_action == 'destroy' && inputs.tf_state_bucket_destroy != 'true' }}
shell: bash
run: |
echo "## Deploy Destroyed! :boom:" >> $GITHUB_STEP_SUMMARY
echo "Buckets and infrastructure should be gone now!" >> $GITHUB_STEP_SUMMARY
- name: Print error result
if: ${{ failure() }}
shell: bash
run: |
echo "## Workflow failed to run :fire:" >> $GITHUB_STEP_SUMMARY
echo "Please check the logs for possible errors." >> $GITHUB_STEP_SUMMARY
echo "If you consider this is a bug in the Github Action, please submit an issue to our repo." >> $GITHUB_STEP_SUMMARY