Skip to content

Commit 7cae593

Browse files
committed
Update IAM policy in Github workflow
1 parent 2e21776 commit 7cae593

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

.github/workflows/deploy-infrastructure.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ jobs:
6060
with:
6161
role-to-assume: ${{ env.aws_role }}
6262
aws-region: eu-west-2
63+
- name: Update IAM policy
64+
run: |
65+
set -e
66+
./../scripts/update-github-actions-policy.sh ${{ env.aws_role }} ../resources/github_actions_policy.json
6367
- name: Set image tag
6468
run: |
6569
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)