Skip to content

Commit 040d1f5

Browse files
authored
Merge pull request #19 from TheMeier/gha
chore: add github action to publish versioned docker images to ghcr.io
2 parents 60bdb03 + a566979 commit 040d1f5

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

.github/workflows/ci.yaml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
name: CI
22

33
on:
4-
push:
5-
branches: [ master ]
64
pull_request:
75

6+
permissions:
7+
contents: read
8+
packages: write
9+
810
jobs:
911
docker-build-push:
1012
runs-on: ubuntu-latest
@@ -16,8 +18,9 @@ jobs:
1618
with:
1719
registry: ghcr.io
1820
username: ${{github.actor}}
19-
password: ${{secrets.GITHUB_TOKEN}}
20-
- name: 'Build Inventory Image'
21+
password: ${{github.token}}
22+
- name: 'Build and Push Image'
2123
run: |
22-
docker build . --tag ghcr.io/$GITHUB_REPOSITORY_OWNER/downdetector-exporter:latest
23-
docker push ghcr.io/$GITHUB_REPOSITORY_OWNER/downdetector-exporter:latest
24+
IMAGE_NAMESPACE=$(echo "$GITHUB_ACTOR" | tr '[:upper:]' '[:lower:]')
25+
docker build . --tag ghcr.io/${IMAGE_NAMESPACE}/downdetector-exporter:latest
26+
docker push ghcr.io/${IMAGE_NAMESPACE}/downdetector-exporter:latest

.github/workflows/release.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
permissions:
9+
contents: write
10+
packages: write
11+
12+
jobs:
13+
docker-build-push:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: 'Pull Code'
17+
uses: actions/checkout@main
18+
19+
- name: 'Login to GitHub Container Registry'
20+
uses: docker/login-action@v3
21+
with:
22+
registry: ghcr.io
23+
username: ${{ github.actor }}
24+
password: ${{ github.token }}
25+
26+
- name: 'Build and Push Image'
27+
run: |
28+
IMAGE_NAMESPACE=$(echo "$GITHUB_REPOSITORY_OWNER" | tr '[:upper:]' '[:lower:]')
29+
docker build \
30+
--build-arg VERSION=${{ github.ref_name }} \
31+
--tag ghcr.io/${IMAGE_NAMESPACE}/downdetector-exporter:${{ github.ref_name }} \
32+
--tag ghcr.io/${IMAGE_NAMESPACE}/downdetector-exporter:latest \
33+
.
34+
docker push ghcr.io/${IMAGE_NAMESPACE}/downdetector-exporter:${{ github.ref_name }}
35+
docker push ghcr.io/${IMAGE_NAMESPACE}/downdetector-exporter:latest
36+
release:
37+
runs-on: ubuntu-latest
38+
needs: docker-build-push
39+
steps:
40+
- name: Checkout repository
41+
uses: actions/checkout@main
42+
with:
43+
fetch-depth: 0
44+
- name: Create Release Page
45+
shell: bash
46+
env:
47+
GH_TOKEN: ${{ github.token }}
48+
run: gh release create ${{ github.ref_name }} --generate-notes

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ COPY go.mod go.sum ./
66
RUN go mod download
77

88
COPY *.go ./
9-
RUN CGO_ENABLED=0 GOOS=linux go build -o /downdetector-exporter
9+
10+
ARG VERSION=dev
11+
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o /downdetector-exporter
1012

1113
FROM gcr.io/distroless/base-debian13 AS build-release-stage
1214

downdetector-exporter.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ const (
3333
)
3434

3535
var (
36-
lg = log.NewLogfmtLogger(os.Stdout)
36+
// version is set via ldflags during build
37+
version = "dev"
38+
lg = log.NewLogfmtLogger(os.Stdout)
3739

3840
// fields for metrics request. If expanded, struct CompanySet needs to be expanded accordingly
3941
fieldsToReturn = []string{"id", "name", "slug", "baseline_current", "country_iso", "stats_24", "stats_60", "status"}
@@ -156,6 +158,12 @@ func main() {
156158

157159
// TODO: - value checking
158160
// app is a command line parser
161+
cli.VersionFlag = &cli.BoolFlag{
162+
Name: "version",
163+
Aliases: []string{"V"},
164+
Usage: "print version information and exit",
165+
}
166+
159167
app := &cli.App{
160168
Authors: []*cli.Author{
161169
{
@@ -166,6 +174,7 @@ func main() {
166174
Commands: nil,
167175
ArgsUsage: " ",
168176
Name: "downdetector-exporter",
177+
Version: version,
169178
Usage: "report metrics of downdetector api",
170179
Flags: []cli.Flag{
171180
&cli.StringFlag{

0 commit comments

Comments
 (0)