Skip to content

Commit 3fd1187

Browse files
committed
build and push to ECR workflow updated
1 parent 68a0e89 commit 3fd1187

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
name: Build and Push to ECR
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
ecr_registry:
7+
description: 'ECR Registry URL (e.g., 123456789012.dkr.ecr.us-east-1.amazonaws.com)'
8+
required: true
9+
type: string
10+
ecr_repository:
11+
description: 'ECR Repository Name (e.g., node-app)'
12+
required: true
13+
default: 'node-app'
14+
type: string
15+
image_tag:
16+
description: 'Image tag (e.g., latest, v1.0.0, staging) - leave empty to use commit SHA'
17+
required: false
18+
type: string
19+
20+
jobs:
21+
build:
22+
name: 'Build Docker Image'
23+
runs-on: ubuntu-latest
24+
outputs:
25+
image_tag: ${{ steps.set-tag.outputs.tag }}
26+
27+
steps:
28+
- name: Checkout Code
29+
uses: actions/checkout@v4
30+
31+
- name: Configure AWS credentials
32+
uses: aws-actions/configure-aws-credentials@v1
33+
with:
34+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
35+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
36+
aws-region: ${{ secrets.AWS_REGION }}
37+
38+
- name: Login to Amazon ECR
39+
id: login-ecr
40+
uses: aws-actions/amazon-ecr-login@v1
41+
42+
- name: Generate image tag
43+
id: set-tag
44+
run: |
45+
if [ -z "${{ github.event.inputs.image_tag }}" ]; then
46+
SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-7)
47+
echo "tag=${SHORT_SHA}" >> $GITHUB_OUTPUT
48+
else
49+
echo "tag=${{ github.event.inputs.image_tag }}" >> $GITHUB_OUTPUT
50+
fi
51+
52+
- name: Build and tag image
53+
env:
54+
ECR_REGISTRY: ${{ github.event.inputs.ecr_registry }}
55+
ECR_REPOSITORY: ${{ github.event.inputs.ecr_repository }}
56+
IMAGE_TAG: ${{ steps.set-tag.outputs.tag }}
57+
run: |
58+
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
59+
echo "Built image: $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
60+
61+
# Save image to a tar file
62+
mkdir -p /tmp/docker-images
63+
docker save $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -o /tmp/docker-images/image.tar
64+
65+
- name: Upload Docker image
66+
uses: actions/upload-artifact@v4
67+
with:
68+
name: docker-image
69+
path: /tmp/docker-images/image.tar
70+
retention-days: 1
71+
72+
scan:
73+
name: 'Security Scan'
74+
runs-on: ubuntu-latest
75+
needs: build
76+
77+
steps:
78+
- name: Download Docker image
79+
uses: actions/download-artifact@v4
80+
with:
81+
name: docker-image
82+
path: /tmp/docker-images
83+
84+
- name: Load Docker image
85+
env:
86+
ECR_REGISTRY: ${{ github.event.inputs.ecr_registry }}
87+
ECR_REPOSITORY: ${{ github.event.inputs.ecr_repository }}
88+
IMAGE_TAG: ${{ needs.build.outputs.image_tag }}
89+
run: |
90+
docker load -i /tmp/docker-images/image.tar
91+
92+
- name: Run Trivy scan
93+
env:
94+
ECR_REGISTRY: ${{ github.event.inputs.ecr_registry }}
95+
ECR_REPOSITORY: ${{ github.event.inputs.ecr_repository }}
96+
IMAGE_TAG: ${{ needs.build.outputs.image_tag }}
97+
run: |
98+
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy:latest image --severity HIGH,CRITICAL $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
99+
100+
push:
101+
name: 'Push to ECR'
102+
runs-on: ubuntu-latest
103+
needs: [build, scan]
104+
105+
steps:
106+
- name: Configure AWS credentials
107+
uses: aws-actions/configure-aws-credentials@v1
108+
with:
109+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
110+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
111+
aws-region: ${{ secrets.AWS_REGION }}
112+
113+
- name: Login to Amazon ECR
114+
id: login-ecr
115+
uses: aws-actions/amazon-ecr-login@v1
116+
117+
- name: Download Docker image
118+
uses: actions/download-artifact@v4
119+
with:
120+
name: docker-image
121+
path: /tmp/docker-images
122+
123+
- name: Load Docker image and push
124+
env:
125+
ECR_REGISTRY: ${{ github.event.inputs.ecr_registry }}
126+
ECR_REPOSITORY: ${{ github.event.inputs.ecr_repository }}
127+
IMAGE_TAG: ${{ needs.build.outputs.image_tag }}
128+
run: |
129+
docker load -i /tmp/docker-images/image.tar
130+
131+
# Push image with unique tag
132+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
133+
134+
# Tag and push as latest
135+
docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest
136+
docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest

0 commit comments

Comments
 (0)