Skip to content

Commit d2ab505

Browse files
committed
ci: add GitHub Actions workflow for building and publishing
This workflow: - Builds container images using ko - Publishes images to GitHub Container Registry on main branch pushes - Publishes both container images and Helm charts on tag releases - Only builds images (no push) for pull requests
1 parent d49e630 commit d2ab505

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: Build and Publish
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
tags: [ 'v*' ]
7+
pull_request:
8+
branches: [ main ]
9+
10+
env:
11+
KO_DOCKER_REPO: ghcr.io/${{ github.repository }}/controller
12+
13+
jobs:
14+
build-and-push:
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: read
18+
packages: write
19+
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
24+
- name: Set up Go
25+
uses: actions/setup-go@v4
26+
with:
27+
go-version: '1.23'
28+
29+
- name: Install Dependencies
30+
run: |
31+
go mod download
32+
go install sigs.k8s.io/controller-tools/cmd/[email protected]
33+
go install sigs.k8s.io/kustomize/kustomize/[email protected]
34+
go install github.com/google/ko@latest
35+
36+
- name: Run tests
37+
run: make test WHAT=unit
38+
39+
- name: Log in to GitHub Container Registry
40+
if: github.event_name != 'pull_request'
41+
uses: docker/login-action@v3
42+
with:
43+
registry: ghcr.io
44+
username: ${{ github.actor }}
45+
password: ${{ secrets.GITHUB_TOKEN }}
46+
47+
- name: Set Release Version
48+
run: |
49+
if [[ $GITHUB_REF == refs/tags/v* ]]; then
50+
# Trim the 'v' prefix from the tag
51+
echo "RELEASE_VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
52+
else
53+
echo "RELEASE_VERSION=dev-$(git rev-parse --short HEAD)" >> $GITHUB_ENV
54+
fi
55+
56+
# Build and push image on push to main
57+
- name: Build and Push Image
58+
if: github.event_name != 'pull_request'
59+
run: |
60+
make publish-image
61+
62+
# Build image only on PR
63+
- name: Build Image (PR)
64+
if: github.event_name == 'pull_request'
65+
run: |
66+
make build-image
67+
68+
# Push helm chart only on tag
69+
- name: Package and Push Helm Chart
70+
if: github.ref_type == 'tag'
71+
run: |
72+
echo "${{ secrets.GITHUB_TOKEN }}" | helm registry login ghcr.io -u ${{ github.actor }} --password-stdin
73+
# Use sed compatible with Linux (GitHub Actions runners)
74+
cp ./config/crd/bases/* helm/crds/
75+
sed -i "s/tag: .*/tag: \"${RELEASE_VERSION}\"/" helm/values.yaml
76+
sed -i "s/version: .*/version: ${RELEASE_VERSION}/" helm/Chart.yaml
77+
sed -i "s/appVersion: .*/appVersion: \"${RELEASE_VERSION}\"/" helm/Chart.yaml
78+
helm package helm
79+
HELM_IMAGE=ghcr.io/${{ github.repository }} make publish-helm

0 commit comments

Comments
 (0)