-
Notifications
You must be signed in to change notification settings - Fork 53
149 lines (138 loc) · 5 KB
/
push-to-ecr.yml
File metadata and controls
149 lines (138 loc) · 5 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
name: Push to ECR
permissions:
id-token: write
contents: read
on:
workflow_call:
inputs:
architecture:
description: 'architecture'
type: string
required: true
environment:
description: 'environment'
type: string
required: true
secrets:
role:
description: 'AWS role to assume'
required: true
account_id:
description: 'AWS account id'
required: true
devRegion:
description: 'AWS region for DEV'
required: true
prodRegion:
description: 'AWS region for PROD'
required: true
jobs:
setup:
runs-on: ubuntu-24.04
outputs:
runner: ${{ steps.select.outputs.runner }}
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Check branch
id: select
run: |
if [ ${{ inputs.architecture }} == 'x86_64' ]; then
echo "runner=ubuntu-24.04" >> $GITHUB_OUTPUT
else
echo "runner=ubuntu-24.04-arm" >> $GITHUB_OUTPUT
fi
cat $GITHUB_OUTPUT
- uses: actions/checkout@v4
- name: Build runtime matrix
id: set-matrix
run: |
jq -c '[.runtimes[] | select(.architectures | index("arm64")) | select(.image != null and .image.baseImage != null) | .path]' manifest.json
echo "matrix=$(jq -c '[.runtimes[] | select(.architectures | index("${{ inputs.architecture }}")) | select(.image != null and .image.baseImage != null) | .path]' manifest.json)" >> $GITHUB_OUTPUT
build-upload:
needs: setup
runs-on: ${{ needs.setup.outputs.runner }}
strategy:
fail-fast: false
matrix:
runtime_id: ${{fromJson(needs.setup.outputs.matrix)}}
steps:
- uses: actions/checkout@v4
- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.role }}
role-session-name: GitHub_Action_LambdaPerf_Session
aws-region: ${{ inputs.environment == 'DEV' && secrets.devRegion || inputs.environment == 'PROD' && secrets.prodRegion }}
- name: Delete huge unnecessary folders
run: |
rm -rf /opt/hostedtoolcache
rm -rf /usr/share/dotnet
rm -rf /opt/ghc
rm -rf "$AGENT_TOOLSDIRECTORY"
- name: Set up Docker
uses: docker/setup-docker-action@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- name: build and deploy
env:
ARCHITECTURE: ${{ inputs.architecture }}
AWS_REGION: ${{ inputs.environment == 'DEV' && secrets.devRegion || inputs.environment == 'PROD' && secrets.prodRegion }}
RUNTIME_ID: ${{ matrix.runtime_id }}
AWS_ACCOUNT_ID: ${{ secrets.account_id }}
run: |
cd container-uploader
yarn install
node app.mjs
clean-untagged-image:
needs: build-upload
runs-on: ubuntu-24.04
steps:
- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.role }}
role-session-name: GitHub_Action_LambdaPerf_Session
aws-region: ${{ inputs.environment == 'DEV' && secrets.devRegion || inputs.environment == 'PROD' && secrets.prodRegion }}
- name: clean untagged images
env:
AWS_REGION: ${{ inputs.environment == 'DEV' && secrets.devRegion || inputs.environment == 'PROD' && secrets.prodRegion }}
run: |
echo "Fetching untagged images..."
IMAGES_JSON=$(aws ecr list-images \
--region "${AWS_REGION}" \
--repository-name lambda-perf \
--filter "tagStatus=UNTAGGED" \
--query 'imageIds' \
--output json)
COUNT=$(echo "$IMAGES_JSON" | jq length)
if (( COUNT == 0 )); then
echo "No untagged images found. Skipping deletion."
exit 0
fi
echo "Found $COUNT untagged images. Deleting in batches of 100..."
# read all images into an array
mapfile -t IMAGES_ARRAY < <(echo "$IMAGES_JSON" | jq -c '.[]')
BATCH=()
for IMG in "${IMAGES_ARRAY[@]}"; do
BATCH+=("$IMG")
if (( ${#BATCH[@]} == 100 )); then
BATCH_JSON=$(printf '%s\n' "${BATCH[@]}" | jq -s '.')
echo "Deleting batch of 100 images..."
aws ecr batch-delete-image \
--region "${AWS_REGION}" \
--repository-name lambda-perf \
--image-ids "$BATCH_JSON"
BATCH=()
fi
done
# delete leftover images
if (( ${#BATCH[@]} > 0 )); then
BATCH_JSON=$(printf '%s\n' "${BATCH[@]}" | jq -s '.')
LEFT_COUNT=${#BATCH[@]}
echo "Deleting final batch of $LEFT_COUNT images..."
aws ecr batch-delete-image \
--region "${AWS_REGION}" \
--repository-name lambda-perf \
--image-ids "$BATCH_JSON"
fi