Skip to content

Commit c860656

Browse files
authored
feat: container image build ci to push image to github registry
Use GitHub CI to build the latest Container image on git push and store container image on GitHub registry Current docker image hosted on Dockerhub, which is having rate-limit. Under a on-prem or org network , it get exhausted easily. i.e. why added workflow to build and store it in ghcr, Github Own Container Registry Signed-off-by: Pratik Raj <[email protected]>
1 parent e52ab0a commit c860656

File tree

1 file changed

+53
-45
lines changed

1 file changed

+53
-45
lines changed

.github/workflows/image.yml

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,60 @@
1+
name: Container Image Build
2+
3+
# This workflow uses actions that are not certified by GitHub.
4+
# They are provided by a third-party and are governed by
5+
# separate terms of service, privacy policy, and support
6+
# documentation.
7+
18
on:
29
push:
3-
branches: [master]
4-
tags: ['v*']
10+
branches: [ 'master' ]
11+
# Publish semver tags as releases.
12+
tags: [ 'v*.*.*' ]
513
pull_request:
14+
branches: [ 'master' ]
15+
16+
env:
17+
# Use docker.io for Docker Hub if empty
18+
REGISTRY: ghcr.io
19+
# github.repository as <account>/<repo>
20+
IMAGE_NAME: ${{ github.repository }}
621

722
jobs:
8-
image:
23+
build:
24+
925
runs-on: ubuntu-latest
26+
permissions:
27+
contents: read
28+
packages: write
29+
1030
steps:
11-
- name: setup buildx
12-
id: buildx
13-
uses: docker/setup-buildx-action@v3
14-
with:
15-
version: latest
16-
17-
- name: login to docker hub
18-
if: github.event_name != 'pull_request'
19-
uses: docker/login-action@v1
20-
with:
21-
registry: docker.io
22-
username: ${{ secrets.DOCKER_USERNAME }}
23-
password: ${{ secrets.DOCKER_PASSWORD }}
24-
25-
- uses: actions/checkout@v4
26-
with:
27-
fetch-depth: 0
28-
29-
- name: generate docker metadata
30-
id: meta
31-
uses: docker/metadata-action@v5
32-
with:
33-
# list of Docker images to use as base name for tags
34-
images: |
35-
ncabatoff/process-exporter
36-
# generate Docker tags based on the following events/attributes
37-
tags: |
38-
type=ref,event=tag
39-
type=ref,event=branch
40-
type=ref,event=pr
41-
type=semver,pattern={{version}}
42-
type=sha
43-
44-
- name: build docker image and, if not PR, push
45-
uses: docker/build-push-action@v5
46-
with:
47-
file: ./Dockerfile
48-
context: .
49-
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64
50-
push: ${{ github.event_name != 'pull_request' }}
51-
tags: ${{ steps.meta.outputs.tags }}
52-
labels: ${{ steps.meta.outputs.labels }}
31+
- name: Checkout repository
32+
uses: actions/[email protected]
33+
34+
# Login against a Docker registry except on PR
35+
# https://github.com/docker/login-action
36+
- name: Log into registry ${{ env.REGISTRY }}
37+
if: github.event_name != 'pull_request'
38+
uses: docker/[email protected]
39+
with:
40+
registry: ${{ env.REGISTRY }}
41+
username: ${{ github.actor }}
42+
password: ${{ secrets.GITHUB_TOKEN }}
43+
44+
# Extract metadata (tags, labels) for Docker
45+
# https://github.com/docker/metadata-action
46+
- name: Extract Container metadata
47+
id: meta
48+
uses: docker/[email protected]
49+
with:
50+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
51+
52+
# Build and push Docker image with Buildx (don't push on PR)
53+
# https://github.com/docker/build-push-action
54+
- name: Build and push Container image
55+
uses: docker/[email protected]
56+
with:
57+
context: docker/
58+
push: ${{ github.event_name != 'pull_request' }}
59+
tags: ${{ steps.meta.outputs.tags }}
60+
labels: ${{ steps.meta.outputs.labels }}

0 commit comments

Comments
 (0)