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

Docker Action #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
67 changes: 67 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Build and Push Docker Image

on:
push:
branches: [ "master" ]
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "master" ]

env:
REGISTRY: docker.io
IMAGE_NAME: ${{ vars.DOCKERHUB_IMAGENAME }}

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

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

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

- name: Log into Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=${{ github.head_ref || github.ref_name }} # source or commit branch name

- name: Git Short SHA
id: git_vars
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: docker/multistage/Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
GIT_VERSION=${{ github.ref_name }}
GIT_COMMIT=${{ github.sha }}
GIT_COMMIT_SHORT=${{ steps.git_vars.outputs.sha_short }}

20 changes: 11 additions & 9 deletions docker/multistage/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
# Version 1.3

# Stage 1. Building service
FROM golang:latest as builder
FROM --platform=$BUILDPLATFORM golang:latest as builder
WORKDIR /go/src/rbaskets
COPY . .
RUN GIT_VERSION="$(git describe --dirty='*' || git symbolic-ref -q --short HEAD)" \
&& GIT_COMMIT="$(git rev-parse HEAD)" \
&& GIT_COMMIT_SHORT="$(git rev-parse --short HEAD)" \
&& set -x \
&& CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo \
-ldflags="-w -s -X main.GitVersion=${GIT_VERSION} -X main.GitCommit=${GIT_COMMIT} -X main.GitCommitShort=${GIT_COMMIT_SHORT}" \
-o /go/bin/rbaskets
ARG TARGETARCH
ARG GIT_VERSION
ARG GIT_COMMIT
ARG GIT_COMMIT_SHORT

RUN set -x \
&& CGO_ENABLED=0 GOOS=linux GOARCH=$TARGETARCH go build -a -installsuffix cgo \
-ldflags="-w -s -X main.GitVersion=${GIT_VERSION} -X main.GitCommit=${GIT_COMMIT} -X main.GitCommitShort=${GIT_COMMIT_SHORT}" \
-o /go/bin/rbaskets

# Stage 2. Packaging into alpine
FROM alpine:latest
FROM --platform=$TARGETPLATFORM alpine:latest
LABEL maintainer="[email protected]"
RUN apk --no-cache add ca-certificates
VOLUME /var/lib/rbaskets
Expand Down