Skip to content

Commit ac8c5ca

Browse files
committed
Split validation and update
1 parent 7069e36 commit ac8c5ca

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

.github/workflows/deploy-infrastructure.yml

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,44 @@ defaults:
4747
working-directory: terraform/app
4848

4949
jobs:
50-
plan:
51-
name: Terraform plan
50+
validate-permissions:
51+
name: Validate permissions
52+
runs-on: ubuntu-latest
53+
permissions:
54+
id-token: write
55+
outputs:
56+
policy-mismatch: ${{ steps.compare-permissions.outputs.policy_mismatch }}
57+
steps:
58+
- name: Checkout code
59+
uses: actions/checkout@v4
60+
with:
61+
ref: ${{ inputs.image_tag || github.sha }}
62+
- name: Configure AWS Credentials
63+
uses: aws-actions/configure-aws-credentials@v4
64+
with:
65+
role-to-assume: ${{ env.aws_role }}
66+
aws-region: eu-west-2
67+
- name: Compare permissions
68+
id: compare-permissions
69+
run: |
70+
VERSION_ID=$(aws iam get-policy --policy-arn arn:aws:iam::${{ env.aws_account_id }}:policy/DeployMavisResources --query 'Policy.DefaultVersionId' --output text)
71+
DEPLOYED_POLICY=$(aws iam get-policy-version --policy-arn arn:aws:iam::${{ env.aws_account_id }}:policy/DeployMavisResources --version-id $VERSION_ID --query 'PolicyVersion.Document' --output json)
72+
echo "fetched deployed policy: $DEPLOYED_POLICY"
73+
POLICY_DIFF=$(diff <(echo "$DEPLOYED_POLICY" | jq --sort-keys .) <(jq --sort-keys . ../resources/github_actions_policy.json))
74+
echo "Policy diff: $POLICY_DIFF"
75+
if [ -n "$POLICY_DIFF" ]; then
76+
echo "Policy mismatch detected: $POLICY_DIFF"
77+
echo "policy_mismatch=true" >> $GITHUB_OUTPUT
78+
else
79+
echo "No policy mismatch detected"
80+
fi
81+
82+
update-permissions:
83+
name: Update permissions
5284
runs-on: ubuntu-latest
85+
needs: validate-permissions
86+
if: inputs.environment == 'sandbox-beta' && needs.validate-permissions.outputs.policy-mismatch == 'true'
87+
environment: ${{ inputs.environment }}
5388
permissions:
5489
id-token: write
5590
steps:
@@ -66,6 +101,24 @@ jobs:
66101
run: |
67102
set -e
68103
./../scripts/update-github-actions-policy.sh arn:aws:iam::${{ env.aws_role }}:policy/DeployMavisResources ../resources/github_actions_policy.json
104+
105+
plan:
106+
name: Terraform plan
107+
runs-on: ubuntu-latest
108+
needs: update-permissions
109+
if: always()
110+
permissions:
111+
id-token: write
112+
steps:
113+
- name: Checkout code
114+
uses: actions/checkout@v4
115+
with:
116+
ref: ${{ inputs.image_tag || github.sha }}
117+
- name: Configure AWS Credentials
118+
uses: aws-actions/configure-aws-credentials@v4
119+
with:
120+
role-to-assume: ${{ env.aws_role }}
121+
aws-region: eu-west-2
69122
- name: Set image tag
70123
run: |
71124
IMAGE_TAG="${{ inputs.image_tag || github.sha }}"

terraform/resources/github_actions_policy.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"Sid": "Statement1",
66
"Effect": "Allow",
77
"Action": [
8+
"acm:DescribeCertificate",
89
"acm:DeleteCertificate",
910
"acm:RequestCertificate",
1011
"application-autoscaling:DeleteScalingPolicy",

0 commit comments

Comments
 (0)