Skip to content

Commit 8be4644

Browse files
authored
Merge pull request #28 from mutablelogic/v1
Added container workflow
2 parents 6106611 + 3181388 commit 8be4644

File tree

5 files changed

+159
-3
lines changed

5 files changed

+159
-3
lines changed

.github/workflows/container.yaml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: Build docker container
2+
on:
3+
release:
4+
types: [ created, edited ]
5+
workflow_dispatch:
6+
inputs:
7+
tag:
8+
description: 'tag'
9+
required: true
10+
default: 'latest'
11+
type: string
12+
jobs:
13+
var:
14+
name: Set variables
15+
runs-on: ubuntu-latest
16+
outputs:
17+
platform: "linux"
18+
image: "ghcr.io/${{ github.repository }}"
19+
tag: ${{ steps.var.outputs.tag }}
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
with:
24+
fetch-depth: 0
25+
- name: Set variables
26+
id: var
27+
run: |
28+
if [ "${{ github.event_name }}" != "release" ] && [ "${{ inputs.tag }}" != "latest" ]; then
29+
TAG="${{ inputs.tag }}" && echo "tag=${TAG#v}" >> $GITHUB_OUTPUT
30+
else
31+
TAG="$(git describe --tags)" && echo "tag=${TAG#v}" >> $GITHUB_OUTPUT
32+
fi
33+
build:
34+
name: Build
35+
needs: var
36+
strategy:
37+
matrix:
38+
arch: [ amd64, arm64 ]
39+
runs-on: ${{ matrix.arch == 'amd64' && 'ubuntu-latest' || matrix.arch }}
40+
steps:
41+
- name: Install git
42+
run: |
43+
sudo apt -y update
44+
sudo apt -y install build-essential git
45+
git config --global advice.detachedHead false
46+
- name: Checkout
47+
uses: actions/checkout@v4
48+
with:
49+
fetch-depth: 0
50+
- name: Login
51+
uses: docker/login-action@v3
52+
with:
53+
registry: ghcr.io
54+
username: ${{ github.repository_owner }}
55+
password: ${{ secrets.GITHUB_TOKEN }}
56+
- name: Build
57+
run: |
58+
git checkout v${{ needs.var.outputs.tag }}
59+
make docker
60+
- name: Push
61+
run: |
62+
docker image tag \
63+
go-client-${{ needs.var.outputs.platform }}-${{ matrix.arch }}:${{ needs.var.outputs.tag }} \
64+
${{ needs.var.outputs.image }}-${{ matrix.arch }}:${{ needs.var.outputs.tag }}
65+
docker push ${{ needs.var.outputs.image }}-${{ matrix.arch }}:${{ needs.var.outputs.tag }}
66+
manifest:
67+
name: Manifest
68+
needs:
69+
- var
70+
- build
71+
strategy:
72+
matrix:
73+
include:
74+
- tag: ${{ needs.var.outputs.tag }}
75+
- tag: latest
76+
runs-on: ubuntu-latest
77+
steps:
78+
- name: Login
79+
uses: docker/login-action@v3
80+
with:
81+
registry: ghcr.io
82+
username: ${{ github.repository_owner }}
83+
password: ${{ secrets.GITHUB_TOKEN }}
84+
- name: Create Manifest
85+
run: |
86+
docker manifest create ${{ needs.var.outputs.image }}:${{ matrix.tag }} \
87+
--amend ${{ needs.var.outputs.image }}-amd64:${{ needs.var.outputs.tag }} \
88+
--amend ${{ needs.var.outputs.image }}-arm64:${{ needs.var.outputs.tag }}
89+
docker manifest annotate --arch arm64 \
90+
${{ needs.var.outputs.image }}:${{ matrix.tag }} \
91+
${{ needs.var.outputs.image }}-arm64:${{ needs.var.outputs.tag }}
92+
docker manifest annotate --arch amd64 \
93+
${{ needs.var.outputs.image }}:${{ matrix.tag }} \
94+
${{ needs.var.outputs.image }}-amd64:${{ needs.var.outputs.tag }}
95+
docker manifest push ${{ needs.var.outputs.image }}:${{ matrix.tag }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
build
2+
etc/_cli

Makefile

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Paths to tools needed in dependencies
22
GO := $(shell which go)
3+
DOCKER := $(shell which docker)
34

45
# Build flags
56
BUILD_MODULE := $(shell cat go.mod | head -1 | cut -d ' ' -f 2)
@@ -10,28 +11,48 @@ BUILD_LD_FLAGS += -X $(BUILD_MODULE)/pkg/version.GitHash=$(shell git rev-parse H
1011
BUILD_LD_FLAGS += -X $(BUILD_MODULE)/pkg/version.GoBuildTime=$(shell date -u '+%Y-%m-%dT%H:%M:%SZ')
1112
BUILD_FLAGS = -ldflags "-s -w $(BUILD_LD_FLAGS)"
1213

14+
# Set OS and Architecture
15+
ARCH ?= $(shell arch | tr A-Z a-z | sed 's/x86_64/amd64/' | sed 's/i386/amd64/' | sed 's/armv7l/arm/' | sed 's/aarch64/arm64/')
16+
OS ?= $(shell uname | tr A-Z a-z)
17+
VERSION ?= $(shell git describe --tags --always | sed 's/^v//')
18+
1319
# Paths to locations, etc
1420
BUILD_DIR := "build"
1521
CMD_DIR := $(wildcard cmd/*)
22+
BUILD_TAG := go-client-${OS}-${ARCH}:${VERSION}
1623

1724
# Targets
1825
all: clean cmd
1926

2027
cmd: $(CMD_DIR)
2128

22-
test:
29+
docker: docker-dep
30+
@echo build docker image: ${BUILD_TAG} for ${OS}/${ARCH}
31+
@${DOCKER} build \
32+
--tag ${BUILD_TAG} \
33+
--build-arg ARCH=${ARCH} \
34+
--build-arg OS=${OS} \
35+
--build-arg SOURCE=${BUILD_MODULE} \
36+
--build-arg VERSION=${VERSION} \
37+
-f etc/Dockerfile .
38+
39+
test: go-dep
40+
@echo Test
2341
@${GO} mod tidy
2442
@${GO} test ./pkg/...
2543

26-
$(CMD_DIR): dependencies mkdir
44+
$(CMD_DIR): go-dep mkdir
2745
@echo Build cmd $(notdir $@)
2846
@${GO} build ${BUILD_FLAGS} -o ${BUILD_DIR}/$(notdir $@) ./$@
2947

3048
FORCE:
3149

32-
dependencies:
50+
go-dep:
3351
@test -f "${GO}" && test -x "${GO}" || (echo "Missing go binary" && exit 1)
3452

53+
docker-dep:
54+
@test -f "${DOCKER}" && test -x "${DOCKER}" || (echo "Missing docker binary" && exit 1)
55+
3556
mkdir:
3657
@echo Mkdir ${BUILD_DIR}
3758
@install -d ${BUILD_DIR}

etc/Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
ARG OS
2+
ARG ARCH
3+
4+
# Run makefile to build all the commands
5+
FROM --platform=${OS}/${ARCH} golang:1.21-bullseye AS builder
6+
ARG OS
7+
ARG ARCH
8+
WORKDIR /usr/src/app
9+
COPY . .
10+
RUN OS=${OS} ARCH=${ARCH} make
11+
12+
# Copy server to /usr/local/bin
13+
FROM --platform=${OS}/${ARCH} debian:bullseye-slim
14+
ARG OS
15+
ARG ARCH
16+
ARG SOURCE
17+
RUN apt update && apt install -y ca-certificates
18+
COPY --from=builder /usr/src/app/build/* /usr/local/bin/
19+
COPY etc/entrypoint.sh .
20+
21+
# Labels
22+
LABEL org.opencontainers.image.source=${SOURCE}
23+
24+
# Entrypoint when running the server
25+
ENTRYPOINT [ "/entrypoint.sh" ]
26+
CMD [ "/usr/local/bin/api" ]

etc/entrypoint.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
if [ -z "$1" ]; then
4+
echo "No command specified"
5+
exit 1
6+
fi
7+
8+
# Nomad: Create the /alloc/logs folder if it doesn't exist
9+
install -d -m 0755 /alloc/logs || exit 1
10+
11+
# Run the command
12+
set -e
13+
exec "$@"

0 commit comments

Comments
 (0)