@@ -47,9 +47,44 @@ defaults:
4747 working-directory : terraform/app
4848
4949jobs :
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::393416225559:policy/DeployMavisResources --query 'Policy.DefaultVersionId' --output text)
71+ DEPLOYED_POLICY=$(aws iam get-policy-version --policy-arn arn:aws:iam::393416225559:policy/DeployMavisResources --version-id $VERSION_ID --query 'PolicyVersion.Document' --output json)
72+ echo "fetched 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,23 @@ 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 : [validate-permissions, update-permissions]
109+ permissions :
110+ id-token : write
111+ steps :
112+ - name : Checkout code
113+ uses : actions/checkout@v4
114+ with :
115+ ref : ${{ inputs.image_tag || github.sha }}
116+ - name : Configure AWS Credentials
117+ uses : aws-actions/configure-aws-credentials@v4
118+ with :
119+ role-to-assume : ${{ env.aws_role }}
120+ aws-region : eu-west-2
69121 - name : Set image tag
70122 run : |
71123 IMAGE_TAG="${{ inputs.image_tag || github.sha }}"
0 commit comments