-
Notifications
You must be signed in to change notification settings - Fork 4
130 lines (116 loc) · 4.53 KB
/
k8s-service-build.yml
File metadata and controls
130 lines (116 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
name: Build k8s-service
on:
push:
branches: [ main, dev ]
paths:
- "k8s-service/**"
- ".github/workflows/k8s-service-build.yml"
- ".github/workflows/k8s-service-deploy.yml"
pull_request:
branches: [ main, dev ]
paths:
- "k8s-service/**"
- ".github/workflows/k8s-service-build.yml"
- ".github/workflows/k8s-service-deploy.yml"
types: [ opened, synchronize, reopened ]
workflow_dispatch:
inputs:
environment:
description: "Environment to deploy to"
required: true
default: "dev"
type: choice
options:
- dev
- prod
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}-k8s-service
jobs:
build:
# Only run on pull requests that are cross-branch or on direct pushes
if: |
github.event_name == 'workflow_dispatch' ||
github.event_name == 'push' ||
(github.event_name == 'pull_request' &&
github.head_ref != 'dev' &&
github.event.pull_request.base.ref != github.event.pull_request.head.ref)
runs-on: blacksmith-4vcpu-ubuntu-2404
strategy:
matrix:
platform: [ linux/amd64 ]
permissions:
contents: read
packages: write
outputs:
short_sha: ${{ steps.short-sha.outputs.short_sha }}
branch_tag: ${{ steps.compute-tag.outputs.branch_tag }}
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Setup Blacksmith Builder
uses: useblacksmith/setup-docker-builder@v1
- name: Sanitize branch names
id: sanitize-branch
run: |
RAW_REF="${{ github.head_ref || github.ref_name }}"
SANITIZED=$(echo "$RAW_REF" | tr '[:upper:]' '[:lower:]' | sed 's#[^a-z0-9._-]#-#g' | sed 's/--*/-/g' | sed 's/^-\|-$//g')
echo "name=$SANITIZED" >> $GITHUB_OUTPUT
- name: Log in to Container Registry
uses: docker/login-action@v4
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract short SHA
id: short-sha
run: echo "short_sha=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
- name: Compute branch-shortsha tag
id: compute-tag
run: |
BRANCH_NAME="${{ github.ref_name }}"
SANITIZED_BRANCH=$(echo "${BRANCH_NAME}" | sed 's/[^a-zA-Z0-9._-]/-/g' | sed 's/--*/-/g' | sed 's/^-\|-$//g')
TAG="${SANITIZED_BRANCH}-${{ steps.short-sha.outputs.short_sha }}"
echo "branch_tag=${TAG}" >> $GITHUB_OUTPUT
echo "Computed tag: ${TAG}"
- name: Build image (PR - no push)
if: github.event_name == 'pull_request'
uses: useblacksmith/build-push-action@v2
with:
context: ./k8s-service
file: ./k8s-service/Dockerfile
platforms: ${{ matrix.platform }}
push: false
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.compute-tag.outputs.branch_tag }}
- name: Build and push Docker image
if: github.event_name != 'pull_request'
uses: useblacksmith/build-push-action@v2
with:
context: ./k8s-service
file: ./k8s-service/Dockerfile
platforms: ${{ matrix.platform }}
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.compute-tag.outputs.branch_tag }}
- name: Output image details
run: |
if [ "${{ github.event_name }}" != "pull_request" ]; then
echo "## ✅ k8s-service Image Built and Pushed" >> $GITHUB_STEP_SUMMARY
else
echo "## 🔨 k8s-service Image Built (Not Pushed)" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Image:** ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" >> $GITHUB_STEP_SUMMARY
echo "**Tag:** ${{ steps.compute-tag.outputs.branch_tag }}" >> $GITHUB_STEP_SUMMARY
echo "**Short SHA:** ${{ steps.short-sha.outputs.short_sha }}" >> $GITHUB_STEP_SUMMARY
echo "**Event:** ${{ github.event_name }}" >> $GITHUB_STEP_SUMMARY
echo "**Branch:** ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
deploy:
needs: build
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
uses: ./.github/workflows/k8s-service-deploy.yml
with:
environment: ${{ github.event_name == 'workflow_dispatch' && inputs.environment || (github.ref_name == 'main' && 'prod' || 'dev') }}
secrets: inherit