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

Cloud build script for simple-game-server #3314

Merged
merged 8 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
19 changes: 14 additions & 5 deletions examples/simple-game-server/Dockerfile.windows
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@
# Use `--build-arg WINDOWS_VERSION=` to select the correct base image
# See https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/version-compatibility
# for details.

# Build Stage
ARG WINDOWS_VERSION=ltsc2019

FROM --platform=linux/amd64 golang:1.20.4 as base
Copy link
Member

Choose a reason for hiding this comment

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

Nit: "builder" is more common.

Suggested change
FROM --platform=linux/amd64 golang:1.20.4 as base
FROM --platform=linux/amd64 golang:1.20.4 as builder

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated with builder


WORKDIR /gopath/src
COPY . /gopath/src/
Copy link
Member

Choose a reason for hiding this comment

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

Let's make this the same as

FROM golang:1.20.4 as builder
WORKDIR /go/src
COPY . agones.dev/agones
WORKDIR /go/src/agones.dev/agones/examples/simple-game-server
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o server .
for consistency.

Suggested change
WORKDIR /gopath/src
COPY . /gopath/src/
WORKDIR /go/src
COPY . /go/src/

Copy link
Contributor Author

@Kalaiselvi84 Kalaiselvi84 Aug 14, 2023

Choose a reason for hiding this comment

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

The Linux build is functioning properly, but the Windows build has an issue: there are no files in /go/src/agones.dev/agones/examples/simple-game-server. This is due to the COPY . agones.dev/agones command, which only copies files from the simple-game-server. Log details can be found here. Linux build is failing if we adjust the work directory path in cloudbuild.yaml file.

Step #2 - "push": #9 [base 2/8] WORKDIR /go/src
Step #2 - "push": #9 DONE 0.7s
Step #2 - "push": 
Step #2 - "push": #10 [base 3/8] COPY . agones.dev/agones
Step #2 - "push": #10 DONE 0.1s
Step #2 - "push": 
Step #2 - "push": #11 [base 4/8] RUN ls -ltr
Step #2 - "push": #0 0.150 total 4
Step #2 - "push": #0 0.150 drwxr-xr-x 3 root root 4096 Aug 14 19:19 agones.dev
Step #2 - "push": #11 DONE 0.2s
Step #2 - "push": 
Step #2 - "push": 
Step #2 - "push": #12 [base 5/8] RUN ls -ltr /go/src/agones.dev/agones
Step #2 - "push": #0 0.135 total 88
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  1129 Aug  4 01:08 gameserverallocation.yaml
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  1080 Aug  4 01:08 gameserver.yaml
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  1183 Aug  4 01:08 gameserver-windows.yaml
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  1131 Aug  4 01:08 gameserver-passthrough.yaml
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root   945 Aug  4 01:08 fleetautoscaler.yaml
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  1136 Aug  4 01:08 fleet.yaml
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  1406 Aug  4 01:08 fleet-tcp.yaml
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  1346 Aug  4 01:08 fleet-distributed.yaml
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  1200 Aug  4 01:08 dev-gameserver.yaml
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  3682 Aug  4 01:08 README.md
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  1017 Aug  4 01:08 Dockerfile
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  2507 Aug  7 17:13 go.sum
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root   578 Aug  7 17:13 go.mod
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root 16836 Aug 10 13:15 main.go
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  1300 Aug 12 19:22 cloudbuild.yaml
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  5481 Aug 12 19:22 Makefile
Step #2 - "push": #0 0.135 -rw-r--r-- 1 root root  1661 Aug 14 19:14 Dockerfile.windows
Step #2 - "push": #12 DONE 0.2s
Step #2 - "push": 
Step #2 - "push": #13 [base 6/8] WORKDIR /go/src/agones.dev/agones/examples/simple-game-server
Step #2 - "push": #13 DONE 0.0s
Step #2 - "push": 
Step #2 - "push": #14 [base 7/8] RUN ls -ltr
Step #2 - "push": #14 0.153 total 0
Step #2 - "push": #14 DONE 0.2s

Copy link
Member

@markmandel markmandel Aug 15, 2023

Choose a reason for hiding this comment

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

🤔 so the line that fails is:

Step #2 - "push": #15 0.339 agones.dev/agones@v1.33.0 (replaced by ../../): reading /go/src/go.mod: open /go/src/go.mod: no such file or directory

I think I see the issue, it's in the makefile.

You can see how the linux image is built to run from the $(root_path):

build-linux-image-amd64:
cd $(root_path) && docker build -f $(project_path)Dockerfile --tag=$(REPOSITORY)$(server_tag_linux_amd64) .
build-linux-image-arm64:
cd $(root_path) && docker buildx build --platform linux/arm64 -f $(project_path)Dockerfile $(DOCKER_BUILD_ARGS) --tag=$(server_tag_linux_arm64) .

But if we look at the Windows target, we see:

build-windows-image-%: ensure-windows-buildx
cd $(root_path) && DOCKER_CLI_EXPERIMENTAL=enabled \
docker buildx build --platform windows/amd64 --builder $(BUILDX_WINDOWS_BUILDER) -f $(project_path)Dockerfile.windows \
--tag=$(REPOSITORY)$(server_tag)-windows_amd64-$* --build-arg WINDOWS_VERSION=$* examples/simple-game-server/ $(WINDOWS_DOCKER_PUSH_ARGS)

Where previously there should be . there is examples/simple-game-server/. I think if you fixup that, then that will resolve the issue (or at least get you closer).

RUN dir

RUN GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o bin/simple-game-server.exe main.go
markmandel marked this conversation as resolved.
Show resolved Hide resolved

FROM mcr.microsoft.com/windows/servercore:${WINDOWS_VERSION}
WORKDIR /
RUN dir

# Unlike the Linux image the binary is built on the host since buildx cannot
# invoke RUN commands for Windows Containers and multi-stage images
# cannot copy data between OSes.
COPY ["bin/simple-game-server.exe", "/server/simple-game-server.exe"]
COPY --from=base /gopath/src/agones.dev/agones/bin/simple-game-server.exe /server

WORKDIR /server
EXPOSE 7654
USER ContainerUser
ENTRYPOINT ["C:\\server\\simple-game-server.exe"]

27 changes: 16 additions & 11 deletions examples/simple-game-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
# \_/ \__,_|_| |_|\__,_|_.__/|_|\___|___/
#

REGISTRY ?= us-docker.pkg.dev/agones-images/examples
REPOSITORY ?=
PROD_REPO ?= us-docker.pkg.dev/agones-images/examples
BUILDX_WINDOWS_BUILDER = windows-builder
# Versions of Windows Server to support, default is Windows Server 2019.
# For the full list of tags see https://hub.docker.com/_/microsoft-windows-servercore.
Expand All @@ -37,7 +38,11 @@ WITH_ARM64 ?= 1

mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
project_path := $(dir $(mkfile_path))
server_tag = $(REGISTRY)/simple-game-server:0.17
ifeq ($(REPOSITORY),)
server_tag := simple-game-server:0.17
else
server_tag := $(REGISTRY)/simple-game-server:0.17
endif

ifeq ($(WITH_WINDOWS), 1)
server_tag_linux_amd64 = $(server_tag)-linux-amd64
Expand Down Expand Up @@ -103,14 +108,10 @@ push-windows-images: $(foreach winver, $(WINDOWS_VERSIONS), push-windows-image-$
push-windows-image-%:
$(MAKE) WINDOWS_DOCKER_PUSH_ARGS=--push build-windows-image-$*

build-windows-image-%: build-windows-binary ensure-windows-buildx
build-windows-image-%: ensure-windows-buildx
cd $(root_path) && DOCKER_CLI_EXPERIMENTAL=enabled \
docker buildx build --platform windows/amd64 --builder $(BUILDX_WINDOWS_BUILDER) -f $(project_path)Dockerfile.windows \
--tag=$(server_tag)-windows_amd64-$* --build-arg WINDOWS_VERSION=$* examples/simple-game-server/ $(WINDOWS_DOCKER_PUSH_ARGS)

# Builds the server binary for Windows (amd64).
build-windows-binary:
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o bin/simple-game-server.exe main.go
--tag=$(REPOSITORY)$(server_tag)-windows_amd64-$* --build-arg WINDOWS_VERSION=$* examples/simple-game-server/ $(WINDOWS_DOCKER_PUSH_ARGS)

ensure-windows-buildx:
# TODO: Remove `--use` and specify `--builder` for each buildx command once Docker 19.03.13 is available.
Expand All @@ -122,14 +123,18 @@ ensure-windows-buildx:

# Build a docker image for the server, and tag it
build-linux-image-amd64:
cd $(root_path) && docker build -f $(project_path)Dockerfile --tag=$(server_tag_linux_amd64) .
cd $(root_path) && docker build -f $(project_path)Dockerfile --tag=$(REPOSITORY)$(server_tag_linux_amd64) .
build-linux-image-arm64:
cd $(root_path) && docker buildx build --platform linux/arm64 -f $(project_path)Dockerfile $(DOCKER_BUILD_ARGS) --tag=$(server_tag_linux_arm64) .

# check if hosted on Google Artifact Registry
gar-check:
gcloud container images describe $(server_tag)
gcloud container images describe $(PROD_REPO)/$(server_tag)

#output the tag for this image
echo-image-tag:
@echo $(server_tag)
@echo $(PROD_REPO)/$(server_tag)

# build and push the simple-game-server image with specified tag
cloud-build:
cd $(root_path) && gcloud builds submit --config=./examples/simple-game-server/cloudbuild.yaml
40 changes: 40 additions & 0 deletions examples/simple-game-server/cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

steps:
#
# Creates the initial make + docker build platform
#
- name: "ubuntu"
script: |
echo 'FROM gcr.io/cloud-builders/docker\nRUN apt-get install make\nENTRYPOINT [\"/usr/bin/make\"]' > Dockerfile.build
- name: gcr.io/cloud-builders/docker
id: build-make-docker
entrypoint: "docker"
args: ["build", "-f", "Dockerfile.build", "-t", "make-docker", "."]

# build and push simple-game-server image to GCR
- name: "make-docker"
id: push
dir: "/workspace/examples/simple-game-server"
env:
- REPOSITORY=${_REPOSITORY}
script: |
make push
options:
dynamic_substitutions: true

substitutions:
_REPOSITORY: us-docker.pkg.dev/${PROJECT_ID}/examples
timeout: 1800s
Loading