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

Added container workflow #28

Merged
merged 2 commits into from
May 26, 2024
Merged
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
95 changes: 95 additions & 0 deletions .github/workflows/container.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Build docker container
on:
release:
types: [ created, edited ]
workflow_dispatch:
inputs:
tag:
description: 'tag'
required: true
default: 'latest'
type: string
jobs:
var:
name: Set variables
runs-on: ubuntu-latest
outputs:
platform: "linux"
image: "ghcr.io/${{ github.repository }}"
tag: ${{ steps.var.outputs.tag }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set variables
id: var
run: |
if [ "${{ github.event_name }}" != "release" ] && [ "${{ inputs.tag }}" != "latest" ]; then
TAG="${{ inputs.tag }}" && echo "tag=${TAG#v}" >> $GITHUB_OUTPUT
else
TAG="$(git describe --tags)" && echo "tag=${TAG#v}" >> $GITHUB_OUTPUT
fi
build:
name: Build
needs: var
strategy:
matrix:
arch: [ amd64, arm64 ]
runs-on: ${{ matrix.arch == 'amd64' && 'ubuntu-latest' || matrix.arch }}
steps:
- name: Install git
run: |
sudo apt -y update
sudo apt -y install build-essential git
git config --global advice.detachedHead false
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build
run: |
git checkout v${{ needs.var.outputs.tag }}
make docker
- name: Push
run: |
docker image tag \
go-client-${{ needs.var.outputs.platform }}-${{ matrix.arch }}:${{ needs.var.outputs.tag }} \
${{ needs.var.outputs.image }}-${{ matrix.arch }}:${{ needs.var.outputs.tag }}
docker push ${{ needs.var.outputs.image }}-${{ matrix.arch }}:${{ needs.var.outputs.tag }}
manifest:
name: Manifest
needs:
- var
- build
strategy:
matrix:
include:
- tag: ${{ needs.var.outputs.tag }}
- tag: latest
runs-on: ubuntu-latest
steps:
- name: Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create Manifest
run: |
docker manifest create ${{ needs.var.outputs.image }}:${{ matrix.tag }} \
--amend ${{ needs.var.outputs.image }}-amd64:${{ needs.var.outputs.tag }} \
--amend ${{ needs.var.outputs.image }}-arm64:${{ needs.var.outputs.tag }}
docker manifest annotate --arch arm64 \
${{ needs.var.outputs.image }}:${{ matrix.tag }} \
${{ needs.var.outputs.image }}-arm64:${{ needs.var.outputs.tag }}
docker manifest annotate --arch amd64 \
${{ needs.var.outputs.image }}:${{ matrix.tag }} \
${{ needs.var.outputs.image }}-amd64:${{ needs.var.outputs.tag }}
docker manifest push ${{ needs.var.outputs.image }}:${{ matrix.tag }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
etc/_cli
27 changes: 24 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Paths to tools needed in dependencies
GO := $(shell which go)
DOCKER := $(shell which docker)

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

# Set OS and Architecture
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/')
OS ?= $(shell uname | tr A-Z a-z)
VERSION ?= $(shell git describe --tags --always | sed 's/^v//')

# Paths to locations, etc
BUILD_DIR := "build"
CMD_DIR := $(wildcard cmd/*)
BUILD_TAG := go-client-${OS}-${ARCH}:${VERSION}

# Targets
all: clean cmd

cmd: $(CMD_DIR)

test:
docker: docker-dep
@echo build docker image: ${BUILD_TAG} for ${OS}/${ARCH}
@${DOCKER} build \
--tag ${BUILD_TAG} \
--build-arg ARCH=${ARCH} \
--build-arg OS=${OS} \
--build-arg SOURCE=${BUILD_MODULE} \
--build-arg VERSION=${VERSION} \
-f etc/Dockerfile .

test: go-dep
@echo Test
@${GO} mod tidy
@${GO} test ./pkg/...

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

FORCE:

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

docker-dep:
@test -f "${DOCKER}" && test -x "${DOCKER}" || (echo "Missing docker binary" && exit 1)

mkdir:
@echo Mkdir ${BUILD_DIR}
@install -d ${BUILD_DIR}
Expand Down
26 changes: 26 additions & 0 deletions etc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ARG OS
ARG ARCH

# Run makefile to build all the commands
FROM --platform=${OS}/${ARCH} golang:1.21-bullseye AS builder
ARG OS
ARG ARCH
WORKDIR /usr/src/app
COPY . .
RUN OS=${OS} ARCH=${ARCH} make

# Copy server to /usr/local/bin
FROM --platform=${OS}/${ARCH} debian:bullseye-slim
ARG OS
ARG ARCH
ARG SOURCE
RUN apt update && apt install -y ca-certificates
COPY --from=builder /usr/src/app/build/* /usr/local/bin/
COPY etc/entrypoint.sh .

# Labels
LABEL org.opencontainers.image.source=${SOURCE}

# Entrypoint when running the server
ENTRYPOINT [ "/entrypoint.sh" ]
CMD [ "/usr/local/bin/api" ]
13 changes: 13 additions & 0 deletions etc/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

if [ -z "$1" ]; then
echo "No command specified"
exit 1
fi

# Nomad: Create the /alloc/logs folder if it doesn't exist
install -d -m 0755 /alloc/logs || exit 1

# Run the command
set -e
exec "$@"
Loading