@@ -47,9 +47,45 @@ 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+ shell : bash
70+ run : |
71+ VERSION_ID=$(aws iam get-policy --policy-arn arn:aws:iam::393416225559:policy/DeployMavisResources --query 'Policy.DefaultVersionId' --output text)
72+ DEPLOYED_POLICY=$(aws iam get-policy-version --policy-arn arn:aws:iam::393416225559:policy/DeployMavisResources --version-id $VERSION_ID --query 'PolicyVersion.Document' --output json)
73+ echo "fetched deployed policy"
74+ POLICY_DIFF=$(diff <(echo "$DEPLOYED_VERSION" | jq --sort-keys .) <(jq --sort-keys . ../resources/github_actions_policy.json))
75+ echo "Policy diff: $POLICY_DIFF"
76+ if [ -n "$POLICY_DIFF" ]; then
77+ echo "Policy mismatch detected"
78+ echo "policy_mismatch=true" >> $GITHUB_OUTPUT
79+ else
80+ echo "No policy mismatch detected"
81+ fi
82+
83+ update-permissions :
84+ name : Update permissions
5285 runs-on : ubuntu-latest
86+ needs : validate-permissions
87+ if : inputs.environment == 'sandbox-beta' && needs.validate-permissions.outputs.policy-mismatch == 'true'
88+ environment : ${{ inputs.environment }}
5389 permissions :
5490 id-token : write
5591 steps :
@@ -66,6 +102,23 @@ jobs:
66102 run : |
67103 set -e
68104 ./../scripts/update-github-actions-policy.sh arn:aws:iam::${{ env.aws_role }}:policy/DeployMavisResources ../resources/github_actions_policy.json
105+
106+ plan :
107+ name : Terraform plan
108+ runs-on : ubuntu-latest
109+ needs : [validate-permissions, update-permissions]
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 }}"
0 commit comments