Skip to content

Commit

Permalink
ci: use makefile to drive automation tasks (#24)
Browse files Browse the repository at this point in the history
* ci: use makefile to drive automation tasks

Signed-off-by: Nico Braun <[email protected]>

* ci: build artifacts in check workflow

Signed-off-by: Nico Braun <[email protected]>

* ci: fix artifact target

Signed-off-by: Nico Braun <[email protected]>

* ci: use setup buildx action

Signed-off-by: Nico Braun <[email protected]>

* ci: lowercase workflow names

Signed-off-by: Nico Braun <[email protected]>

* ci: use long form of buildx push flag

Signed-off-by: Nico Braun <[email protected]>

* chore: format makefile

Signed-off-by: Nico Braun <[email protected]>

---------

Signed-off-by: Nico Braun <[email protected]>
  • Loading branch information
bluebrown authored Jul 25, 2023
1 parent 533ff25 commit 01f72fc
Show file tree
Hide file tree
Showing 23 changed files with 363 additions and 830 deletions.
33 changes: 33 additions & 0 deletions .github/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -o nounset -o errexit -o errtrace -o pipefail

script_abs="$(readlink -f "$0")"
script_dir="$(dirname "$script_abs")"

if [ "${PRE_RELEASE:-0}" = 1 ]; then
pre=true
fi

git tag -a -m "release" "$VERSION"
git push "$(git remote show)" "$VERSION"

rid="$(curl -fsSL \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/bluebrown/vscode-extension-yamlfmt/releases \
-d '{ "tag_name":"'"$VERSION"'", "generate_release_notes": true, "prerelease": '"${pre:-false}"' }' |
jq -r '.id')"

find "${DIST_DIR:-$script_dir/../.dist/}" -type f |
while read -r asset_path; do
curl -fsSL \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Content-Type: application/octet-stream" \
"https://uploads.github.com/repos/bluebrown/kobold/releases/$rid/assets?name=${asset_path##*/}" \
--data-binary "@$asset_path" >/dev/null
done
36 changes: 36 additions & 0 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: check

on:
workflow_dispatch: {}
push:
branches:
- main
pull_request:
types:
- opened
- synchronize
- reopened
- ready_for_review


jobs:
validation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with: {go-version: "1.20"}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Install dependencies
run: go mod download

- name: Run all checks
run: make check

- name: Build artifacts
run: make artifacts
39 changes: 39 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: release

on:
workflow_dispatch:
inputs:
preRelease:
description: Create a pre release, if set
type: boolean
default: false

jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set git config
run: |
git config --global user.email "[email protected]"
git config --global user.name "GitHub Actions"
- name: Login to the container registry
run: docker login --username bluebrown --password ${{secrets.DOCKERHUB_TOKEN}}

- name: Set up Go
uses: actions/setup-go@v4
with: {go-version: "1.20"}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Create a github release
run: make github-release
2 changes: 2 additions & 0 deletions .github/workflows/triage.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name: triage

on:
issues:
types:
- opened

jobs:
label_issue:
runs-on: ubuntu-latest
Expand Down
23 changes: 0 additions & 23 deletions .github/workflows/validate.yaml

This file was deleted.

7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
/.dist/

/bin/*
!/bin/task
!/bin/makehelp

.env
!manifests/base/etc/.env
!manifests/example/etc/.env
!/manifests/base/etc/.env
!/manifests/example/etc/.env
!/e2e/kobold/etc/.env

dockerconfig.json
6 changes: 6 additions & 0 deletions .yamlfmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
continue_on_error: true
formatter:
type: basic
indent: 2
scan_folded_as_literal: true
retain_line_breaks: true
134 changes: 134 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

PATH := $(CURDIR)/bin:$(PATH)
export PATH

GOBIN = $(CURDIR)/bin/
export GOBIN

GOOS = $(shell go env GOOS)
GOARCH = $(shell go env GOARCH)

##@ Options

BUILD_TAGS ?= gitgo gitexec
VERSION ?= dev
PRE_RELEASE ?= 0
CONTAINER_REGISTRY ?= docker.io
BUILDX_FLAGS ?= --load
DIST_DIR ?= .dist

export VERSION
export PRE_RELEASE
export CONTAINER_REGISTRY

remote = $(shell git remote show)
head = $(shell git remote show $(remote) | sed -n '/HEAD branch/s/.*: //p')
current = $(shell git branch --show-current)


##@ Commands

help: ## Show this help text
bin/makehelp Makefile


###@ Develop

.PHONY: e2e-up
e2e-up: bin/kind bin/kubectl bin/kustomize ## Spin up the local e2e setup
bash e2e/up.sh

.PHONY: e2e-down
e2e-down: bin/kind bin/kubectl bin/kustomize ## Tear down the local e22 setup
bash e2e/down.sh


###@ Validate

.PHONY: check
check: license-check vet test ## Run all checks

.PHONY: vet
vet: ## Lint the code
go vet ./...

.PHONY: test
test: ## Run test suite
go test ./...

.PHONY: license-check
license-check: bin/go-licenses ## Check for dangerous licenses of dependencies
go-licenses check --include_tests \
--allowed_licenses=0BSD,ISC,BSD-2-Clause,BSD-3-Clause,MIT,Apache-2.0,MPL-2.0 \
./cmd/server/

.PHONY: git-ishead
git-ishead: # Fail, if current branch is not HEAD
test $(current) = $(head)

.PHONY: git-isclean
git-isclean: # Fail, if worktree is dirty
git diff-index --quiet HEAD --


###@ Build

.PHONY: build
build: ## Build the binaries with for each tag of BUILD_TAGS
$(foreach tag,$(BUILD_TAGS),go build -tags $(tag) \
-ldflags='-w -s -X "main.version=$(VERSION)"' -o bin/kobold$(if $(filter-out $(tag), gitgo),-$(tag)) ./cmd/server/;)

.PHONY: image-build
image-build: ## Build the images with VERSION as tag. Passes BUILDX_FLAGS to buildx
docker buildx bake --file build/docker-bake.hcl $(BUILDX_FLAGS)

.PHONY: artefacts
artifacts: bin/kustomize build ## Create all release artifacts and put the in .dist/
mkdir -p .dist && rm -rf .dist/*
$(foreach binary,$(wildcard bin/kobold*),tar -czf .dist/$(notdir $(binary)).$(GOOS)-$(GOARCH).tgz $(binary);)
bin/kustomize build manifests/dist/ -o .dist/kobold.manifests.yaml
cp assets/schema.json .dist/kobold.schema.json
$(MAKE) image-build BUILDX_FLAGS='--set *.attest=type=sbom \
--set gitgo.output=type=tar,dest=.dist/kobold-gitgo.oci.tar \
--set gitexec.output=type=tar,dest=.dist/kobold-gitexec.oci.tar'


###@ Publish

.PHONY: version-next
version-next: # internal command to set VERSION to the next semver
$(eval VERSION = $(shell docker run --rm -u "$$(id -u):$$(id -g)" \
-v $(CURDIR):/tmp -w /tmp convco/convco version --bump \
$(if $(filter $(PRE_RELEASE), 1),--prerelease rc)))

.PHONY: image-publish
image-publish: ## Build and push the images to CONTAINER_REGISTRY
$(MAKE) image-build BUILDX_FLAGS='--set *.attest=type=sbom --set=*.output=type=registry'

.PHONY: github-pages
github-pages: bin/mdbook ## Build and publish the docs to github pages
bash docs/publish.sh

.PHONY: github-release
github-release: git-ishead git-isclean version-next artifacts image-publish ## Create a new release on GitHub and publish the images. Set PRE_RELEASE=1 for pre releases
bash .github/release.sh


## Dependencies

bin/kubectl:
curl -fsSL "https://dl.k8s.io/release/$(shell curl -fsSL https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" -o bin/kubectl

bin/kustomize:
curl -fsSL "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash -s bin

bin/kind:
curl -fsSL https://kind.sigs.k8s.io/dl/latest/kind-linux-amd64 | install /dev/stdin bin/kind

bin/mdbook:
curl -fsSL https://github.com/rust-lang/mdBook/releases/download/v0.4.14/mdbook-v0.4.14-x86_64-unknown-linux-gnu.tar.gz | tar -C bin -xzf -

bin/go-licenses:
go install github.com/google/go-licenses@latest
Loading

0 comments on commit 01f72fc

Please sign in to comment.