diff --git a/.dockerignore b/.dockerignore index a63fa03..d1ff676 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,5 +3,4 @@ build log go.work -*.test -go.work.sum \ No newline at end of file +go.work.sum diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..9e1ef9c --- /dev/null +++ b/.github/workflows/docker.yml @@ -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 diff --git a/Dockerfile b/Dockerfile index dcd440c..20b2b7a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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}'" + + +# * 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" ]