Skip to content

Commit

Permalink
feat: add goreleaser-release command (#78)
Browse files Browse the repository at this point in the history
* feat: add goreleaser-release command

* ci: add test for goreleaser release

* ci: add app for testing

* docs: add goreleaser release example

* fix: lint errors
  • Loading branch information
brivu authored Oct 2, 2023
1 parent 22b066e commit 8c1d56c
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 3 deletions.
20 changes: 19 additions & 1 deletion .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ jobs:
name: go/default
tag: "1.20.8"
steps:
- checkout
- go/install-goreleaser
int-test-goreleaser-release:
executor:
name: go/default
tag: "1.20.8"
steps:
- go/install-goreleaser
- go/goreleaser-release:
github-token: TEST_GH_TOKEN
validate-yaml: true
publish-release: false
project-path: ~/project/tests
int-test-cimg-go:
executor:
name: go/default
Expand Down Expand Up @@ -120,6 +130,13 @@ workflows:
# Make sure to include "filters: *filters" in every test job you want to run as part of your deployment.
- int-test-goreleaser-install:
filters: *filters
- int-test-goreleaser-release:
pre-steps:
- checkout
- run:
name: Change to test directory
command: cd ~/project/tests
filters: *filters
- int-test-cimg-go:
filters: *filters
- int-test-cimg-base:
Expand Down Expand Up @@ -150,6 +167,7 @@ workflows:
- int-test-dirty-cache
- int-test-vm-arm64
- int-test-goreleaser-install
- int-test-goreleaser-release
context: orb-publisher
filters:
branches:
Expand Down
37 changes: 37 additions & 0 deletions src/commands/goreleaser-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
description: |
Build Go applications and release them on GitHub.
GoReleaser CLI must be installed and a GitHub Token
with write:packages permissions is also required.
parameters:
github-token:
description: |
A GitHub Token is with write:packages permissions is required.
type: string
default: GITHUB_TOKEN
project-path:
description: |
The path to the directory containing your Go project files:
.goreleaser.yaml, go.mod, main.go.
Defaults to $HOME.
type: string
default: $HOME
validate-yaml:
description: |
Set to true to validate .goreleaser.yaml.
type: boolean
default: false
publish-release:
description: |
Set to true to publish release to GitHub
type: boolean
default: false
steps:
- run:
name: "Build and release Go Binaries to GitHub with GoReleaser"
environment:
GITHUB_TOKEN: << parameters.github-token >>
GO_BOOL_VALIDATE_YAML: << parameters.validate-yaml >>
GO_BOOL_PUBLISH_RELEASE: << parameters.publish-release >>
GO_EVAL_PROJECT_PATH: << parameters.project-path >>
command: << include(scripts/goreleaser-release.sh) >>
3 changes: 1 addition & 2 deletions src/commands/install-goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
description: |
GoReleaser is a release automation tool for managing Go projects.
Install GoReleaser in your build. Golang must be installed.
A GitHub Token is with write:packages permissions is also required.
parameters:
version:
description: The GoRelaser version to install. Defaults to latest
description: The GoReleaser version to install. Defaults to latest
type: string
default: "latest"

Expand Down
25 changes: 25 additions & 0 deletions src/examples/goreleaser-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
description: |
Build Go applications and release them on GitHub.
Install GoReleaser CLI and add a GitHub Token with
write:packages permissions and store as environment variable.
usage:
version: 2.1
orbs:
go: circleci/[email protected]
jobs:
go-build-release:
executor:
name: go/default
tag: "1.20.8"
steps:
- go/install-goreleaser
- go/goreleaser-release:
# validate .goreleaser.yml
validate-yaml: true
# publish release on GitHub
publish-release: true
project-path: /path/to/Go/project
workflows:
main:
jobs:
- go-build-release
27 changes: 27 additions & 0 deletions src/scripts/goreleaser-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

GITHUB_TOKEN="$(echo "\$$GITHUB_TOKEN" | circleci env subst)"
GO_EVAL_PROJECT_PATH="$(eval echo "${GO_EVAL_PROJECT_PATH}")"

# Change to directory containing project files
cd "${GO_EVAL_PROJECT_PATH}" || return

if [ -z "${GITHUB_TOKEN}" ]; then
echo "No GitHub Token provided. Please add token as environment variable in CircleCI."
exit 1
fi

if [ "$GO_BOOL_VALIDATE_YAML" -eq 1 ]; then
# Validate .goreleaser.yaml file
goreleaser check
fi

if [ "$GO_BOOL_PUBLISH_RELEASE" -eq 1 ]; then
# Build binaries and publish release to GitHub
goreleaser release
else
# Build binaries and test release locally
goreleaser release --snapshot --clean
fi


44 changes: 44 additions & 0 deletions tests/.goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com

# The lines bellow are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/need to use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...

builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of `uname`.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
3 changes: 3 additions & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module main

go 1.20
6 changes: 6 additions & 0 deletions tests/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// main.go
package main

func main() {
println("Hello, World!")
}

0 comments on commit 8c1d56c

Please sign in to comment.