-
Notifications
You must be signed in to change notification settings - Fork 4
191 lines (170 loc) · 6.13 KB
/
github-service-build.yml
File metadata and controls
191 lines (170 loc) · 6.13 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
name: Build github-service
on:
push:
branches: [main, dev]
paths:
- "github-service/**"
- ".github/workflows/github-service-build.yml"
- ".github/workflows/github-service-deploy.yml"
pull_request:
branches: [main, dev]
paths:
- "github-service/**"
- ".github/workflows/github-service-build.yml"
- ".github/workflows/github-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 }}-github-service
jobs:
lint:
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-2vcpu-ubuntu-2404
permissions:
contents: read
actions: write
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: Setup pnpm
uses: pnpm/action-setup@v5
with:
version: 10
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: 25
cache: "pnpm"
cache-dependency-path: github-service/pnpm-lock.yaml
- name: Install dependencies
working-directory: ./github-service
run: pnpm install --frozen-lockfile
- name: Lint code
working-directory: ./github-service
run: |
pnpm lint
git diff --exit-code ..
- name: Cancel workflow on failure
if: failure()
run: gh run cancel ${{ github.run_id }}
env:
GH_TOKEN: ${{ github.token }}
build:
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
actions: write
outputs:
short_sha: ${{ steps.short-sha.outputs.short_sha }}
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 metadata
id: meta
uses: docker/metadata-action@v6
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr,prefix=pr-
type=sha,format=short
- name: Build image (PR - no push)
if: github.event_name == 'pull_request'
uses: useblacksmith/build-push-action@v2
with:
context: ./github-service
file: ./github-service/Dockerfile
platforms: ${{ matrix.platform }}
push: false
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Build and push Docker image
if: github.event_name != 'pull_request'
uses: useblacksmith/build-push-action@v2
with:
context: ./github-service
file: ./github-service/Dockerfile
platforms: ${{ matrix.platform }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Extract short SHA for deployment
id: short-sha
run: |
SHORT_SHA=$(echo "${{ steps.meta.outputs.tags }}" | grep -o '[a-f0-9]\{7\}' | head -1)
if [ -z "$SHORT_SHA" ]; then
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
fi
echo "short_sha=$SHORT_SHA" >> $GITHUB_OUTPUT
echo "Extracted short SHA: $SHORT_SHA"
- name: Output image details
run: |
if [ "${{ github.event_name }}" != "pull_request" ]; then
echo "## ✅ github-service Image Built and Pushed" >> $GITHUB_STEP_SUMMARY
else
echo "## 🔨 github-service Image Built (Not Pushed)" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Image:** ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" >> $GITHUB_STEP_SUMMARY
echo "**Tags:** ${{ steps.meta.outputs.tags }}" >> $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
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo "" >> $GITHUB_STEP_SUMMARY
echo "ℹ️ **Note:** Image was built for testing but not pushed to registry" >> $GITHUB_STEP_SUMMARY
fi
- name: Cancel workflow on failure
if: failure()
run: gh run cancel ${{ github.run_id }}
env:
GH_TOKEN: ${{ github.token }}
deploy:
needs: [lint, build]
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
uses: ./.github/workflows/github-service-deploy.yml
with:
environment: ${{ github.event_name == 'workflow_dispatch' && inputs.environment || (github.ref_name == 'main' && 'prod' || 'dev') }}
secrets: inherit