Skip to content

Commit 4790078

Browse files
authored
Add option to set terraform state file name and append extras (#11)
* Add option to set terraform state file name and append extras * Cosmetics in README
1 parent 8727384 commit 4790078

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656

5757
steps:
5858
- name: Create deploy-bucket
59-
uses: bitovi/[email protected].0
59+
uses: bitovi/[email protected].1
6060
with:
6161
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
6262
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
@@ -108,6 +108,8 @@ The following inputs can be used as `step.with` keys
108108
| Name | Type | Description |
109109
|------------------|---------|------------------------------------|
110110
| `tf_action` | String | Option to run Terraform `apply` / `destroy` action. Will run `plan` if nothing defined. |
111+
| `tf_state_file_name` | String | Change this to be anything you want to. Careful to be consistent here. A missing file could trigger recreation, or stepping over destruction of non-defined objects. Defaults to `tf-state-site`. |
112+
| `tf_state_file_name_append` | String | Appends a string to the tf-state-file. Setting this to `unique` will generate `tf-state-aws-unique`. (Can co-exist with `tf_state_file_name`) |
111113
| `tf_state_bucket` | String | AWS S3 bucket name to use for Terraform state. Defaults to `${org}-${repo}-{branch}-tf-state` |
112114
| `tf_state_bucket_destroy` | Boolean | Force purge and deletion of S3 bucket defined if terraform destroy action succeded. |
113115
<hr/>

action.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ inputs:
3535
tf_action:
3636
desctiption: 'Option to run Terraform apply / destroy option. Will run plan if nothing defined.'
3737
required: false
38+
tf_state_file_name:
39+
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.'
40+
required: false
41+
tf_state_file_name_append:
42+
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. '
43+
required: false
3844
tf_state_bucket:
3945
description: 'AWS S3 bucket to use for Terraform state. Defaults to `${org}-${repo}-{branch}-tf-state`'
4046
required: false
@@ -105,6 +111,8 @@ runs:
105111
AWS_RESOURCE_IDENTIFIER: ${{ inputs.aws_resource_identifier }}
106112
AWS_ADDITIONAL_TAGS: ${{ inputs.aws_additional_tags }}
107113
# TF
114+
TF_STATE_FILE_NAME: ${{ inputs.tf_state_file_name }}
115+
TF_STATE_FILE_NAME_APPEND: ${{ inputs.tf_state_file_name_append }}
108116
TF_STATE_BUCKET: ${{ inputs.tf_state_bucket }}
109117
TF_STATE_BUCKET_DESTROY: ${{ inputs.tf_state_bucket_destroy }}
110118
# Site

scripts/generate_provider.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ set -e
1010

1111
echo "In generate_provider.sh"
1212

13+
function generate_tf_state_file_name () {
14+
if [ -n "$TF_STATE_FILE_NAME" ]; then
15+
filename="$TF_STATE_FILE_NAME"
16+
else
17+
filename="tf-state-$1"
18+
fi
19+
20+
if [ -n "$TF_STATE_FILE_NAME_APPEND" ]; then
21+
filename="${filename}-${TF_STATE_FILE_NAME_APPEND}"
22+
fi
23+
echo $filename
24+
}
25+
1326
echo "
1427
terraform {
1528
required_providers {
@@ -26,7 +39,7 @@ terraform {
2639
backend \"s3\" {
2740
region = \"${AWS_DEFAULT_REGION}\"
2841
bucket = \"${TF_STATE_BUCKET}\"
29-
key = \"tf-state-site\"
42+
key = \"$(generate_tf_state_file_name site)\"
3043
encrypt = true #AES-256encryption
3144
}
3245
}

0 commit comments

Comments
 (0)