Skip to content

Commit

Permalink
Configure Github actions
Browse files Browse the repository at this point in the history
* Validate PRs by running build & test
* Release Go binaries and images
* Create custom images for test purposes
  • Loading branch information
kloyan committed May 21, 2023
1 parent 8e61e07 commit 8d86c42
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 12 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/devel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Development

on:
workflow_dispatch:
inputs: {}

permissions:
contents: read
packages: write
id-token: write

env:
REGISTRY: ghcr.io

jobs:
devel:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Fetch tags
run: git fetch --force --tags

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version-file: go.mod

- name: Setup Docker buildx
uses: docker/setup-buildx-action@v2

- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build devel image
uses: goreleaser/goreleaser-action@v4
with:
distribution: goreleaser
version: latest
args: release --clean --snapshot -f .goreleaser-devel.yaml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Push devel image
env:
IMAGE_NAME: ${{ github.event.repository.name }}-devel
run: |
docker image ls --format "{{.Repository}}:{{.Tag}}" |\
grep -e "$IMAGE_NAME:.*$(git rev-parse --short HEAD).*" |\
xargs -I{} docker push {}
50 changes: 50 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Release

on:
push:
tags:
- 'v*'

permissions:
contents: write
packages: write
id-token: write

env:
REGISTRY: ghcr.io

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Fetch tags
run: git fetch --force --tags

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version-file: go.mod

- name: Setup Docker buildx
uses: docker/setup-buildx-action@v2

- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Release binaries & image
uses: goreleaser/goreleaser-action@v4
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
25 changes: 25 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Go Tests

on:
pull_request:
branches:
- "main"
- "rel-*"

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version-file: go.mod

- name: Build
run: make build

- name: Tests
run: make test
30 changes: 30 additions & 0 deletions .goreleaser-devel.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
project_name: remote-work-processor
snapshot:
name_template: "{{ incminor .Version }}-{{ .ShortCommit }}"
builds:
- goos:
- linux
goarch:
- amd64
main: ./cmd/remote-work-processor/main.go
binary: remote-work-processor
ldflags: -s -w -X main.Version={{.Version}} -X main.BuildDate={{.Date}}
env:
- CGO_ENABLED=0
dockers:
- dockerfile: Dockerfile
image_templates:
- "ghcr.io/sap/{{ .ProjectName }}-devel:{{ .Version }}"
use: buildx
goos: linux
goarch: amd64
build_flag_templates:
- --platform=linux/amd64
- --label=org.opencontainers.image.created={{ .Date }}
- --label=org.opencontainers.image.title={{ .ProjectName }}-devel
- --label=org.opencontainers.image.version={{ .ShortCommit }}
- --label=org.opencontainers.image.revision=test-{{ .FullCommit }}
- --label=org.opencontainers.image.description={{ .ProjectName }}-devel {{ .ShortCommit }}
- --label=org.opencontainers.image.source=https://github.com/sap/{{ .ProjectName }}
release:
disable: true
43 changes: 43 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
project_name: remote-work-processor
builds:
- goos:
- linux
- darwin
- windows
goarch:
- amd64
- arm64
main: ./cmd/remote-work-processor/main.go
binary: remote-work-processor
ldflags: -s -w -X main.Version={{.Version}} -X main.BuildDate={{.Date}}
env:
- CGO_ENABLED=0
dockers:
- dockerfile: Dockerfile
image_templates:
- "ghcr.io/sap/{{ .ProjectName }}:{{ .Version }}"
- "ghcr.io/sap/{{ .ProjectName }}:{{ .Major }}.{{ .Minor }}"
use: buildx
goos: linux
goarch: amd64
build_flag_templates:
- --platform=linux/amd64
- --label=org.opencontainers.image.created={{ .Date }}
- --label=org.opencontainers.image.title={{ .ProjectName }}
- --label=org.opencontainers.image.version={{ .Version }}
- --label=org.opencontainers.image.revision={{ .FullCommit }}
- --label=org.opencontainers.image.description={{ .ProjectName }} {{ .Version }}
- --label=org.opencontainers.image.source=https://github.com/sap/{{ .ProjectName }}
archives:
- format: binary
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
checksum:
name_template: 'checksums.txt'
changelog:
sort: asc
use: git
release:
footer: |
## Docker Images
- `docker pull ghcr.io/sap/{{.ProjectName}}:{{ .Version }}`
14 changes: 3 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
FROM golang:1.20-bullseye as builder

WORKDIR /autopi

# Copy the Go Modules manifests
COPY . .

# Build
RUN CGO_ENABLED=0 make build

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static-debian11:nonroot

ARG BIN_FILE=./remote-work-processor

WORKDIR /
COPY --from=builder /autopi/remote-work-processor .
COPY ${BIN_FILE} .

USER 65532:65532

Expand Down
19 changes: 18 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
#!/usr/bin/make -f

OUT_EXEC ?= dist/remote-work-processor
OUT_DIR ?= dist
OUT_NAME ?= remote-work-processor
OUT_EXEC = $(OUT_DIR)/$(OUT_NAME)
MAIN = ./cmd/remote-work-processor/main.go
PROTO_DIR = build/proto
DOCKER ?= docker
REGISTRY ?= ghcr.io/sap
IMAGE_NAME ?= remote-work-processor
VERSION ?= dev
IMAGE_TAG = $(REGISTRY)/$(IMAGE_NAME):$(VERSION)

build: fmt vet
$(CURDIR)/scripts/build.sh "$(MAIN)" $(OUT_EXEC)

image: build
$(DOCKER) build \
--no-cache \
--tag $(IMAGE_TAG) \
--build-arg BIN_FILE=$(OUT_EXEC) \
.

test:
go test ./...

fmt:
$(CURDIR)/scripts/assertgofmt.sh

Expand Down

0 comments on commit 8d86c42

Please sign in to comment.