-
Notifications
You must be signed in to change notification settings - Fork 0
98 lines (96 loc) · 4.39 KB
/
Copy pathpreview.build-image.yaml
File metadata and controls
98 lines (96 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
name: Build and push image from the PR to ECR
on:
workflow_call:
inputs:
APPLICATION_NAME:
description: The name of the application
required: true
type: string
PR_NUMBER:
description: Number of the current PR
required: true
type: number
GHA_TRIGGER_EVENT:
description: GitHub PR event that triggered the workflow
required: true
type: string
artifactName:
required: false
description: If provided, downloads a previously uploaded artifact (has to be in the same workflow). Both artifactPath and artifactName have to be passed.
default: ""
type: string
artifactPath:
required: false
description: If provided, downloads a previously uploaded artifact (has to be in the same workflow). Both artifactPath and artifactName have to be passed.
default: ""
type: string
timeoutMinutes:
required: false
description: Maximum minutes before the job is cancelled
default: 40
type: number
jobs:
build-and-push-image-to-ecr:
permissions:
id-token: write
contents: read
runs-on: ubuntu-latest
timeout-minutes: ${{ inputs.timeoutMinutes }}
steps:
- name: Checkout current git repository
uses: actions/checkout@v7
- name: Download build artifact
if: inputs.artifactPath != '' && inputs.artifactName != ''
uses: actions/download-artifact@v8
with:
path: ${{ inputs.artifactPath }}
name: ${{ inputs.artifactName }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v6
with:
aws-region: eu-central-1
role-to-assume: ${{ vars.AWS_ASSUME_ROLE_ARN_ECR }}
- name: Create ECR repository if it doesn't exist
run: |
if ! aws ecr describe-repositories --repository-names ${{ inputs.APPLICATION_NAME }} 2>/dev/null; then
echo "Repository ${{ inputs.APPLICATION_NAME }} does not exist, creating it..."
aws ecr create-repository --repository-name ${{ inputs.APPLICATION_NAME }}
echo "Setting lifecycle policy..."
else
echo "Repository ${{ inputs.APPLICATION_NAME }} already exists, skipping creation"
fi
echo "Applying lifecycle policies"
LIFECYCLE_POLICY='{"rules":[
{"rulePriority":1,"description":"Preserve preview images","selection":{"tagStatus":"tagged","tagPatternList":["preview-*"],"countType":"sinceImagePushed","countUnit":"days","countNumber":365},"action":{"type":"expire"}},
{"rulePriority":2,"description":"Preserve production images","selection":{"tagStatus":"tagged","tagPatternList":["v*"],"countType":"imageCountMoreThan","countNumber":50},"action":{"type":"expire"}},
{"rulePriority":3,"description":"Remove untagged images","selection":{"tagStatus":"untagged","countType":"sinceImagePushed","countUnit":"days","countNumber":7},"action":{"type":"expire"}}
]}'
aws ecr put-lifecycle-policy --repository-name ${{ inputs.APPLICATION_NAME }} --lifecycle-policy-text "$LIFECYCLE_POLICY"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v4
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build and push to repository
uses: docker/build-push-action@v7.2.0
with:
context: .
file: Containerfile
platforms: linux/amd64
provenance: false
push: true
tags: |
${{ steps.login-ecr.outputs.registry }}/${{ inputs.APPLICATION_NAME }}:preview-${{ github.event.pull_request.number }}
${{ steps.login-ecr.outputs.registry }}/${{ inputs.APPLICATION_NAME }}:${{ github.event.pull_request.head.sha }}
comment-pr:
if: ${{ inputs.GHA_TRIGGER_EVENT != 'synchronize' }}
runs-on: ubuntu-latest
timeout-minutes: ${{ inputs.timeoutMinutes }}
steps:
- name: Comment PR
uses: thollander/actions-comment-pull-request@v3
with:
message: |
Preview URL: https://${{ inputs.APPLICATION_NAME }}-preview-${{ inputs.PR_NUMBER }}.staging.parcellab.dev
Preview Argo App: https://argocd.staging.parcellab.dev/applications/argocd/${{ inputs.APPLICATION_NAME }}-preview-${{ inputs.PR_NUMBER }}
comment-tag: preview-url