Add docker build workflow

This commit is contained in:
Khanh Ngo 2023-05-24 17:51:44 +02:00
parent f3ed766bc4
commit 86e52c5868
No known key found for this signature in database
GPG key ID: 29077342AA5648F6
3 changed files with 103 additions and 4 deletions

99
.github/workflows/docker-build.yml vendored Normal file
View file

@ -0,0 +1,99 @@
name: Build container images
on:
push:
branches:
- "master"
tags:
- "*"
jobs:
build-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# set environment
- name: Set APP_VERSION env
run: echo "APP_VERSION=$(echo ${GITHUB_REF} | rev | cut -d'/' -f 1 | rev )" >> $GITHUB_ENV
- name: Set BUILD_TIME env
run: echo "BUILD_TIME=$(date)" >> $GITHUB_ENV
- name: Set COMMIT env
run: echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: Environment printer
uses: managedkaos/print-env@v1.0
- name: Prepare image tags
id: image-tags
run: |
base=ngoduykhanh/wireguard-ui
app_version=dev
## Set git tag as image tag
##
if [[ '${{ github.ref }}' == *"refs/tags/"* ]]; then
github_tag="${GITHUB_REF#refs/*/}"
SEMVER_REGEX="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
if [[ "$github_tag" =~ $SEMVER_REGEX ]]; then
github_tag=$(echo "${github_tag}" | sed 's/^v//')
fi
app_version=${github_tag}
container_images=$(cat <<END_HEREDOC
${base}:${github_tag}
END_HEREDOC
)
## Set 'latest' image tag if 'main' or 'master'
## branch is pushed
##
elif [[ '${{ github.ref }}' == 'refs/heads/master' || '${{ github.ref }}' == 'refs/heads/main' ]]; then
container_images=$(cat <<END_HEREDOC
${base}:latest
END_HEREDOC
)
fi
## Print tags for debugging purpose
##
echo "[INFO] container_images: ${container_images}"
## Set container_images output
##
echo "container_images<<EOF" >> $GITHUB_OUTPUT
echo "$container_images" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
## Set APP_VERSION env
#
echo "APP_VERSION=${app_version}" >> $GITHUB_ENV
# set up docker and build images
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
context: .
platforms: linux/amd64,linux/arm/v7
tags: ${{ steps.image-tags.outputs.container_images }}
build-args: |
APP_VERSION=${{ env.APP_VERSION }}
BUILD_TIME=${{ env.BUILD_TIME }}
COMMIT=${{ env.COMMIT }}

View file

@ -4,7 +4,9 @@ LABEL maintainer="Khanh Ngo <k@ndk.name"
ARG TARGETOS=linux
ARG TARGETARCH=amd64
ARG COMMIT=
ARG APP_VERSION=dev
ARG BUILD_TIME=""
ARG COMMIT=""
ARG BUILD_DEPENDENCIES="npm \
yarn"
@ -50,7 +52,7 @@ COPY . /build
RUN cp -r /build/custom/ assets/
# Build
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="-X main.gitCommit=${COMMIT}" -a -o wg-ui .
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags="-X main.appVersion=${APP_VERSION} -X main.buildTime=${BUILD_TIME} -X main.gitCommit=${COMMIT}" -a -o wg-ui .
# Release stage
FROM alpine:3.16

2
Jenkinsfile vendored
View file

@ -1,2 +0,0 @@
@Library('ndk-jenkins-shared-libs')_
imageBuilder('wireguard-ui')