Skip to content

Commit 8642e6e

Browse files
authored
Merge pull request #2 from mrjvs/nex-go-rewrite
2 parents 3276d1e + 48dcbad commit 8642e6e

File tree

2 files changed

+87
-14
lines changed

2 files changed

+87
-14
lines changed

.github/workflows/docker.yml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Build and Publish Docker Image
2+
3+
on:
4+
push:
5+
pull_request:
6+
workflow_dispatch:
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ${{ github.repository }}
11+
12+
jobs:
13+
build-publish:
14+
env:
15+
SHOULD_PUSH_IMAGE: ${{ (github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/dev')) || github.event_name == 'workflow_dispatch' }}
16+
runs-on: ubuntu-latest
17+
18+
permissions:
19+
contents: read
20+
packages: write
21+
22+
steps:
23+
- name: Set up QEMU for Docker
24+
uses: docker/setup-qemu-action@v3
25+
26+
- name: Set up Docker Buildx
27+
uses: docker/setup-buildx-action@v3
28+
29+
- name: Log into the container registry
30+
if: ${{ env.SHOULD_PUSH_IMAGE == 'true' }}
31+
uses: docker/login-action@v3
32+
with:
33+
registry: ${{ env.REGISTRY }}
34+
username: ${{ github.actor }}
35+
password: ${{ secrets.GITHUB_TOKEN }}
36+
37+
- name: Extract Docker metadata
38+
id: meta
39+
uses: docker/metadata-action@v5
40+
with:
41+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
42+
tags: |
43+
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }}
44+
type=raw,value=edge,enable=${{ github.ref == 'refs/heads/dev' }}
45+
type=sha
46+
47+
- name: Build and push Docker image
48+
id: build-and-push
49+
uses: docker/build-push-action@v6
50+
with:
51+
platforms: linux/amd64,linux/arm64
52+
push: ${{ env.SHOULD_PUSH_IMAGE }}
53+
tags: ${{ steps.meta.outputs.tags }}
54+
labels: ${{ steps.meta.outputs.labels }}
55+
cache-from: type=gha
56+
cache-to: type=gha,mode=max

Dockerfile

+31-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1-
# --- builder ---
2-
FROM golang:1.20.6-alpine3.17 as builder
3-
LABEL stage=builder
4-
RUN apk add git
5-
WORKDIR /build
1+
# syntax=docker/dockerfile:1
62

7-
COPY go.* ./
8-
RUN go mod download
3+
ARG app_dir="/home/go/app"
94

10-
COPY . ./
5+
6+
# * Building the application
7+
FROM golang:1.22-alpine3.20 AS build
8+
ARG app_dir
9+
10+
WORKDIR ${app_dir}
11+
12+
RUN --mount=type=cache,target=/go/pkg/mod/ \
13+
--mount=type=bind,source=go.sum,target=go.sum \
14+
--mount=type=bind,source=go.mod,target=go.mod \
15+
go mod download -x
16+
17+
COPY . .
1118
ARG BUILD_STRING=pretendo.luigismansion2.docker
12-
RUN go build -ldflags "-X 'main.serverBuildString=${BUILD_STRING}'" -v -o server
19+
RUN --mount=type=cache,target=/go/pkg/mod/ \
20+
CGO_ENABLED=0 go build -ldflags "-X 'main.serverBuildString=${BUILD_STRING}'" -v -o ${app_dir}/build/server
21+
22+
23+
# * Running the final application
24+
FROM alpine:3.20 AS final
25+
ARG app_dir
26+
WORKDIR ${app_dir}
27+
28+
RUN addgroup go && adduser -D -G go go
29+
30+
RUN mkdir -p ${app_dir}/log && chown go:go ${app_dir}/log
31+
32+
USER go
1333

14-
# --- runner ---
15-
FROM alpine:3.17 as runner
16-
WORKDIR /build
34+
COPY --from=build ${app_dir}/build/server ${app_dir}/server
1735

18-
COPY --from=builder /build/server /build/
19-
CMD ["/build/server"]
36+
CMD [ "./server" ]

0 commit comments

Comments
 (0)