-
Notifications
You must be signed in to change notification settings - Fork 4
143 lines (127 loc) · 4.81 KB
/
github-service-build.yml
File metadata and controls
143 lines (127 loc) · 4.81 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
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:
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
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@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
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
deploy:
needs: 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