Skip to content

Commit 06018d5

Browse files
authored
feat: INF-3140 keep preview images for the preview PR lifetime (#63)
* feat: preserve preview images for the preview PR lifetime * fix the release workflow * fix policy * use list * add tagPrefixList * fix policy * remove duplicate = * set preview image expiration to 365 days * use latest policies
1 parent 8ac3331 commit 06018d5

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

.github/workflows/build-and-push-image-to-ecr.yaml renamed to .github/workflows/preview.build-image.yaml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,20 @@ jobs:
5252
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
5353
- name: Create ECR repository if it doesn't exist
5454
run: |
55-
aws ecr describe-repositories --repository-names ${{ inputs.APPLICATION_NAME }} || \
56-
aws ecr create-repository --repository-name ${{ inputs.APPLICATION_NAME }}
57-
LIFECYCLE_POLICY='{"rules":[{"rulePriority":1,"description":"Keep last 500 images","selection":{"tagStatus":"any","countType":"imageCountMoreThan","countNumber":500},"action":{"type":"expire"}}]}'
55+
if ! aws ecr describe-repositories --repository-names ${{ inputs.APPLICATION_NAME }} 2>/dev/null; then
56+
echo "Repository ${{ inputs.APPLICATION_NAME }} does not exist, creating it..."
57+
aws ecr create-repository --repository-name ${{ inputs.APPLICATION_NAME }}
58+
echo "Setting lifecycle policy..."
59+
else
60+
echo "Repository ${{ inputs.APPLICATION_NAME }} already exists, skipping creation"
61+
fi
62+
63+
echo "Applying lifecycle policies"
64+
LIFECYCLE_POLICY='{"rules":[
65+
{"rulePriority":1,"description":"Preserve preview images","selection":{"tagStatus":"tagged","tagPatternList":["preview-*"],"countType":"sinceImagePushed","countNumber":365},"action":{"type":"expire"}},
66+
{"rulePriority":2,"description":"Preserve production images","selection":{"tagStatus":"tagged","tagPatternList":["v*"],"countType":"imageCountMoreThan","countNumber":50},"action":{"type":"expire"}}
67+
{"rulePriority":3,"description":"Remove untagged images","selection":{"tagStatus":"untagged","countType":"sinceImagePushed","countNumber":7},"action":{"type":"expire"}}
68+
]}'
5869
aws ecr put-lifecycle-policy --repository-name ${{ inputs.APPLICATION_NAME }} --lifecycle-policy-text "$LIFECYCLE_POLICY"
5970
- name: Set up Docker Buildx
6071
uses: docker/setup-buildx-action@v2
@@ -70,7 +81,7 @@ jobs:
7081
provenance: false
7182
push: true
7283
tags: |
73-
${{ steps.login-ecr.outputs.registry }}/${{ inputs.APPLICATION_NAME }}:preview
84+
${{ steps.login-ecr.outputs.registry }}/${{ inputs.APPLICATION_NAME }}:preview-${{ github.event.pull_request.number }}
7485
${{ steps.login-ecr.outputs.registry }}/${{ inputs.APPLICATION_NAME }}:${{ github.event.pull_request.head.sha }}
7586
comment-pr:
7687
if: ${{ inputs.GHA_TRIGGER_EVENT != 'synchronize' }}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Remove preview tag from ECR
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
APPLICATION_NAME:
7+
description: The name of the application
8+
required: true
9+
type: string
10+
secrets:
11+
AWS_ROLE_TO_ASSUME:
12+
required: true
13+
description: AWS OIDC role for GitHub to assume
14+
15+
jobs:
16+
remove-preview-tag:
17+
permissions:
18+
id-token: write
19+
contents: read
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Configure AWS credentials
23+
uses: aws-actions/configure-aws-credentials@v2
24+
with:
25+
aws-region: eu-central-1
26+
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
27+
- name: Remove preview tag from ECR
28+
run: |
29+
# Check if repository exists
30+
if aws ecr describe-repositories --repository-names ${{ inputs.APPLICATION_NAME }} 2>/dev/null; then
31+
echo "Repository ${{ inputs.APPLICATION_NAME }} exists, attempting to remove preview-${{ github.event.pull_request.number }} tag..."
32+
33+
# Remove the preview tag
34+
aws ecr batch-delete-image \
35+
--repository-name ${{ inputs.APPLICATION_NAME }} \
36+
--image-ids imageTag=preview-${{ github.event.pull_request.number }} || \
37+
echo "Tag preview-${{ github.event.pull_request.number }} not found or already removed"
38+
else
39+
echo "Repository ${{ inputs.APPLICATION_NAME }} does not exist, nothing to remove"
40+
fi

0 commit comments

Comments
 (0)