Skip to content

Commit

Permalink
New CI pipeline based on goreleaser
Browse files Browse the repository at this point in the history
  • Loading branch information
dex4er committed Feb 14, 2024
1 parent 343852d commit ef813e0
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 188 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ jobs:
release:
name: Release

permissions:
id-token: write
contents: write

runs-on: ubuntu-latest

steps:
Expand All @@ -25,9 +29,9 @@ jobs:
run: git fetch --force --tags

- name: Setup Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: 1.21.1 # datasource=github-releases depName=golang/go
go-version: 1.21.3 # datasource=github-releases depName=golang/go

- name: Goreleaser
uses: goreleaser/goreleaser-action@v5
Expand Down
15 changes: 12 additions & 3 deletions .github/workflows/snapshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
with:
fetch-depth: 0

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

- name: Fake terraform
run: |
echo '#!/bin/bash' | sudo tee /usr/local/bin/terraform
Expand All @@ -41,10 +44,16 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: 1.21.1 # datasource=github-releases depName=golang/go
go-version: 1.21.3 # datasource=github-releases depName=golang/go

- name: Build binary
run: make download build
- name: Goreleaser
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: 1.21.2 # datasource=github-releases depName=goreleaser/goreleaser
args: release --clean --snapshot --single-target --output tf
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Test binary
run: |
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*.log
*.tfstate
demo-*.txt
dist/
tmp/
/tf
.terraform
.vscode
77 changes: 8 additions & 69 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ ifneq (,$(wildcard .env))
endif

AWK = awk
DOCKER = docker
ECHO = echo
GO = go
GORELEASER = goreleaser
HEAD = head
INSTALL = install
MAKE = make
PRINTF = printf
RM = rm
SORT = sort

ifeq ($(OS),Darwin)
SORT = gsort
else
SORT = sort
endif

ifeq ($(GOOS),windows)
EXE = .exe
else ifeq ($(shell $(GO) env GOOS),windows)
Expand All @@ -40,13 +44,6 @@ BINDIR = /usr/local/bin
endif
endif

VERSION = $(shell ( git describe --tags --exact-match 2>/dev/null || ( git describe --tags 2>/dev/null || echo "0.0.0-0-g$$(git rev-parse --short=8 HEAD)" ) | sed 's/-[0-9][0-9]*-g/-SNAPSHOT-/') | sed 's/^v//' )
REVISION = $(shell git rev-parse HEAD)
BUILDDATE = $(shell TZ=GMT date '+%Y-%m-%dT%R:%SZ')

CGO_ENABLED = 0
export CGO_ENABLED

.PHONY: help
help:
@echo Targets:
Expand All @@ -55,12 +52,7 @@ help:
.PHONY: build
build: ## Build app binary for single target
$(call print-target)
$(GO) build -trimpath -ldflags="-s -w -X main.version=$(VERSION)"

.PHONY: goreleaser
goreleaser: ## Build app binary for all targets
$(call print-target)
$(GORELEASER) release --auto-snapshot --clean --skip-publish
$(GORELEASER) build --clean --snapshot --single-target --output $(BIN)

$(BIN):
@$(MAKE) build
Expand Down Expand Up @@ -97,59 +89,6 @@ clean: ## Clean working directory
$(RM) -f $(BIN)
$(RM) -rf dist

.PHONY: version
version: ## Show version
@$(ECHO) "$(VERSION)"

.PHONY: revision
revision: ## Show revision
@$(ECHO) "$(REVISION)"

.PHONY: builddate
builddate: ## Show build date
@$(ECHO) "$(BUILDDATE)"

DOCKERFILE = Dockerfile
IMAGE_NAME = $(BIN)
LOCAL_REPO = localhost:5000/$(IMAGE_NAME)
DOCKER_REPO = localhost:5000/$(IMAGE_NAME)

ifeq ($(PROCESSOR_ARCHITECTURE),ARM64)
PLATFORM = linux/arm64
else ifeq ($(shell uname -m),arm64)
PLATFORM = linux/arm64
else ifeq ($(shell uname -m),aarch64)
PLATFORM = linux/arm64
else ifeq ($(findstring ARM64, $(shell uname -s)),ARM64)
PLATFORM = linux/arm64
else
PLATFORM = linux/amd64
endif

.PHONY: image
image: ## Build a local image without publishing artifacts.
$(MAKE) build GOOS=linux
$(call print-target)
$(DOCKER) buildx build --file=$(DOCKERFILE) \
--platform=$(PLATFORM) \
--build-arg VERSION=$(VERSION) \
--build-arg REVISION=$(REVISION) \
--build-arg BUILDDATE=$(BUILDDATE) \
--tag $(LOCAL_REPO) \
--load \
.

.PHONY: push
push: ## Publish to container registry.
$(call print-target)
$(DOCKER) tag $(LOCAL_REPO) $(DOCKER_REPO):v$(VERSION)-$(subst /,-,$(PLATFORM))
$(DOCKER) push $(DOCKER_REPO):v$(VERSION)-$(subst /,-,$(PLATFORM))

.PHONY: test-image
test-image: ## Test local image
$(call print-target)
$(DOCKER) run --platform=$(PLATFORM) --rm -t $(LOCAL_REPO) -v

define print-target
@$(PRINTF) "Executing target: \033[36m$@\033[0m\n"
endef
116 changes: 2 additions & 114 deletions Makefile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ foreach ($arg in $args) {
}
}

if (-not $env:DOCKER) { $env:DOCKER = "docker" }
if (-not $env:GO) { $env:GO = "go" }
if (-not $env:GORELEASER) { $env:GORELEASER = "goreleaser" }

Expand Down Expand Up @@ -65,47 +64,6 @@ else {
}
}

function Get-Version {
try {
$exactMatch = git describe --tags --exact-match 2>$null
if (-not [string]::IsNullOrEmpty($exactMatch)) {
$version = $exactMatch
}
else {
$tags = git describe --tags 2>$null;
if ([string]::IsNullOrEmpty($tags)) {
$commitHash = (git rev-parse --short=8 HEAD).Trim();
$version = "0.0.0-0-g$commitHash"
}
else {
$version = $tags -replace '-[0-9][0-9]*-g', '-SNAPSHOT-'
}
}
$version = $version -replace '^v', ''
return $version
}
catch {
return "0.0.0"
}
}

function Get-Revision {
$revision = git rev-parse HEAD
return $revision
}

function Get-Builddate {
$datetime = Get-Date
$utc = $datetime.ToUniversalTime()
return $utc.tostring("yyyy-MM-ddTHH:mm:ssZ")
}

if (-not $env:VERSION) { $env:VERSION = (& get-version) }
if (-not $env:REVISION) { $env:REVISION = (& get-revision) }
if (-not $env:BUILDDATE) { $env:BUILDDATE = (& get-builddate) }

if (-not $env:CGO_ENABLED) { $env:CGO_ENABLED = "0" }

function Invoke-CommandWithEcho {
param (
[string]$Command,
Expand Down Expand Up @@ -138,13 +96,9 @@ function Write-Target {
## TARGET build Build app binary for single target
function Invoke-Target-Build {
Write-Target "build"
Invoke-CommandWithEcho $env:GO -Arguments "build", "-trimpath", "-ldflags=`"-s -w -X main.version=$env:VERSION`""
}
# $(GORELEASER) build --clean --snapshot --single-target --output $(BIN)

## TARGET goreleaser Build app binary for all targets
function Invoke-Target-Goreleaser {
Write-Target "goreleaser"
Invoke-CommandWithEcho $env:GORELEASER -Arguments "release", "--auto-snapshot", "--clean", "--skip-publish"
Invoke-CommandWithEcho $env:GORELEASER -Arguments "build", "--clean", "--snapshot", "--single-target", "--output", $env:BIN
}

## TARGET install Build and install app binary
Expand Down Expand Up @@ -188,72 +142,6 @@ function Invoke-Target-Clean {
Invoke-ExpressionWithEcho -Command "Remove-Item dist -Recurse -Force -ErrorAction SilentlyContinue"
}

## TARGET version Show version
function Invoke-Target-Version {
Write-Host $env:VERSION
}

## TARGET revision Show revision
function Invoke-Target-Revision {
Write-Host $env:REVISION
}

## TARGET builddate Show build date
function Invoke-Target-Builddate {
Write-Host $env:BUILDDATE
}

if (-not $env:DOCKERFILE) { $env:DOCKERFILE = "Dockerfile" }
if (-not $env:IMAGE_NAME) { $env:IMAGE_NAME = $env:BIN }
if (-not $env:LOCAL_REPO) { $env:LOCAL_REPO = "localhost:5000/$env:IMAGE_NAME" }
if (-not $env:DOCKER_REPO) { $env:DOCKER_REPO = "localhost:5000/$env:IMAGE_NAME" }

if (-not $env:PLATFORM) {
if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") {
$env:PLATFORM = "linux/arm64"
}
elseif ((uname -m) -eq "arm64") {
$env:PLATFORM = "linux/arm64"
}
elseif ((uname -m) -eq "aarch64") {
$env:PLATFORM = "linux/arm64"
}
elseif ((uname -s) -match "ARM64") {
$env:PLATFORM = "linux/arm64"
}
else {
$env:PLATFORM = "linux/amd64"
}
}

## TARGET image Build a local image without publishing artifacts.
function Invoke-Target-Image {
$env:GOOS = "linux"
Invoke-Target-Build
Write-Target "image"
Invoke-CommandWithEcho $env:DOCKER -Arguments "buildx", "build", "--file=$env:DOCKERFILE",
"--platform=$env:PLATFORM",
"--build-arg", "VERSION=$env:VERSION",
"--build-arg", "REVISION=$env:REVISION",
"--build-arg", "BUILDDATE=$env:BUILDDATE",
"--tag", $env:LOCAL_REPO,
"--load",
"."
}

## TARGET push Publish to container registry.
function Invoke-Target-Push {
Write-Target "push"
Invoke-CommandWithEcho $env:DOCKER -Arguments "tag", $env:LOCAL_REPO, "$($env:DOCKER_REPO):v$($env:VERSION)-$($env:PLATFORM -replace '/','-')"
Invoke-CommandWithEcho $env:DOCKER -Arguments "push", "$($env:DOCKER_REPO):v$($env:VERSION)-$($env:PLATFORM -replace '/','-')"
}

## TARGET test-image Test local image
function Invoke-Target-Test-Image {
Write-Target "test-image"
Invoke-CommandWithEcho $env:DOCKER -Arguments "run", "--platform=$env:PLATFORM", "--rm", "-t", $env:LOCAL_REPO, "-v"
}

function Invoke-Target-Help {
Write-Host "Targets:"
Get-Content $PSCommandPath |
Expand Down
1 change: 1 addition & 0 deletions dist/artifacts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"tf","path":"dist/tf_darwin_arm64/tf","goos":"darwin","goarch":"arm64","internal_type":4,"type":"Binary","extra":{"Binary":"tf","Ext":"","ID":"tf"}}]
Loading

0 comments on commit ef813e0

Please sign in to comment.