Skip to content

Commit

Permalink
add CI for release and create image (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alva8756 authored Nov 1, 2023
1 parent b5b9929 commit ed46e71
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: release

on:
push:
tags:
- 'v*.*.*'

jobs:
goreleaser:
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
packages: write
steps:
-
name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.20"
-
name: install cosign
uses: sigstore/cosign-installer@main
-
uses: anchore/sbom-action/[email protected]
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COSIGN_EXPERIMENTAL: 1
GOVERSION: "1.20"
86 changes: 86 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
project_name: hollow-bomservice
before:
hooks:
- go mod tidy

builds:
- id: go
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
ldflags:
- -X "github.com/metal-toolbox/hollow-bomservice/internal/version.AppVersion={{ .Version }}"
-X "github.com/metal-toolbox/hollow-bomservice/internal/version.GoVersion={{ .Env.GOVERSION }}"
-X "github.com/metal-toolbox/hollow-bomservice/internal/version.GitCommit={{ .Commit }}"
-X "github.com/metal-toolbox/hollow-bomservice/internal/version.GitBranch={{ .Branch }}"
-X "github.com/metal-toolbox/hollow-bomservice/internal/version.BuildDate={{ .Date }}"

archives:
- id: go
format: tar.gz
name_template: >-
{{ .ProjectName }}_
{{ .Version }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}64bit
{{- else if eq .Arch "386" }}32bit
{{- else if eq .Arch "arm" }}ARM
{{- else if eq .Arch "arm64" }}ARM64
{{- else }}{{ .Arch }}{{ end }}
files:
- README.md

checksum:
name_template: "checksums.txt"

snapshot:
name_template: "{{ .Tag }}-next"

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"

dockers:
- image_templates:
- "ghcr.io/metal-toolbox/{{.ProjectName}}:{{ .Tag }}"
- "ghcr.io/metal-toolbox/{{.ProjectName}}:latest"
dockerfile: Dockerfile
build_flag_templates:
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"

sboms:
- artifacts: archive
- id: source
artifacts: source

signs:
- cmd: cosign
signature: "${artifact}.sig"
certificate: "${artifact}.pem"
args:
- "sign-blob"
- "--oidc-issuer=https://token.actions.githubusercontent.com"
- "--output-certificate=${certificate}"
- "--output-signature=${signature}"
- "${artifact}"
- "--yes" # required on cosign 2.0.0+
artifacts: all
output: true

docker_signs:
- cmd: cosign
args:
- "sign"
- "--oidc-issuer=https://token.actions.githubusercontent.com"
- "${artifact}"
- "--yes" # required on cosign 2.0.0+
artifacts: all
output: true
59 changes: 59 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package version

import (
"fmt"
"runtime"
"runtime/debug"
"strings"
)

var (
GitCommit string
GitBranch string
GitSummary string
BuildDate string
AppVersion string
ServerserviceVersion = serverserviceVersion()
GoVersion = runtime.Version()
)

type Version struct {
GitCommit string `json:"git_commit"`
GitBranch string `json:"git_branch"`
GitSummary string `json:"git_summary"`
BuildDate string `json:"build_date"`
AppVersion string `json:"app_version"`
GoVersion string `json:"go_version"`
ServerserviceVersion string `json:"serverservice_version"`
}

func Current() *Version {
return &Version{
GitBranch: GitBranch,
GitCommit: GitCommit,
GitSummary: GitSummary,
BuildDate: BuildDate,
AppVersion: AppVersion,
GoVersion: GoVersion,
ServerserviceVersion: ServerserviceVersion,
}
}

func (v *Version) String() string {
return fmt.Sprintf("version=%s ref=%s branch=%s built=%s", v.AppVersion, v.GitCommit, v.GitBranch, v.BuildDate)
}

func serverserviceVersion() string {
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
return ""
}

for _, d := range buildInfo.Deps {
if strings.Contains(d.Path, "serverservice") {
return d.Version
}
}

return ""
}

0 comments on commit ed46e71

Please sign in to comment.