Skip to content

Commit 7069e36

Browse files
committed
Update IAM policy in Github workflow
1 parent 2e21776 commit 7069e36

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

.github/workflows/deploy-infrastructure.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ env:
3939
aws_role: ${{ inputs.environment == 'production'
4040
&& 'arn:aws:iam::820242920762:role/GithubDeployMavisAndInfrastructure'
4141
|| 'arn:aws:iam::393416225559:role/GithubDeployMavisAndInfrastructure' }}
42+
aws_account_id: ${{ inputs.environment == 'production'
43+
&& '820242920762' || '393416225559' }}
4244

4345
defaults:
4446
run:
@@ -60,6 +62,10 @@ jobs:
6062
with:
6163
role-to-assume: ${{ env.aws_role }}
6264
aws-region: eu-west-2
65+
- name: Update IAM policy
66+
run: |
67+
set -e
68+
./../scripts/update-github-actions-policy.sh arn:aws:iam::${{ env.aws_role }}:policy/DeployMavisResources ../resources/github_actions_policy.json
6369
- name: Set image tag
6470
run: |
6571
IMAGE_TAG="${{ inputs.image_tag || github.sha }}"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
3+
if [ "$#" -ne 2 ]; then
4+
echo "Usage: $0 <policy-arn> <policy-file>"
5+
exit 1
6+
fi
7+
8+
POLICY_ARN=$1
9+
POLICY_FILE=$2
10+
11+
# Get existing policy versions
12+
EXISTING_VERSIONS=$(aws iam list-policy-versions --policy-arn "$POLICY_ARN" --query 'Versions[].VersionId' --output text)
13+
14+
# If there are 5 or more versions, delete the oldest one
15+
if [ "$(echo "$EXISTING_VERSIONS" | wc -w)" -ge 5 ]; then
16+
OLDEST_VERSION=$(echo "$EXISTING_VERSIONS" | awk '{print $NF}')
17+
echo "Deleting oldest version: $OLDEST_VERSION"
18+
aws iam delete-policy-version --policy-arn "$POLICY_ARN" --version-id "$OLDEST_VERSION"
19+
else
20+
echo "No need to delete any policy versions."
21+
fi
22+
23+
# Create a new version of the policy
24+
aws iam create-policy-version --policy-arn "$POLICY_ARN" --policy-document "file://$POLICY_FILE" --set-as-default

0 commit comments

Comments
 (0)