Skip to content

Commit ae8ea67

Browse files
committedAug 18, 2021
ci(docker): move docker build to the actions
1 parent 10a60c7 commit ae8ea67

File tree

5 files changed

+164
-37
lines changed

5 files changed

+164
-37
lines changed
 

‎.github/workflows/registry.yml

+66-4
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,80 @@ on:
88
env:
99
REGISTRY: ghcr.io
1010
IMAGE_NAME: ${{ github.repository }}
11-
BUILDX_VERSIONS: linux/amd64
11+
BUILDX_VERSIONS: linux/amd64,linux/arm64,linux/arm/v7
12+
GO_VERSION: 1.17
1213

1314
jobs:
14-
build-and-push-image:
15+
build-go:
1516
runs-on: ubuntu-latest
1617
permissions:
1718
contents: read
18-
packages: write
19-
2019
steps:
2120
- name: Checkout repository
2221
uses: actions/checkout@v2
2322
with:
2423
fetch-depth: 2
2524

25+
- name: Setup go
26+
id: go
27+
uses: actions/setup-go@v2
28+
with:
29+
go-version: ${{ env.GO_VERSION }}
30+
31+
- name: Cache go
32+
uses: actions/cache@v2.1.6
33+
with:
34+
path: |
35+
~/.cache/go-build
36+
~/go/pkg/mod
37+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
38+
restore-keys: |
39+
${{ runner.os }}-go-
40+
41+
- name: Cache app build
42+
id: app_cache
43+
uses: actions/cache@v2.1.6
44+
with:
45+
path: ./bin/
46+
key: ${{ runner.os }}-webapp-${{ hashFiles('bin') }}
47+
48+
- name: Build go binaries
49+
run: |
50+
export CGO_ENABLED=0
51+
for TARGETPLATFORM in $( echo ${{ env.BUILDX_VERSIONS }} | tr -s ',' ' ' ); do
52+
time go build -ldflags="-d -s -w" -o ./bin/${TARGETPLATFORM}/postmanq -a cmd/postmanq/main.go &
53+
time go build -ldflags="-d -s -w" -o ./bin/${TARGETPLATFORM}/pmq-grep -a cmd/tools/pmq-grep/main.go &
54+
time go build -ldflags="-d -s -w" -o ./bin/${TARGETPLATFORM}/pmq-publish -a cmd/tools/pmq-publish/main.go &
55+
time go build -ldflags="-d -s -w" -o ./bin/${TARGETPLATFORM}/pmq-report -a cmd/tools/pmq-report/main.go &
56+
time go build -ldflags="-d -s -w" -o ./bin/${TARGETPLATFORM}/healthcheck -a cmd/healthcheck/main.go &
57+
done
58+
wait
59+
60+
- uses: actions/upload-artifact@v2
61+
with:
62+
name: application
63+
path: |
64+
./bin/
65+
./build/
66+
./config.yaml
67+
68+
build-and-publish-image:
69+
needs: build-go
70+
runs-on: ubuntu-latest
71+
strategy:
72+
matrix:
73+
include:
74+
- docker: postman
75+
tagSuffix: ""
76+
- docker: postman/alpine
77+
tagSuffix: "-alpine"
78+
permissions:
79+
packages: write
80+
steps:
81+
- uses: actions/download-artifact@v2
82+
with:
83+
name: application
84+
2685
- name: Set up QEMU
2786
uses: docker/setup-qemu-action@v1
2887

@@ -45,6 +104,8 @@ jobs:
45104
type=ref,event=branch
46105
type=ref,event=pr
47106
type=semver,pattern={{major}}.{{minor}}.{{patch}}
107+
flavor: |
108+
suffix=${{ matrix.tagSuffix }},onlatest=true
48109
49110
- name: Cache Docker layers
50111
uses: actions/cache@v2.1.6
@@ -59,6 +120,7 @@ jobs:
59120
id: docker_build
60121
uses: docker/build-push-action@v2
61122
with:
123+
file: build/${{ matrix.docker }}/Dockerfile
62124
context: .
63125
cache-from: type=local,src=/tmp/.buildx-cache
64126
cache-to: type=local,dest=/tmp/.buildx-cache-new

‎Dockerfile

-33
This file was deleted.

‎build/postman/Dockerfile

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
FROM golang:1.17-alpine as builder
2+
3+
RUN addgroup -g 1001 postmanq && \
4+
adduser -S -u 1001 -G postmanq postmanq && \
5+
mkdir /app
6+
7+
ARG TARGETPLATFORM
8+
COPY ./bin/${TARGETPLATFORM}/ /app/
9+
COPY ./config.yaml /etc/postman.yaml
10+
11+
RUN chown -R 1001:1001 /app && \
12+
chown 1001:1001 /etc/postman.yaml && \
13+
chmod +x /app/*
14+
15+
FROM scratch
16+
WORKDIR /
17+
18+
COPY --from=builder /etc/passwd /etc/passwd
19+
COPY --from=builder /etc/group /etc/group
20+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
21+
COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip /usr/local/go/lib/time/zoneinfo.zip
22+
COPY --from=builder /etc/postman.yaml /etc/postman.yaml
23+
COPY --from=builder /app/postmanq /postmanq
24+
COPY --from=builder /app/pmq-grep /pmq-grep
25+
COPY --from=builder /app/pmq-publish /pmq-publish
26+
COPY --from=builder /app/pmq-report /pmq-report
27+
COPY --from=builder /app/healthcheck /healthcheck
28+
29+
USER postmanq:postmanq
30+
31+
ENV PORT=1080
32+
EXPOSE $PORT
33+
34+
HEALTHCHECK --interval=5s --timeout=1s --start-period=2s --retries=3 CMD [ "/healthcheck" ]
35+
36+
CMD ["/postmanq", "-f", "/etc/postman.yaml"]

‎build/postman/alpine/Dockerfile

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
FROM golang:1.17-alpine as builder
2+
3+
RUN addgroup -g 1001 postmanq && \
4+
adduser -S -u 1001 -G postmanq postmanq && \
5+
mkdir /app
6+
7+
ARG TARGETPLATFORM
8+
COPY ./bin/${TARGETPLATFORM}/ /app/
9+
COPY ./config.yaml /etc/postman.yaml
10+
11+
RUN chown -R 1001:1001 /app && \
12+
chown 1001:1001 /etc/postman.yaml && \
13+
chmod +x /app/*
14+
15+
FROM alpine:3.14
16+
WORKDIR /
17+
18+
COPY --from=builder /etc/passwd /etc/passwd
19+
COPY --from=builder /etc/group /etc/group
20+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
21+
COPY --from=builder /usr/local/go/lib/time/zoneinfo.zip /usr/local/go/lib/time/zoneinfo.zip
22+
COPY --from=builder /etc/postman.yaml /etc/postman.yaml
23+
COPY --from=builder /app/postmanq /usr/bin/postmanq
24+
COPY --from=builder /app/pmq-grep /usr/bin/pmq-grep
25+
COPY --from=builder /app/pmq-publish /usr/bin/pmq-publish
26+
COPY --from=builder /app/pmq-report /usr/bin/pmq-report
27+
28+
USER postmanq:postmanq
29+
30+
ENV PORT=1080
31+
EXPOSE $PORT
32+
33+
HEALTHCHECK --interval=5s --timeout=1s --start-period=2s --retries=3 CMD wget -nv -t1 --spider http://localhost:${PORT}/health || exit 1
34+
35+
CMD ["/usr/bin/postmanq", "-f", "/etc/postman.yaml"]

‎cmd/healthcheck/main.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
"time"
9+
)
10+
11+
func main() {
12+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
13+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%s/health", os.Getenv("PORT")), nil)
14+
if err != nil {
15+
cancel()
16+
os.Exit(1)
17+
}
18+
19+
res, err := http.DefaultClient.Do(req)
20+
if err != nil {
21+
cancel()
22+
os.Exit(1)
23+
}
24+
25+
_ = res.Body.Close()
26+
cancel()
27+
}

0 commit comments

Comments
 (0)
Please sign in to comment.