Skip to content

Commit 9832416

Browse files
committed
adjust build-image
1 parent c01109b commit 9832416

2 files changed

Lines changed: 44 additions & 30 deletions

File tree

.github/workflows/build-and-deploy-to-eks.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ jobs:
109109
enableContainerScan: ${{ inputs.enableContainerScan }}
110110
runner: ${{ inputs.runner }}
111111
version: ${{ needs.init.outputs.version }}
112+
appName: ${{ inputs.artifactName }}
113+
environment: ${{ inputs.env }}
112114
secrets: inherit
113115

114116
commit:

.github/workflows/build-image.yaml

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ on:
3030
version:
3131
required: true
3232
type: string
33+
appName:
34+
required: false
35+
type: string
36+
environment:
37+
required: false
38+
type: string
3339

3440
env:
3541
IMAGE_SCAN_SEVERITY: LOW
@@ -43,8 +49,11 @@ jobs:
4349
permissions:
4450
id-token: write
4551
contents: read
46-
environment: ${{ github.event.deployment.payload.env }}
52+
environment: ${{ inputs.environment != '' && inputs.environment || github.event.deployment.payload.env }}
4753
runs-on: ${{ inputs.runner }}
54+
env:
55+
APP_NAME: ${{ inputs.appName != '' && inputs.appName || inputs.artifactName != '' && inputs.artifactName || github.event.deployment.payload.name }}
56+
ENVIRONMENT: ${{ inputs.environment != '' && inputs.environment || github.event.deployment.payload.env }}
4857
steps:
4958
- name: Checkout current git repository
5059
uses: actions/checkout@v4
@@ -70,12 +79,12 @@ jobs:
7079
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
7180
- name: Create ECR repository if it doesn't exist
7281
run: |
73-
if ! aws ecr describe-repositories --repository-names ${{ github.event.deployment.payload.name }} 2>/dev/null; then
74-
echo "Repository ${{ github.event.deployment.payload.name }} does not exist, creating it..."
75-
aws ecr create-repository --repository-name ${{ github.event.deployment.payload.name }}
82+
if ! aws ecr describe-repositories --repository-names ${{ env.APP_NAME }} 2>/dev/null; then
83+
echo "Repository ${{ env.APP_NAME }} does not exist, creating it..."
84+
aws ecr create-repository --repository-name ${{ env.APP_NAME }}
7685
echo "Setting lifecycle policy..."
7786
else
78-
echo "Repository ${{ github.event.deployment.payload.name }} already exists, skipping creation"
87+
echo "Repository ${{ env.APP_NAME }} already exists, skipping creation"
7988
fi
8089
8190
echo "Applying lifecycle policies"
@@ -84,7 +93,7 @@ jobs:
8493
{"rulePriority":2,"description":"Preserve production images","selection":{"tagStatus":"tagged","tagPatternList":["v*"],"countType":"imageCountMoreThan","countNumber":50},"action":{"type":"expire"}},
8594
{"rulePriority":3,"description":"Remove untagged images","selection":{"tagStatus":"untagged","countType":"sinceImagePushed","countUnit":"days","countNumber":7},"action":{"type":"expire"}}
8695
]}'
87-
aws ecr put-lifecycle-policy --repository-name ${{ github.event.deployment.payload.name }} --lifecycle-policy-text "$LIFECYCLE_POLICY"
96+
aws ecr put-lifecycle-policy --repository-name ${{ env.APP_NAME }} --lifecycle-policy-text "$LIFECYCLE_POLICY"
8897
- name: Login to Amazon ECR
8998
id: login-ecr
9099
uses: aws-actions/amazon-ecr-login@v2
@@ -94,24 +103,24 @@ jobs:
94103
build-args: |
95104
GITHUB_SHA=${{ github.sha }}
96105
VERSION=${{ inputs.version }}
97-
APP_NAME=${{ github.event.deployment.payload.name }}
98-
ENVIRONMENT=${{ github.event.deployment.payload.env }}
106+
APP_NAME=${{ env.APP_NAME }}
107+
ENVIRONMENT=${{ env.ENVIRONMENT }}
99108
NPM_GITHUB_TOKEN=${{ secrets.npmGithubReadToken }}
100-
cache-from: type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}:cache
101-
cache-to: mode=max,image-manifest=true,oci-mediatypes=true,type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}:cache
109+
cache-from: type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}:cache
110+
cache-to: mode=max,image-manifest=true,oci-mediatypes=true,type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}:cache
102111
context: ${{ github.event.deployment.payload.container.context }}
103112
load: true
104113
file: ${{ github.event.deployment.payload.container.file }}
105114
platforms: linux/amd64
106115
tags: |
107-
${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}:latest
108-
${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}:${{ inputs.version }}
109-
${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}:${{ github.sha }}
116+
${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}:latest
117+
${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}:${{ inputs.version }}
118+
${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}:${{ github.sha }}
110119
- name: Scan for vulnerabilities
111120
if: inputs.enableContainerScan
112121
uses: crazy-max/ghaction-container-scan@v3
113122
with:
114-
image: ${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}:latest
123+
image: ${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}:latest
115124
dockerfile: Containerfile
116125
severity: ${{ env.IMAGE_SCAN_SEVERITY }}
117126
severity_threshold: ${{ env.IMAGE_SCAN_SEVERITY_THRESHOLD }}
@@ -120,18 +129,21 @@ jobs:
120129
TRIVY_TIMEOUT: ${{ env.IMAGE_SCAN_TRIVY_TIMEOUT }}
121130
- name: Push image to ECR
122131
run: |
123-
docker push -a ${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}
132+
docker push -a ${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}
124133
125134
build-ecr-matrix:
126135
if: inputs.imageTargets != ''
127136
permissions:
128137
id-token: write
129138
contents: read
130-
environment: ${{ github.event.deployment.payload.env }}
139+
environment: ${{ inputs.environment != '' && inputs.environment || github.event.deployment.payload.env }}
131140
runs-on: ${{ inputs.runner }}
132141
strategy:
133142
matrix:
134143
containerfile_targets: ${{ fromJson(inputs.imageTargets) }}
144+
env:
145+
APP_NAME: ${{ inputs.appName != '' && inputs.appName || inputs.artifactName != '' && inputs.artifactName || github.event.deployment.payload.name }}
146+
ENVIRONMENT: ${{ inputs.environment != '' && inputs.environment || github.event.deployment.payload.env }}
135147
steps:
136148
- name: Checkout current git repository
137149
uses: actions/checkout@v4
@@ -157,12 +169,12 @@ jobs:
157169
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
158170
- name: Create ${{ matrix.containerfile_targets }} ECR repository if it doesn't exist
159171
run: |
160-
if ! aws ecr describe-repositories --repository-names ${{ github.event.deployment.payload.name }}-${{ matrix.containerfile_targets }} 2>/dev/null; then
161-
echo "Repository ${{ github.event.deployment.payload.name }}-${{ matrix.containerfile_targets }} does not exist, creating it..."
162-
aws ecr create-repository --repository-name ${{ github.event.deployment.payload.name }}-${{ matrix.containerfile_targets }}
172+
if ! aws ecr describe-repositories --repository-names ${{ env.APP_NAME }}-${{ matrix.containerfile_targets }} 2>/dev/null; then
173+
echo "Repository ${{ env.APP_NAME }}-${{ matrix.containerfile_targets }} does not exist, creating it..."
174+
aws ecr create-repository --repository-name ${{ env.APP_NAME }}-${{ matrix.containerfile_targets }}
163175
echo "Setting lifecycle policy..."
164176
else
165-
echo "Repository ${{ github.event.deployment.payload.name }}-${{ matrix.containerfile_targets }} already exists, skipping creation"
177+
echo "Repository ${{ env.APP_NAME }}-${{ matrix.containerfile_targets }} already exists, skipping creation"
166178
fi
167179
168180
echo "Applying lifecycle policies"
@@ -171,7 +183,7 @@ jobs:
171183
{"rulePriority":2,"description":"Preserve production images","selection":{"tagStatus":"tagged","tagPatternList":["v*"],"countType":"imageCountMoreThan","countNumber":50},"action":{"type":"expire"}},
172184
{"rulePriority":3,"description":"Remove untagged images","selection":{"tagStatus":"untagged","countType":"sinceImagePushed","countUnit":"days","countNumber":7},"action":{"type":"expire"}}
173185
]}'
174-
aws ecr put-lifecycle-policy --repository-name ${{ github.event.deployment.payload.name }}-${{ matrix.containerfile_targets }} --lifecycle-policy-text "$LIFECYCLE_POLICY"
186+
aws ecr put-lifecycle-policy --repository-name ${{ env.APP_NAME }}-${{ matrix.containerfile_targets }} --lifecycle-policy-text "$LIFECYCLE_POLICY"
175187
- name: Login to Amazon ECR
176188
id: login-ecr
177189
uses: aws-actions/amazon-ecr-login@v2
@@ -181,25 +193,25 @@ jobs:
181193
build-args: |
182194
GITHUB_SHA=${{ github.sha }}
183195
VERSION=${{ inputs.version }}
184-
APP_NAME=${{ github.event.deployment.payload.name }}
185-
ENVIRONMENT=${{ github.event.deployment.payload.env }}
196+
APP_NAME=${{ env.APP_NAME }}
197+
ENVIRONMENT=${{ env.ENVIRONMENT }}
186198
NPM_GITHUB_TOKEN=${{ secrets.npmGithubReadToken }}
187-
cache-from: type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}:cache
188-
cache-to: mode=max,image-manifest=true,oci-mediatypes=true,type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}:cache
199+
cache-from: type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}:cache
200+
cache-to: mode=max,image-manifest=true,oci-mediatypes=true,type=registry,ref=${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}:cache
189201
context: ${{ github.event.deployment.payload.container.context }}
190202
load: true
191203
file: ${{ github.event.deployment.payload.container.file }}
192204
platforms: linux/amd64
193205
tags: |
194-
${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}-${{ matrix.containerfile_targets }}:latest
195-
${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}-${{ matrix.containerfile_targets }}:${{ inputs.version }}
196-
${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}-${{ matrix.containerfile_targets }}:${{ github.sha }}
206+
${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}-${{ matrix.containerfile_targets }}:latest
207+
${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}-${{ matrix.containerfile_targets }}:${{ inputs.version }}
208+
${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}-${{ matrix.containerfile_targets }}:${{ github.sha }}
197209
target: ${{ matrix.containerfile_targets }}
198210
- name: Scan for vulnerabilities
199211
if: inputs.enableContainerScan
200212
uses: crazy-max/ghaction-container-scan@v3
201213
with:
202-
image: ${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}-${{ matrix.containerfile_targets }}:latest
214+
image: ${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}-${{ matrix.containerfile_targets }}:latest
203215
dockerfile: Containerfile
204216
severity: ${{ env.IMAGE_SCAN_SEVERITY }}
205217
severity_threshold: ${{ env.IMAGE_SCAN_SEVERITY_THRESHOLD }}
@@ -208,4 +220,4 @@ jobs:
208220
TRIVY_TIMEOUT: ${{ env.IMAGE_SCAN_TRIVY_TIMEOUT }}
209221
- name: Push ${{ matrix.containerfile_targets }} image to ECR
210222
run: |
211-
docker push -a ${{ steps.login-ecr.outputs.registry }}/${{ github.event.deployment.payload.name }}-${{ matrix.containerfile_targets }}
223+
docker push -a ${{ steps.login-ecr.outputs.registry }}/${{ env.APP_NAME }}-${{ matrix.containerfile_targets }}

0 commit comments

Comments
 (0)