Skip to content

Commit

Permalink
feat(prj): support docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Nov 8, 2024
1 parent 0a084ef commit e4923c8
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: docker

on:
workflow_dispatch:
inputs:
ref:
description: 'Ref to checkout'
required: true
default: 'master'
push:
tags:
- 'v*'

jobs:
docker:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event_name == 'workflow_dispatch' && inputs.ref || github.ref }}
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
context: git
images: |
${{ github.repository }}
ghcr.io/${{ github.repository }}
tags: |
type=match,pattern=\d+.\d+.\d+
type=ref,event=branch
type=ref,event=pr
- 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
with:
username: ${{ github.repository_owner }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.RELEASE_TOKEN }}
- name: Use latest Dockerfile if workflow_dispatch
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
curl -s https://raw.githubusercontent.com/iyear/tdl/master/Dockerfile > Dockerfile
- name: Extract Dockerfile args
id: args
run: |
echo "commit=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
echo "commit_date=$(git show -s --format=%cI)" >> "$GITHUB_OUTPUT"
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
VERSION=${{ steps.meta.outputs.version }}
COMMIT=${{ steps.args.outputs.commit }}
COMMIT_DATE=${{ steps.args.outputs.commit_date }}
platforms: linux/amd64,linux/386,linux/arm64,linux/arm/v7,linux/arm/v6,linux/riscv64
push: true
provenance: false
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/
FROM --platform=$BUILDPLATFORM golang:1.21-alpine AS builder

ARG VERSION="dev"
ARG COMMIT="unknown"
ARG COMMIT_DATE="unknown"

WORKDIR /

COPY . .

ARG TARGETOS
ARG TARGETARCH

RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
GOOS=$TARGETOS GOARCH=$TARGETARCH \
go build -trimpath \
-ldflags "-s -w \
-X github.com/iyear/tdl/pkg/consts.Version=${VERSION} \
-X github.com/iyear/tdl/pkg/consts.Commit=${COMMIT} \
-X github.com/iyear/tdl/pkg/consts.CommitDate=${COMMIT_DATE}" \
-o tdl

FROM alpine:latest

RUN apk add --no-cache ca-certificates

COPY --from=builder /tdl /usr/bin/tdl

ENTRYPOINT ["tdl"]

0 comments on commit e4923c8

Please sign in to comment.