Skip to content

Commit bbd5516

Browse files
committed
ci(workflow): add GitHub Actions for building and publishing Docker images
This commit introduces a new GitHub Actions workflow to automate the process of building and publishing Docker images. The workflow triggers on pushes to the main branch, workflow dispatch events, and certain pull request activities. It includes steps for checking out code, setting up Docker Buildx, logging into container registries (GitHub and Docker Hub), building the Docker image, and signing it with cosign. The Docker image is built for multiple platforms and pushed to both GitHub Container Registry and Docker Hub.
1 parent 1aa70d3 commit bbd5516

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Build and Push Docker image
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
workflow_dispatch:
9+
pull_request:
10+
types: [opened, synchronize, reopened, edited]
11+
12+
jobs:
13+
build-and-push:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
packages: write
18+
id-token: write # For cosign
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
24+
- name: Set up Docker Buildx
25+
uses: docker/setup-buildx-action@v3
26+
27+
- name: Log in to GitHub Container Registry
28+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
29+
uses: docker/login-action@v3
30+
with:
31+
registry: ghcr.io
32+
username: ${{ github.actor }}
33+
password: ${{ secrets.GITHUB_TOKEN }}
34+
35+
- name: Log in to Docker Hub
36+
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main'
37+
uses: docker/login-action@v3
38+
with:
39+
registry: docker.io
40+
username: ${{ secrets.DOCKERHUB_USERNAME }}
41+
password: ${{ secrets.DOCKERHUB_TOKEN }}
42+
43+
- name: Build and push Docker image
44+
uses: docker/build-push-action@v6
45+
id: docker_build
46+
with:
47+
context: .
48+
file: ./docker/Dockerfile
49+
push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
50+
cache-from: type=gha
51+
cache-to: type=gha,mode=max
52+
tags: |
53+
ghcr.io/obeone/multi-registry-cache:latest
54+
docker.io/obeoneorg/multi-registry-cache:latest
55+
platforms: linux/amd64,linux/arm64,linux/arm/v8,linux/arm/v6,linux/arm/v7,linux/i386
56+
57+
- name: Set up cosign
58+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
59+
uses: sigstore/cosign-installer@v3
60+
61+
- name: Sign the container image with cosign
62+
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
63+
run: |
64+
cosign sign --yes ghcr.io/obeone/multi-registry-cache@${DIGEST}
65+
cosign sign --yes docker.io/obeoneorg/multi-registry-cache@${DIGEST}
66+
env:
67+
COSIGN_EXPERIMENTAL: true
68+
DIGEST: ${{ steps.docker_build.outputs.digest }}

0 commit comments

Comments
 (0)