Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Docker setup #23

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
build
log
go.work
*.test
go.work.sum
go.work.sum
47 changes: 47 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Build and Publish Docker Image

on:
push:
pull_request:
workflow_dispatch:

jobs:
build-publish:
env:
SHOULD_PUSH_IMAGE: ${{ (github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/dev')) || github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest

steps:
- name: Set up QEMU for Docker
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log into the Docker container registry
if: ${{ env.SHOULD_PUSH_IMAGE == 'true' }}
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ github.repository }}
tags: |
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/master' }}
type=raw,value=edge,enable=${{ github.ref == 'refs/heads/dev' }}
type=sha

- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64
push: ${{ env.SHOULD_PUSH_IMAGE }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
47 changes: 32 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
# --- builder ---
FROM golang:1.20.6-alpine3.17 as builder
LABEL stage=builder
RUN apk add git
WORKDIR /build
# syntax=docker/dockerfile:1

COPY go.* ./
RUN go mod download
ARG app_dir="/home/go/app"

COPY . ./
ARG BUILD_STRING=pretendo.friends.docker
RUN go build -ldflags "-X 'main.serverBuildString=${BUILD_STRING}'" -v -o server

# --- runner ---
FROM alpine:3.17 as runner
WORKDIR /build
# * Building the application
FROM golang:1.22-alpine3.20 AS build
ARG app_dir
ARG build_string=pretendo.friends.docker

COPY --from=builder /build/server /build/
CMD ["/build/server"]
WORKDIR ${app_dir}

RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=bind,source=go.sum,target=go.sum \
--mount=type=bind,source=go.mod,target=go.mod \
go mod download -x

COPY . .
RUN --mount=type=cache,target=/go/pkg/mod/ \
CGO_ENABLED=0 go build -v -o ${app_dir}/build/server -ldflags "-X 'main.serverBuildString=${build_string}'"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, what is the build string? Is this a Go/Pretendo thing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a Pretendo-specific thing. @jonbarrow can explain more accurately about what it does.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@binaryoverload Every game server has 2 servers, the authentication server and the real server (called the "secure server"). The client must first pass through the authentication server before being given the address of the secure server

In the login methods the client uses for the authentication server, the servers build string is sent. It contains information about the server like it's build number, branch, NEX version, etc.

Most games don't care what is set here, but some do scan the build string for relevant data. So it must be set. For example Mario Kart 8 has the following build string branch:origin/project/nfs build:3_10_26_2006_0



# * Running the final application
FROM alpine:3.20 AS final
ARG app_dir
WORKDIR ${app_dir}

RUN addgroup go && adduser -D -G go go

RUN mkdir -p ${app_dir}/log && chown go:go ${app_dir}/log

USER go

COPY --from=build ${app_dir}/build/server ${app_dir}/server

CMD [ "./server" ]