Skip to content

Commit

Permalink
chore(gh-actions): refactor test and release github actions (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-github committed Jun 16, 2024
1 parent b4a84bd commit b733cb1
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 87 deletions.
31 changes: 31 additions & 0 deletions .github/goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
project_name: xelon-cloud-controller-manager

before:
hooks:
- go mod tidy

builds:
- skip: true

changelog:
sort: asc
groups:
- title: "Features"
regexp: "^.*feat[(\\w)]*:+.*$"
order: 0
- title: 'Bug fixes'
regexp: "^.*fix[(\\w)]*:+.*$"
order: 1
- title: "Others"
order: 999
filters:
exclude:
- "^docs:"
- "^test:"
- "^Merge pull request"
- "^Release"

release:
github:
owner: Xelon-AG
name: xelon-cloud-controller-manager
45 changes: 0 additions & 45 deletions .github/goreleaser.yml

This file was deleted.

10 changes: 2 additions & 8 deletions .github/workflows/release-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: "go.mod"
cache: true

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}

- name: Release development Docker image
run: make build release-docker-dev
run: make release-docker-dev
16 changes: 8 additions & 8 deletions .github/workflows/release.yml → .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: "go.mod"
- name: Get Git tag
run: echo "VERSION=${{ github.ref_name }}" >> $GITHUB_ENV

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}

- name: Release production Docker images
run: make release-docker

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v3
uses: goreleaser/goreleaser-action@v6
with:
version: latest
args: release -f .github/goreleaser.yml --rm-dist
args: release -f .github/goreleaser.yaml --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24 changes: 21 additions & 3 deletions .github/workflows/unit_tests.yml → .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- "CHANGELOG.md"
- "README.md"
push:
branches: [ master ]
branches: [main]
paths-ignore:
- "CHANGELOG.md"
- "README.md"
Expand All @@ -15,15 +15,33 @@ permissions:
contents: read

jobs:
build:
name: build
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: "go.mod"

- name: Build
run: make build

unit_tests:
name: unit tests
runs-on: ubuntu-latest
needs: build
timeout-minutes: 10
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version-file: "go.mod"

Expand Down
16 changes: 14 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# syntax=docker/dockerfile:1
FROM golang:1.22 AS builder

ARG GIT_COMMIT
ARG GIT_TREE_STATE
ARG SOURCE_DATE_EPOCH
ARG VERSION

ENV CGO_ENABLED=0

# copy manifest files only to cache layer with dependencies
Expand All @@ -12,15 +17,22 @@ COPY cmd/ cmd/
COPY internal/ internal/

# build
RUN go build -o xelon-cloud-controller-manager -ldflags="-s -w" -trimpath cmd/xelon-cloud-controller-manager/main.go
RUN go build -trimpath \
-ldflags="-s -w \
-X github.com/Xelon-AG/xelon-cloud-controller-manager/internal/xelon.gitCommit=${GIT_COMMIT:-none} \
-X github.com/Xelon-AG/xelon-cloud-controller-manager/internal/xelon.gitTreeState=${GIT_TREE_STATE:-none} \
-X github.com/Xelon-AG/xelon-cloud-controller-manager/internal/xelon.sourceDateEpoch=${SOURCE_DATE_EPOCH:-0} \
-X github.com/Xelon-AG/xelon-cloud-controller-manager/internal/xelon.version=${VERSION:-local}" \
-o xelon-cloud-controller-manager cmd/xelon-cloud-controller-manager/main.go



FROM gcr.io/distroless/static:nonroot AS production

ARG VERSION

LABEL org.opencontainers.image.ref.name="xelon-cloud-controller-manager" \
LABEL org.opencontainers.image.licenses="Apache-2.0" \
org.opencontainers.image.ref.name="xelon-cloud-controller-manager" \
org.opencontainers.image.source="https://github.com/Xelon-AG/xelon-cloud-controller-manager" \
org.opencontainers.image.vendor="Xelon AG" \
org.opencontainers.image.version="${VERSION:-local}"
Expand Down
62 changes: 47 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
# Project variables
PROJECT_NAME := xelon-cloud-controller-manager
IMAGE_NAME := xelonag/xelon-cloud-controller-manager
IMAGE_NAME ?= xelonag/xelon-cloud-controller-manager

# Build variables
.DEFAULT_GOAL = test
BUILD_DIR := build
TOOLS_DIR := $(shell pwd)/tools
TOOLS_BIN_DIR := ${TOOLS_DIR}/bin

VERSION ?= $(shell git describe --always)
GIT_COMMIT ?= $(shell git rev-parse HEAD)
ifeq ($(strip $(shell git status --porcelain 2>/dev/null)),)
GIT_TREE_STATE=clean
else
GIT_TREE_STATE=dirty
endif
SOURCE_DATE_EPOCH ?= $(shell git log -1 --pretty=%ct)
LDFLAGS ?= -X github.com/Xelon-AG/xelon-cloud-controller-manager/internal/xelon.gitCommit=${GIT_COMMIT}
LDFLAGS := $(LDFLAGS) -X github.com/Xelon-AG/xelon-cloud-controller-manager/internal/xelon.gitTreeState=${GIT_TREE_STATE}
LDFLAGS := $(LDFLAGS) -X github.com/Xelon-AG/xelon-cloud-controller-manager/internal/xelon.sourceDateEpoch=${SOURCE_DATE_EPOCH}
LDFLAGS := $(LDFLAGS) -X github.com/Xelon-AG/xelon-cloud-controller-manager/internal/xelon.version=${VERSION}


## tools: Install required tooling.
Expand All @@ -31,14 +43,6 @@ lint:
@${TOOLS_BIN_DIR}/golangci-lint run


## build: Build binary for linux/amd64 system.
.PHONE: build
build:
@echo "==> Building binary..."
@echo " running go build for GOOS=linux GOARCH=amd64"
@GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -o $(BUILD_DIR)/$(PROJECT_NAME) cmd/xelon-cloud-controller-manager/main.go


## test: Run all unit tests.
.PHONY: test
test:
Expand All @@ -47,20 +51,48 @@ test:
@go test -count=1 -v -cover -coverprofile=$(BUILD_DIR)/coverage.out -parallel=4 ./...


## build-docker-dev: Build docker dev image with included binary.
.PHONE: build-docker-dev
build-docker-dev: build
@echo "==> Building docker image $(IMAGE_NAME):dev..."
@docker build --platform=linux/amd64 --build-arg VERSION=$(VERSION) --tag $(IMAGE_NAME):dev --file Dockerfile .
## build: Build binary for linux/amd64 system.
.PHONE: build
build:
@echo "==> Building binary..."
@echo " running go build for GOOS=linux GOARCH=amd64"
@GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -o $(BUILD_DIR)/$(PROJECT_NAME) cmd/xelon-cloud-controller-manager/main.go


## build-docker: Build docker image with included binary.
.PHONE: build-docker
build-docker:
@echo "==> Building docker image $(IMAGE_NAME)..."
@docker build \
--build-arg GIT_COMMIT=$(GIT_COMMIT) \
--build-arg GIT_TREE_STATE=$(GIT_TREE_STATE) \
--build-arg SOURCE_DATE_EPOCH=$(SOURCE_DATE_EPOCH) \
--build-arg VERSION=$(VERSION) \
--tag $(IMAGE_NAME) --file Dockerfile .


## release-docker-dev: Release development docker image.
.PHONE: release-docker-dev
release-docker-dev: build-docker-dev
release-docker-dev: build-docker
@echo "==> Tagging docker image $(IMAGE_NAME):dev..."
@docker tag $(IMAGE_NAME) $(IMAGE_NAME):dev
@echo "==> Releasing development docker image $(IMAGE_NAME):dev..."
@docker push $(IMAGE_NAME):dev


## release-docker: Release docker image.
.PHONE: release-docker
release-docker: build-docker
@echo "==> Tagging docker image $(IMAGE_NAME):latest..."
@docker tag $(IMAGE_NAME) $(IMAGE_NAME):latest
@echo "==> Releasing docker image $(IMAGE_NAME):latest..."
@docker push $(IMAGE_NAME):latest
@echo "==> Tagging docker image $(IMAGE_NAME):$(VERSION)..."
@docker tag $(IMAGE_NAME) $(IMAGE_NAME):$(VERSION)
@echo "==> Releasing docker image $(IMAGE_NAME):$(VERSION)..."
@docker push $(IMAGE_NAME):$(VERSION)


help: Makefile
@echo "Usage: make <command>"
@echo ""
Expand Down
4 changes: 0 additions & 4 deletions cmd/xelon-cloud-controller-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ import (
"github.com/Xelon-AG/xelon-cloud-controller-manager/internal/xelon"
)

// var (
// version = "dev"
// )

func main() {
rand.NewSource(time.Now().UnixNano())

Expand Down
5 changes: 3 additions & 2 deletions internal/xelon/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func init() {
}

func newCloud() (cloudprovider.Interface, error) {
klog.InfoS("Cloud controller manager information", "provider", ProviderName, "version_info", GetVersionInfo())

token := os.Getenv(xelonTokenEnv)
if token == "" {
return nil, fmt.Errorf("environment variable %q is required (use k8s secret)", xelonTokenEnv)
Expand All @@ -55,8 +57,7 @@ func newCloud() (cloudprovider.Interface, error) {
return nil, fmt.Errorf("environment variable %q is required", xelonKubernetesClusterIDEnv)
}

userAgent := "xelon-cloud-controller-manager"
opts := []xelon.ClientOption{xelon.WithUserAgent(userAgent)}
opts := []xelon.ClientOption{xelon.WithUserAgent(UserAgent())}
if apiURL := os.Getenv(xelonBaseURLEnv); apiURL != "" {
opts = append(opts, xelon.WithBaseURL(apiURL))
}
Expand Down
36 changes: 36 additions & 0 deletions internal/xelon/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package xelon

import (
"fmt"
"runtime"
)

// These are set during build time via -ldflags
var (
gitCommit = "none"
gitTreeState = "none"
sourceDateEpoch = "0"
version = "dev"
)

type VersionInfo struct {
GitCommit string `json:"git_commit,omitempty"`
GitTreeState string `json:"git_tree_state,omitempty"`
GoVersion string `json:"go_version,omitempty"`
SourceDateEpoch string `json:"source_data_epoch,omitempty"`
Version string `json:"version,omitempty"`
}

func GetVersionInfo() VersionInfo {
return VersionInfo{
GitCommit: gitCommit,
GitTreeState: gitTreeState,
GoVersion: runtime.Version(),
SourceDateEpoch: sourceDateEpoch,
Version: version,
}
}

func UserAgent() string {
return fmt.Sprintf("xelon-cloud-controller-manager/%s", version)
}

0 comments on commit b733cb1

Please sign in to comment.