Skip to content

Commit

Permalink
Improve CI Pipeline on PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidswiss committed Aug 14, 2024
1 parent cda368c commit 03d2bed
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 42 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: PR Automation

on:
pull_request: {}

jobs:
publish-branch-images:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Determine Go version from go.mod
run: echo "GO_VERSION=$(grep "go 1." go.mod | cut -d " " -f 2)" >> $GITHUB_ENV

- uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}

- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Login to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build branch and push AppCat
run: make docker-push-branchtag

- name: Build branch and push Functions
run: make function-push-package-branchtag
32 changes: 4 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

# Image URL to use all building/pushing image targets
IMG_TAG ?= latest
GHCR_IMG ?= ghcr.io/vshn/appcat:$(IMG_TAG)
DOCKER_CMD ?= docker
PROJECT_ROOT_DIR = .
PROJECT_NAME ?= appcat
PROJECT_OWNER ?= vshn

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand All @@ -18,14 +16,6 @@ else
sed ?= sed
endif

# For alpine image it is required the following env before building the application
DOCKER_IMAGE_GOOS = linux
DOCKER_IMAGE_GOARCH = amd64

PROJECT_ROOT_DIR = .
PROJECT_NAME ?= appcat
PROJECT_OWNER ?= vshn

PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
BIN_FILENAME ?= $(PROJECT_DIR)/appcat

Expand Down Expand Up @@ -58,6 +48,7 @@ $(protoc_bin): | $(go_bin)

-include docs/antora-preview.mk docs/antora-build.mk
-include package/package.mk
-include ci.mk

.PHONY: help
help: ## Display this help.
Expand Down Expand Up @@ -156,26 +147,11 @@ build:
test: ## Run tests
go test ./... -count=1

.PHONY: docker-build
docker-build:
env CGO_ENABLED=0 GOOS=$(DOCKER_IMAGE_GOOS) GOARCH=$(DOCKER_IMAGE_GOARCH) \
go build -o ${BIN_FILENAME}
docker build --platform $(DOCKER_IMAGE_GOOS)/$(DOCKER_IMAGE_GOARCH) -t ${GHCR_IMG} .

.PHONY: docker-build-branchtag
docker-build-branchtag: docker-build ## Build docker image with current branch name
tag=$$(git rev-parse --abbrev-ref HEAD) && \
docker tag ${GHCR_IMG} ghcr.io/vshn/appcat:"$${tag////_}"

.PHONY: kind-load-branch-tag
kind-load-branch-tag: ## load docker image with current branch tag into kind
tag=$$(git rev-parse --abbrev-ref HEAD) && \
kind load docker-image --name kindev ghcr.io/vshn/appcat:"$${tag////_}"

.PHONY: docker-push
docker-push: docker-build ## Push docker image with the manager.
docker push ${GHCR_IMG}

# Generate webhook certificates.
# This is only relevant when debugging.
# Component-appcat installs a proper certificate for this.
Expand Down
50 changes: 50 additions & 0 deletions ci.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Image URL to use all building/pushing image targets
IMG_TAG ?= latest
GHCR_IMG ?= ghcr.io/vshn/appcat:$(IMG_TAG)
DOCKER_CMD ?= docker

# For alpine image it is required the following env before building the application
DOCKER_IMAGE_GOOS = linux
DOCKER_IMAGE_GOARCH = amd64

.PHONY: docker-build
docker-build:
env CGO_ENABLED=0 GOOS=$(DOCKER_IMAGE_GOOS) GOARCH=$(DOCKER_IMAGE_GOARCH) \
go build -o ${BIN_FILENAME}
docker build --platform $(DOCKER_IMAGE_GOOS)/$(DOCKER_IMAGE_GOARCH) -t ${GHCR_IMG} .

.PHONY: docker-build-branchtag
IMG_TAG = $(shell git rev-parse --abbrev-ref HEAD | sed 's/\//_/g')
docker-build-branchtag: docker-build ## Build docker image with current branch name

.PHONY: docker-push
docker-push: docker-build ## Push docker image with the manager.
docker push ${GHCR_IMG}

.PHONY: docker-push-branchtag
IMG_TAG = $(shell git rev-parse --abbrev-ref HEAD | sed 's/\//_/g')
docker-push-branchtag: docker-build-branchtag docker-push ## Push docker image with current branch name

.PHONY: function-build
function-build: docker-build
yq e '.spec.image="${GHCR_IMG}"' package/crossplane.yaml.template > package/crossplane.yaml
rm -f package/*.xpkg
go run github.com/crossplane/crossplane/cmd/[email protected] xpkg build -f package --verbose --embed-runtime-image=${GHCR_IMG} -o package/package-function-appcat.xpkg
git checkout package/crossplane.yaml

.PHONY: function-push-package
function-push-package: function-build
go run github.com/crossplane/crossplane/cmd/[email protected] xpkg push -f package/package-function-appcat.xpkg ghcr.io/vshn/appcat:${IMG_TAG}-func --verbose

.PHONY: function-build-branchtag
IMG_TAG = $(shell git rev-parse --abbrev-ref HEAD | sed 's/\//_/g')
function-build-branchtag: docker-build-branchtag
yq e '.spec.image="${GHCR_IMG}"' package/crossplane.yaml.template > package/crossplane.yaml
rm -f package/*.xpkg
go run github.com/crossplane/crossplane/cmd/[email protected] xpkg build -f package --verbose --embed-runtime-image=${GHCR_IMG} -o package/package-function-appcat.xpkg
git checkout package/crossplane.yaml

.PHONY: function-push-package-branchtag
IMG_TAG = $(shell git rev-parse --abbrev-ref HEAD | sed 's/\//_/g')
function-push-package-branchtag: function-build-branchtag
go run github.com/crossplane/crossplane/cmd/[email protected] xpkg push -f package/package-function-appcat.xpkg ${GHCR_IMG}-func --verbose
14 changes: 0 additions & 14 deletions package/package.mk

This file was deleted.

0 comments on commit 03d2bed

Please sign in to comment.