Skip to content

Commit c8eb5be

Browse files
authored
ci: add ECR container build (#461)
1 parent 6352163 commit c8eb5be

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Build & Push Container
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
tags:
8+
- '*'
9+
merge_group:
10+
pull_request_target:
11+
types: [labeled, synchronize, reopened, ready_for_review, opened]
12+
13+
env:
14+
PUSH_FROM_PR: >-
15+
${{ github.event_name == 'pull_request_target' &&
16+
(
17+
contains(github.event.pull_request.labels.*.name, 'push-container') ||
18+
contains(github.event.pull_request.labels.*.name, 'deploy-pr-temp-env')
19+
)
20+
}}
21+
22+
jobs:
23+
terraform:
24+
name: "ECR"
25+
runs-on: ubuntu-latest
26+
if: github.repository == 'coderamp-labs/gitingest'
27+
28+
permissions:
29+
id-token: write
30+
contents: read
31+
pull-requests: write
32+
33+
steps:
34+
- name: Checkout
35+
uses: actions/checkout@v4
36+
37+
- name: configure aws credentials
38+
uses: aws-actions/configure-aws-credentials@v4
39+
with:
40+
role-to-assume: ${{ secrets.CODERAMP_AWS_ECR_REGISTRY_PUSH_ROLE_ARN }}
41+
role-session-name: GitHub_to_AWS_via_FederatedOIDC
42+
aws-region: eu-west-1
43+
44+
- name: Set current timestamp
45+
id: vars
46+
run: |
47+
echo "timestamp=$(date +%s)" >> $GITHUB_OUTPUT
48+
echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
49+
50+
- name: Login to Amazon ECR
51+
id: login-ecr
52+
uses: aws-actions/amazon-ecr-login@v2
53+
54+
- name: Docker Meta
55+
id: meta
56+
uses: docker/metadata-action@v5
57+
with:
58+
images: |
59+
${{ secrets.ECR_REGISTRY_URL }}
60+
flavor: |
61+
latest=false
62+
tags: |
63+
type=ref,event=branch,branch=main,suffix=-${{ steps.vars.outputs.sha_short }}-${{ steps.vars.outputs.timestamp }}
64+
type=ref,event=pr,suffix=-${{ steps.vars.outputs.sha_short }}-${{ steps.vars.outputs.timestamp }}
65+
type=pep440,pattern={{raw}}
66+
67+
- name: Set up QEMU
68+
uses: docker/setup-qemu-action@v3
69+
70+
- name: Set up Docker Buildx
71+
uses: docker/setup-buildx-action@v3
72+
73+
- name: Build and push
74+
uses: docker/build-push-action@v6
75+
with:
76+
context: .
77+
platforms: linux/amd64, linux/arm64
78+
push: ${{ github.event_name != 'pull_request_target' || env.PUSH_FROM_PR == 'true' }}
79+
tags: ${{ steps.meta.outputs.tags }}
80+
labels: ${{ steps.meta.outputs.labels }}
81+
cache-from: type=gha
82+
cache-to: type=gha,mode=max

.github/workflows/docker_image.yml renamed to .github/workflows/docker-build.ghcr.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ concurrency:
1717
env:
1818
REGISTRY: ghcr.io
1919
IMAGE_NAME: ${{ github.repository }}
20-
# Now allow pushing from PRs when either 'push-container' OR 'deploy-pr-temp-env' is present:
2120
PUSH_FROM_PR: >-
22-
${{ github.event_name == 'pull_request' &&
21+
${{ github.event_name == 'pull_request_target' &&
2322
(
2423
contains(github.event.pull_request.labels.*.name, 'push-container') ||
2524
contains(github.event.pull_request.labels.*.name, 'deploy-pr-temp-env')
@@ -31,6 +30,7 @@ permissions:
3130

3231
jobs:
3332
docker-build:
33+
name: "GHCR"
3434
runs-on: ubuntu-latest
3535
permissions:
3636
contents: read
@@ -84,14 +84,14 @@ jobs:
8484
with:
8585
context: .
8686
platforms: linux/amd64, linux/arm64
87-
push: ${{ github.event_name != 'pull_request' || env.PUSH_FROM_PR == 'true' }}
87+
push: ${{ github.event_name != 'pull_request_target' || env.PUSH_FROM_PR == 'true' }}
8888
tags: ${{ steps.meta.outputs.tags }}
8989
labels: ${{ steps.meta.outputs.labels }}
9090
cache-from: type=gha
9191
cache-to: type=gha,mode=max
9292

9393
- name: Generate artifact attestation
94-
if: github.event_name != 'pull_request' || env.PUSH_FROM_PR == 'true'
94+
if: github.event_name != 'pull_request_target' || env.PUSH_FROM_PR == 'true'
9595
uses: actions/attest-build-provenance@e8998f949152b193b063cb0ec769d69d929409be # v2.4.0
9696
with:
9797
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}

0 commit comments

Comments
 (0)