-
Notifications
You must be signed in to change notification settings - Fork 1
106 lines (94 loc) · 3.55 KB
/
build.yml
File metadata and controls
106 lines (94 loc) · 3.55 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
---
name: Build & Push Images
on:
push:
branches: ["**"]
paths-ignore:
- "k8s/**"
- "**.md"
pull_request:
types: [opened, synchronize, reopened]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
env:
REGISTRY: ${{ vars.REGISTRY || 'registry.wdr.io' }}
REPO: ${{ github.event.repository.name }}
jobs:
# ---------------------------------------------------------------
# Stage 1: Validate codebase.
# Simple project has no application code to lint — just verify
# the Dockerfile and static content are well-formed.
# ---------------------------------------------------------------
validate:
name: Validate
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Lint Dockerfile
uses: hadolint/hadolint-action@v3.1.0
with:
dockerfile: silta/nginx.Dockerfile
failure-threshold: error
# ---------------------------------------------------------------
# Stage 2: Build and push Docker image.
# ---------------------------------------------------------------
build-image:
name: Build & Push — nginx
runs-on: ubuntu-latest
needs: validate
permissions:
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set image tag
id: tag
shell: bash
run: |
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME}}"
SAFE=$(echo "$BRANCH" | tr '/' '-' | tr '_' '-' | cut -c1-40)
TIMESTAMP=$(date -u +%Y%m%dT%H%M%S)
echo "tag=${SAFE}-${TIMESTAMP}-$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
echo "branch=${SAFE}" >> "$GITHUB_OUTPUT"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Harbor registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Ensure Harbor project exists
shell: bash
run: |
BODY=$(curl -s \
-u '${{ secrets.REGISTRY_USERNAME }}:${{ secrets.REGISTRY_PASSWORD }}' \
"https://${REGISTRY}/api/v2.0/projects?name=${REPO}")
if echo "$BODY" | grep -q '"project_id"'; then
echo "Project '${REPO}' already exists."
else
echo "Creating project '${REPO}'..."
curl -s -f -X POST \
-u '${{ secrets.REGISTRY_USERNAME }}:${{ secrets.REGISTRY_PASSWORD }}' \
-H "Content-Type: application/json" \
"https://${REGISTRY}/api/v2.0/projects" \
-d "{\"project_name\":\"${REPO}\",\"public\":false}" \
&& echo "Project created." \
|| echo "::warning::Failed to create project (may already exist or lack permissions)."
fi
- name: Build & push image
uses: docker/build-push-action@v6
with:
context: .
file: silta/nginx.Dockerfile
pull: true
push: true
tags: ${{ env.REGISTRY }}/${{ env.REPO }}/nginx:${{ steps.tag.outputs.tag }}
labels: |
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.branch=${{ steps.tag.outputs.branch }}
cache-from: type=gha,scope=nginx
cache-to: type=gha,scope=nginx,mode=max