Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions .github/actions/terraform-action/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Action to run TF Scaffold Terraform Action

#### Prerequisites
Authenticated with AWS Account
Authenticated with AWS Account. The Terraform directory must contain an executable `./bin/terraform.sh`. Python 3 is required to parse non-empty `terraform_args`.

#### Context
This action allows the workflow to run terraform commands via TF Scaffold
Expand All @@ -15,9 +15,11 @@ This action allows the workflow to run terraform commands via TF Scaffold
- group (required): Terraform group to reference e.g dev.
- bucket-prefix (required): AWS Terraform bucket used for TF state.
- region (required): AWS region used e.g eu-west-1.
- terraform_version (optional): Terraform Version to install on runner.
- terraform_args (optional): Additional Terraform arguments.
- terraform_directory (optional): Directory location of Terraform
- terraform_version (optional): Terraform version to install (default `1.9.8`).
- terraform_args (optional): Additional Terraform arguments, parsed with shell-style quoting.
- terraform_directory (optional): Directory containing `bin/terraform.sh` (default `.`).
- artifact_name (optional): Extra artifact name suffix (default empty).
- upload_artifact (optional): Upload the output artifact (`'true'` by default; set to `'false'` to disable).

#### Outputs
N/A
Expand All @@ -26,7 +28,6 @@ N/A
```yaml
- name: Terraform - db
uses: dvsa/.github/.github/actions/terraform-action@v7
working-directory: .
with:
action: plan
environment: dev
Expand All @@ -38,7 +39,6 @@ N/A

- name: Terraform - app
uses: dvsa/.github/.github/actions/terraform-action@v7
working-directory: ./infrastructure
with:
action: plan
environment: dev
Expand All @@ -47,5 +47,6 @@ N/A
bucket-prefix: mot-recalls-tfscaffold
region: eu-west-1
project: mot-project
terraform_directory: ./infrastructure
terraform_args: "-var lambda_version=1.x.x"
```
54 changes: 36 additions & 18 deletions .github/actions/terraform-action/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,38 @@ inputs:
action:
description: 'Terraform action to run e.g plan'
required: true
type: string
project:
description: 'Terraform project'
required: true
type: string
environment:
description: 'Terraform environment'
required: true
type: string
component:
description: 'Terraform component'
required: true
type: string
group:
description: 'Terraform group'
required: true
type: string
bucket-prefix:
description: 'AWS Terraform bucket used for TF state'
required: true
type: string
region:
description: 'AWS region'
required: true
type: string
terraform_version:
type: number
default: '1.3.7'
default: '1.9.8'
description: Terraform Version to install on runner
terraform_args:
type: string
default: ''
description: Additional Terraform arguments
terraform_directory:
type: string
default: '.'
description: Directory location of Terraform
artifact_name:
type: string
default: ''
description: 'Name of the artifact to upload (defaults to environment-component if empty)'
upload_artifact:
type: boolean
default: true
default: 'true'
description: 'Whether to upload the artifact or not'

runs:
Expand All @@ -61,18 +49,48 @@ runs:
- name: Terraform init and plan
id: plan
working-directory: ${{ inputs.terraform_directory }}
env:
INPUT_ACTION: ${{ inputs.action }}
INPUT_PROJECT: ${{ inputs.project }}
INPUT_ENVIRONMENT: ${{ inputs.environment }}
INPUT_COMPONENT: ${{ inputs.component }}
INPUT_GROUP: ${{ inputs.group }}
INPUT_BUCKET_PREFIX: ${{ inputs.bucket-prefix }}
INPUT_REGION: ${{ inputs.region }}
INPUT_TERRAFORM_ARGS: ${{ inputs.terraform_args }}
run: |
mkdir -p plans/${{ inputs.environment }}
./bin/terraform.sh --action ${{ inputs.action }} --project ${{ inputs.project }} --environment ${{ inputs.environment }} --component ${{ inputs.component }} --group ${{ inputs.group }} --bucket-prefix ${{ inputs.bucket-prefix }} --region ${{ inputs.region }} --compact-warnings --no-color -- ${{ inputs.terraform_args }} | tee plans/${{ inputs.environment }}/${{ inputs.component }}-plan.txt
set -o pipefail
if [[ ! -x ./bin/terraform.sh || ! -f ./bin/terraform.sh ]]; then
echo "::error::./bin/terraform.sh is missing or not executable in the Terraform directory."
exit 1
fi
args=()
if [[ -n "$INPUT_TERRAFORM_ARGS" ]]; then
parsed_args=$(python3 -c 'import json, os, shlex; print(json.dumps(shlex.split(os.environ["INPUT_TERRAFORM_ARGS"])))')
while IFS= read -r -d '' arg; do
args+=("$arg")
done < <(python3 -c 'import json, sys; [sys.stdout.buffer.write(arg.encode() + b"\0") for arg in json.loads(sys.argv[1])]' "$parsed_args")
fi
mkdir -p "plans/$INPUT_ENVIRONMENT"
./bin/terraform.sh \
--action "$INPUT_ACTION" \
--project "$INPUT_PROJECT" \
--environment "$INPUT_ENVIRONMENT" \
--component "$INPUT_COMPONENT" \
--group "$INPUT_GROUP" \
--bucket-prefix "$INPUT_BUCKET_PREFIX" \
--region "$INPUT_REGION" \
--compact-warnings --no-color -- "${args[@]}" \
| tee "plans/$INPUT_ENVIRONMENT/$INPUT_COMPONENT-plan.txt"
shell: bash

- name: Get current date
id: date
run: echo "date=$(date +'%Y-%m-%d-%H-%M-%S')" >> $GITHUB_OUTPUT
run: echo "date=$(date +'%Y-%m-%d-%H-%M-%S')" >> "$GITHUB_OUTPUT"
shell: bash

- name: Save Artifact
if: ${{ inputs.upload_artifact }}
if: ${{ inputs.upload_artifact == 'true' }}
uses: actions/upload-artifact@v7
with:
name: "${{ inputs.artifact_name != '' && format('{0}-{1}-{2}-{3}', inputs.environment, inputs.component, inputs.artifact_name, steps.date.outputs.date) || format('{0}-{1}-{2}', inputs.environment, inputs.component, steps.date.outputs.date) }}"
Expand Down
5 changes: 4 additions & 1 deletion .github/actions/terraform-fmt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ N/A
The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. This command applies a subset of the Terraform language style conventions, along with other minor adjustments for readability.

#### Inputs
N/A
- `terraform_version` (optional): Terraform version to install (default `1.9.8`).
- `check` (optional): Report formatting errors without changing files (`'true'` by default).
- `recursive` (optional): Check subdirectories (`'true'` by default).
- `path` (optional): File or directory to format (default current directory).

#### Outputs
N/A
Expand Down
37 changes: 22 additions & 15 deletions .github/actions/terraform-fmt/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ name: Terraform Format

inputs:
terraform_version:
type: number
default: '1.3.7'
default: '1.9.8'
description: Terraform version to install
check:
required: false
type: boolean
default: true
default: 'true'
description: Flag whether to only report errors without making changes
recursive:
required: false
default: true
default: 'true'
description: Flag to trigger recursive checks into subfolders of project path
path:
required: false
type: string
description: Target directory in which to run fmt command

runs:
Expand All @@ -24,13 +22,22 @@ runs:
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform_version }}
- run: |
if ${{ inputs.CHECK }} == true; then
echo "CHECK='-check'" >> $GITHUB_ENV
fi
if ${{ inputs.RECURSIVE }} == true; then
echo "RECURSIVE='-recursive'" >> $GITHUB_ENV
fi
shell: bash
- run: terraform fmt ${{ env.CHECK }} ${{ env.RECURSIVE }} ${{ inputs.path }}
terraform_wrapper: false
- name: Format Terraform
env:
INPUT_CHECK: ${{ inputs.check }}
INPUT_RECURSIVE: ${{ inputs.recursive }}
INPUT_PATH: ${{ inputs.path }}
run: |
args=()
if [[ "$INPUT_CHECK" == 'true' ]]; then
args+=(-check)
fi
if [[ "$INPUT_RECURSIVE" == 'true' ]]; then
args+=(-recursive)
fi
if [[ -n "$INPUT_PATH" ]]; then
args+=("$INPUT_PATH")
fi
terraform fmt "${args[@]}"
shell: bash
12 changes: 7 additions & 5 deletions .github/actions/terraform-workspace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Action to create Terraform Namespace

#### Prerequisites
N/A
The Terraform configuration must already be initialized and its backend accessible.
The checkout step preserves untracked files, including `.terraform`, from earlier steps.

#### Context
Creates TF workspace with either github actor name or if provided a unique name e.g uat1
Expand All @@ -12,19 +13,20 @@ Each Terraform configuration has an associated backend that defines how Terrafor
The persistent data stored in the backend belongs to a workspace. The backend initially has only one workspace containing one Terraform state associated with that configuration. Some backends support multiple named workspaces, allowing multiple states to be associated with a single configuration. The configuration still has only one backend, but you can deploy multiple distinct instances of that configuration without configuring a new backend or changing authentication credentials.

#### Inputs
- terraform_version (optional): Terraform Version to install on runner.
- terraform_workspace_name (optional): Name for Terraform Workspace e.g dev01 or uat01.
- terraform_version (optional): Terraform version to install (default `1.9.8`).
- terraform_workspace_name (optional): Workspace name, defaulting to the GitHub actor when empty.
- terraform_directory (optional): Directory containing the initialized configuration (default `.`).

#### Outputs
N/A
The selected workspace name is exported as `WORKSPACE` for subsequent steps.

#### Usage
```yaml
- name: TF workspace
uses: dvsa/.github/.github/actions/terraform-workspace@v7
with:
terraform_workspace_name: ${{ inputs.terraform_workspace_name }}
terraform_version: 1.3.7
terraform_version: '1.9.8'

- name: TF workspace
uses: dvsa/.github/.github/actions/terraform-workspace@v7
Expand Down
46 changes: 23 additions & 23 deletions .github/actions/terraform-workspace/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ name: Terraform Workspace

inputs:
terraform_version:
type: number
default: '1.3.7'
description: Terraform Version to install on runner
default: '1.9.8'
description: Terraform version to install on runner
terraform_workspace_name:
type: string
description: Name for Teraform Workspace e.g dev01 or uat01
description: Name for Terraform workspace, e.g. dev01 or uat01
terraform_directory:
default: '.'
description: Directory containing the initialized Terraform configuration

runs:
using: 'composite'
steps:
- uses: actions/checkout@v7
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform_version }}

- name: Create Terraform Workspace Default
if: ${{ inputs.terraform_workspace_name == null }}
run: |
terraform workspace select "${{ github.actor }}" || terraform workspace new "${{ github.actor }}"
echo "WORKSPACE=${{ github.actor }}" >> $GITHUB_ENV
shell: bash

- name: Create Terraform Workspace
if: ${{ inputs.terraform_workspace_name != null }}
run: |
terraform workspace select "${{ inputs.terraform_workspace_name }}" || terraform workspace new "${{ inputs.terraform_workspace_name }}"
echo "WORKSPACE=${{ inputs.terraform_workspace_name }}" >> $GITHUB_ENV
shell: bash
- uses: actions/checkout@v7
with:
clean: false
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform_version }}
terraform_wrapper: false
- name: Select or create Terraform workspace
working-directory: ${{ inputs.terraform_directory }}
env:
INPUT_WORKSPACE: ${{ inputs.terraform_workspace_name }}
ACTOR: ${{ github.actor }}
run: |
workspace="${INPUT_WORKSPACE:-$ACTOR}"
terraform workspace select "$workspace" || terraform workspace new "$workspace"
echo "WORKSPACE=$workspace" >> "$GITHUB_ENV"
shell: bash
Loading