Skip to content

Commit

Permalink
added role assumption to test job
Browse files Browse the repository at this point in the history
  • Loading branch information
dms1981 committed Dec 11, 2023
1 parent cf829e9 commit 928b629
Showing 1 changed file with 39 additions and 9 deletions.
48 changes: 39 additions & 9 deletions .github/workflows/reusable_terraform_plan_apply_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ on:
required: false
description: "The terraform version to use"
default: "~1.5"
init_plan_apply_tfargs:
type: string
required: false
description: "Any terraform arguments to be passed into terrafrom init, plan and apply, e.g. --lock-timeout=300s"
default: "-input=false -lock-timeout=300s"
plan_apply_tfargs:
type: string
required: false
Expand Down Expand Up @@ -97,6 +92,12 @@ jobs:
run: |
ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${ACCOUNT_NAME}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)
echo "ACCOUNT_NUMBER=${ACCOUNT_NUMBER}" >> $GITHUB_ENV
- name: Get Backend AWS Account Number
run: |
BACKEND_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)
echo "BACKEND_NUMBER=${BACKEND_NUMBER}" >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
Expand All @@ -114,12 +115,14 @@ jobs:
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
terraform --version
echo "terraform init ${{ inputs.init_plan_apply_tfargs }}"
terraform init ${{ inputs.init_plan_apply_tfargs }}
echo "terraform init -backend-config=assume_role={role_arn=\"arn:aws:iam::${{env.BACKEND_NUMBER}}:role/modernisation-account-terraform-state-member-access\"}"
terraform init -backend-config=assume_role={role_arn=\"arn:aws:iam::${{env.BACKEND_NUMBER}}:role/modernisation-account-terraform-state-member-access\"}
- name: Terraform Workspace Select
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
terraform workspace select "${WORKSPACE_NAME}"
- name: Terraform State Refresh (Optional)
if: inputs.do_state_refresh_on_plan == true
working-directory: "terraform/environments/${{ inputs.application }}"
Expand All @@ -128,6 +131,7 @@ jobs:
tf_args="${{ inputs.init_plan_apply_tfargs }} ${{ inputs.plan_apply_tfargs }}"
echo "terraform apply -refresh-only -auto-approve ${tf_args}"
terraform apply -refresh-only -auto-approve ${tf_args} | bash ${GITHUB_WORKSPACE}/scripts/redact-output.sh
- name: Terraform Plan
id: plan
env:
Expand All @@ -144,6 +148,7 @@ jobs:
echo "exitcode=${exitcode}" # 0=clean plan, 1=error, 2=stuff in plan
echo "exitcode=${exitcode}" >> $GITHUB_OUTPUT
(( exitcode == 1 )) && exit 1 || exit 0
- name: Create Plan PR message (Optional)
if: github.event_name == 'pull_request' && steps.plan.outputs.exitcode == '2' && inputs.post_plan_to_pr == true
working-directory: "terraform/environments/${{ inputs.application }}"
Expand All @@ -164,6 +169,7 @@ jobs:
echo 'TF_PLAN_OUT<<EOF' >> $GITHUB_ENV
comment >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Hide Previous PR comment (Optional)
if: ${{ github.event_name == 'pull_request' }}
working-directory: "scripts/minimise-comments"
Expand All @@ -173,6 +179,7 @@ jobs:
run: |
go build
./minimise-comments
- name: Post Plan to PR (Optional)
if: github.event_name == 'pull_request' && steps.plan.outputs.exitcode == '2' && inputs.post_plan_to_pr == true
env:
Expand All @@ -184,6 +191,7 @@ jobs:
-H "Authorization: Bearer ${{ env.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
-d '{"body":'"${escaped_message}"'}'
terratest:
name: "terratest"
needs: plan
Expand All @@ -197,6 +205,12 @@ jobs:
run: |
ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${ACCOUNT_NAME}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)
echo "ACCOUNT_NUMBER=${ACCOUNT_NUMBER}" >> $GITHUB_ENV
- name: Get Backend AWS Account Number
run: |
BACKEND_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)
echo "BACKEND_NUMBER=${BACKEND_NUMBER}" >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
Expand All @@ -223,6 +237,12 @@ jobs:
run: |
ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${ACCOUNT_NAME}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)
echo "ACCOUNT_NUMBER=${ACCOUNT_NUMBER}" >> $GITHUB_ENV
- name: Get Backend AWS Account Number
run: |
BACKEND_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)
echo "BACKEND_NUMBER=${BACKEND_NUMBER}" >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
Expand All @@ -240,26 +260,30 @@ jobs:
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
terraform --version
echo "terraform init ${{ inputs.init_plan_apply_tfargs }}"
terraform init ${{ inputs.init_plan_apply_tfargs }}
echo "terraform init -backend-config=assume_role={role_arn=\"arn:aws:iam::${{env.BACKEND_NUMBER}}:role/modernisation-account-terraform-state-member-access\"}"
terraform init -backend-config=assume_role={role_arn=\"arn:aws:iam::${{env.BACKEND_NUMBER}}:role/modernisation-account-terraform-state-member-access\"}
- name: Terraform Workspace Select
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
terraform workspace select "${WORKSPACE_NAME}"
- name: Terraform Plan
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
set -o pipefail
tf_args="-out x.tfplan ${{ inputs.init_plan_apply_tfargs }} ${{ inputs.plan_apply_tfargs }}"
echo "terraform plan ${tf_args}"
terraform plan ${tf_args} | bash ${GITHUB_WORKSPACE}/scripts/redact-output.sh
- name: Terraform Apply
working-directory: "terraform/environments/${{ inputs.application }}"
run: |
set -o pipefail
tf_args="${{ inputs.init_plan_apply_tfargs }} ${{ inputs.plan_apply_tfargs }} x.tfplan"
echo "terraform apply ${tf_args}"
terraform apply ${tf_args} | bash ${GITHUB_WORKSPACE}/scripts/redact-output.sh
smoketest:
name: "smoketest"
needs: apply
Expand All @@ -273,6 +297,12 @@ jobs:
run: |
ACCOUNT_NUMBER=$(jq -r -e --arg account_name "${ACCOUNT_NAME}" '.account_ids[$account_name]' <<< $ENVIRONMENT_MANAGEMENT)
echo "ACCOUNT_NUMBER=${ACCOUNT_NUMBER}" >> $GITHUB_ENV
- name: Get Backend AWS Account Number
run: |
BACKEND_NUMBER=$(jq -r -e '.modernisation_platform_account_id' <<< $ENVIRONMENT_MANAGEMENT)
echo "BACKEND_NUMBER=${BACKEND_NUMBER}" >> $GITHUB_ENV
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
Expand Down

0 comments on commit 928b629

Please sign in to comment.