Refactor workflow:

- Replace `prep` step by `docker/metadata-action`
- Use vars and secrets to support customizations for:
      - registry name
      - registry user
      - registry password
      - image name
      - image platforms

Signed-off-by: Dmytro Bondar <git@bonddim.com>
This commit is contained in:
Dmytro Bondar 2023-09-20 21:15:55 +02:00 committed by Dmytro Bondar
parent ebdd5eb8b7
commit af7c29f21a
No known key found for this signature in database
GPG Key ID: 71F0CAE1D900E84D
2 changed files with 41 additions and 49 deletions

View File

@ -1,4 +1,4 @@
name: Build multi-platform docker images via buildx
name: Docker Build

on:
pull_request:
@ -10,58 +10,50 @@ on:
tags:
- 'v*.*.*'

env:
IMAGE_NAME: ${{ vars.IMAGE_NAME || github.repository }}
IMAGE_PLATFORMS: ${{ vars.IMAGE_PLATFORMS || 'linux/amd64,linux/arm64,linux/arm/v7' }}
REGISTRY: ${{ vars.REGISTRY_NAME || secrets.REGISTRY_NAME || 'docker.io' }}

jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Set Prepare
id: prep
run: |
DOCKER_IMAGE=vx3r/wg-gen-web
VERSION=edge
if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}
elif [[ $GITHUB_REF == refs/heads/* ]]; then
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
elif [[ $GITHUB_REF == refs/pull/* ]]; then
VERSION=pr-${{ github.event.number }}
fi
TAGS="${DOCKER_IMAGE}:${VERSION}"
if [ "${{ github.event_name }}" = "push" ]; then
TAGS="$TAGS,${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}"
fi
TAGS="$TAGS,${DOCKER_IMAGE}:latest"
echo ::set-output name=version::${VERSION}
echo ::set-output name=tags::${TAGS}
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
echo ::set-output name=sha_short::$(git rev-parse --short HEAD)
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Login to DockerHub
uses: docker/login-action@v3
# https://github.com/actions/checkout
- uses: actions/checkout@v4

# https://github.com/docker/login-action
- uses: docker/login-action@v3
if: ${{ github.event_name != 'pull_request' }}
with:
username: ${{ secrets.DOCKER_LOGIN_USERNAME }}
password: ${{ secrets.DOCKER_LOGIN_PASSWORD }}
-
name: Build and push
uses: docker/build-push-action@v5
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_LOGIN_USERNAME || secrets.REGISTRY_USERNAME || github.actor }}
password: ${{ secrets.DOCKER_LOGIN_PASSWORD || secrets.REGISTRY_PASSWORD || github.token }}

# https://github.com/docker/setup-buildx-action
- uses: docker/setup-buildx-action@v3

# https://github.com/docker/setup-qemu-action
- uses: docker/setup-qemu-action@v3

# https://github.com/docker/metadata-action
- uses: docker/metadata-action@v5
id: meta
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=tag
type=ref,event=pr
type=raw,value=latest,enable={{is_default_branch}}
type=sha

# https://github.com/docker/build-push-action
- uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64,linux/arm/v7
platforms: ${{ env.IMAGE_PLATFORMS }}
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.prep.outputs.tags }}
labels: |
org.opencontainers.image.source=${{ github.event.repository.html_url }}
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
org.opencontainers.image.revision=${{ github.sha }}
build-args: |
COMMIT=${{ steps.prep.outputs.sha_short }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: COMMIT=${{ github.sha }}

View File

@ -4,7 +4,7 @@ FROM golang:alpine AS build-back
WORKDIR /app
ARG COMMIT
COPY . .
RUN go build -o wg-gen-web-linux -ldflags="-X 'github.com/vx3r/wg-gen-web/version.Version=${COMMIT}'" github.com/vx3r/wg-gen-web/cmd/wg-gen-web
RUN go build -o wg-gen-web-linux -ldflags="-X 'github.com/vx3r/wg-gen-web/version.Version=${COMMIT::7}'" github.com/vx3r/wg-gen-web/cmd/wg-gen-web

FROM node:18.13.0-alpine AS build-front
WORKDIR /app