Skip to content

Commit

Permalink
Update GitHub Action release workflow
Browse files Browse the repository at this point in the history
Use goreleaser to build binary, images and perform attestation.
Note: At the moment we only build amd64 versions as arm64 is
currently broken because of kafka dependency that is only build
for amd64.
  • Loading branch information
cjh1 committed Dec 20, 2024
1 parent 7d97788 commit 3e65e65
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 10 deletions.
87 changes: 77 additions & 10 deletions .github/workflows/build_and_release_image.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,79 @@
name: Build and Publish Docker Images
name: Release with goreleaser

on:
- push # Perform a build of the contents from the branch
- pull_request # Perform a build after merging with the target branch
- workflow_dispatch
push:
tags:
- v*

permissions: write-all # Necessary for the generate-build-provenance action with containers

jobs:
build_and_release:
uses: Cray-HPE/hms-build-image-workflows/.github/workflows/build_and_release_image.yaml@v2
with:
image-name: cray-power-control
enable-pr-comment: true
secrets: inherit
build:
runs-on: ubuntu-latest
steps:
- name: Install cross-compilation tools
run: |
sudo apt-get update
sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
- name: Set up latest stable Go
uses: actions/setup-go@v5
with:
go-version: stable
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Checkout out the repo
uses: actions/checkout@v4
with:
fetch-tags: 1
fetch-depth: 1

# Set environment variables required by GoReleaser
- name: Set build environment variables
run: |
echo "GIT_STATE=$(if git diff-index --quiet HEAD --; then echo 'clean'; else echo 'dirty'; fi)" >> $GITHUB_ENV
echo "BUILD_HOST=$(hostname)" >> $GITHUB_ENV
echo "GO_VERSION=$(go version | awk '{print $3}')" >> $GITHUB_ENV
echo "BUILD_USER=$(whoami)" >> $GITHUB_ENV
- name: Docker Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Release with goreleaser
uses: goreleaser/goreleaser-action@v6
env:
GITHUB_TOKEN: ${{ github.token }}
with:
version: '~> v2'
args: release --clean
id: goreleaser

- name: Process goreleaser output
id: process_goreleaser_output
run: |
echo "const fs = require('fs');" > process.js
echo 'const artifacts = ${{ steps.goreleaser.outputs.artifacts }}' >> process.js
echo "const firstNonNullDigest = artifacts.find(artifact => artifact.extra && artifact.extra.Digest != null)?.extra.Digest;" >> process.js
echo "console.log(firstNonNullDigest);" >> process.js
echo "fs.writeFileSync('digest.txt', firstNonNullDigest);" >> process.js
node process.js
echo "digest=$(cat digest.txt)" >> $GITHUB_OUTPUT
- name: Attest power-control binary amd64
uses: actions/attest-build-provenance@v1
with:
subject-path: dist/pcs_linux_amd64_v3/hms-power-control
# - name: Attest power-control binary arm64
# uses: actions/attest-build-provenance@v1
# with:
# subject-path: dist/pcs_linux_arm64_v8.0/hms-power-control
- name: Generate build provenance
uses: actions/attest-build-provenance@v1
with:
subject-name: ghcr.io/cjh1/pcs
subject-digest: ${{ steps.process_goreleaser_output.outputs.digest }}
push-to-registry: true
127 changes: 127 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
version: 2.4

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

builds:
- id: pcs
main: ./cmd/hms-power-control
binary: hms-power-control
goos:
- linux
#- darwin
goarch:
- amd64
#- arm64
goamd64:
- v3
env:
- CGO_ENABLED=1
ldflags:
- "-s -w -X main.GitCommit={{.Commit}} \
-X main.BuildTime={{.Timestamp}} \
-X main.Version={{.Version}} \
-X main.GitBranch={{.Branch}} \
-X main.GitTag={{.Tag}} \
-X main.GitState={{ .Env.GIT_STATE }} \
-X main.BuildHost={{ .Env.BUILD_HOST }} \
-X main.GoVersion={{ .Env.GO_VERSION }} \
-X main.BuildUser={{ .Env.BUILD_USER }} "

dockers:
- image_templates:
- &amd64_linux_image ghcr.io/cjh1/{{.ProjectName}}:{{ .Tag }}-amd64
- ghcr.io/cjh1/{{.ProjectName}}:{{ .Major }}-amd64
- ghcr.io/cjh1/{{.ProjectName}}:{{ .Major }}.{{ .Minor }}-amd64
use: buildx
build_flag_templates:
- "--pull"
- "--platform=linux/amd64"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
goarch: amd64
goamd64: v3
extra_files:
- LICENSE
- CHANGELOG.md
- README.md
- configs/
- .version

# - image_templates:
# - &arm64v8_linux_image ghcr.io/cjh1/{{.ProjectName}}:{{ .Tag }}-arm64
# - ghcr.io/cjh1/{{.ProjectName}}:{{ .Major }}-arm64
# - ghcr.io/cjh1/{{.ProjectName}}:{{ .Major }}.{{ .Minor }}-arm64
# use: buildx
# build_flag_templates:
# - "--pull"
# - "--platform=linux/arm64"
# - "--label=org.opencontainers.image.created={{.Date}}"
# - "--label=org.opencontainers.image.title={{.ProjectName}}"
# - "--label=org.opencontainers.image.revision={{.FullCommit}}"
# - "--label=org.opencontainers.image.version={{.Version}}"
# goarch: arm64
# extra_files:
# - LICENSE
# - CHANGELOG.md
# - README.md
# - configs/
# - .version

docker_manifests:
- name_template: "ghcr.io/cjh1/{{.ProjectName}}:latest"
image_templates:
- *amd64_linux_image
#- *arm64v8_linux_image

- name_template: "ghcr.io/cjh1/{{.ProjectName}}:{{ .Tag }}"
image_templates:
- *amd64_linux_image
#- *arm64v8_linux_image

- name_template: "ghcr.io/cjh1/{{.ProjectName}}:{{ .Major }}"
image_templates:
- *amd64_linux_image
#- *arm64v8_linux_image

- name_template: "ghcr.io/cjh1/{{.ProjectName}}:{{ .Major }}.{{ .Minor }}"
image_templates:
- *amd64_linux_image
#- *arm64v8_linux_image

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 }}
files:
- LICENSE
- CHANGELOG.md
- README.md

checksum:
name_template: 'checksums.txt'
snapshot:
version_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'


# The lines beneath this are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Power Control Service(pcs)

This is a fork of the original PCS code from [Cray-HPE/hms-power-control](https://github.com/Cray-HPE/hms-power-control), suitable only for experimentation and demo purposes at this point.

## Build/Install with goreleaser

This project uses [GoReleaser](https://goreleaser.com/) to automate releases and include additional build metadata such as commit info, build time, and versioning. Below is a guide on how to set up and build the project locally using GoReleaser.

### Environment Variables

To include detailed build metadata, ensure the following environment variables are set:

* __GIT_STATE__: Indicates whether there are uncommitted changes in the working directory. Set to clean if the repository is clean, or dirty if there are uncommitted changes.
* __BUILD_HOST__: The hostname of the machine where the build is being performed.
* __GO_VERSION__: The version of Go used for the build. GoReleaser uses this to ensure consistent Go versioning information.
* __BUILD_USER__: The username of the person or system performing the build.

Set all the environment variables with:
```bash
export GIT_STATE=$(if git diff-index --quiet HEAD --; then echo 'clean'; else echo 'dirty'; fi)
export BUILD_HOST=$(hostname)
export GO_VERSION=$(go version | awk '{print $3}')
export BUILD_USER=$(whoami)
```

### Building Locally with GoReleaser

Once the environment variables are set, you can build the project locally using GoReleaser in snapshot mode (to avoid publishing).


Follow the installation instructions from [GoReleaser’s documentation](https://goreleaser.com/install/).

1. Run GoReleaser in snapshot mode with the --snapshot flag to create a local build without attempting to release it:
```bash
goreleaser release --snapshot --clean
```
2. Check the dist/ directory for the built binaries, which will include the metadata from the environment variables. You can inspect the binary output to confirm that the metadata was correctly embedded.

__NOTE__ If you see errors, ensure that you are using the same version of goreleaser that is being used in the [Release Action](.github/workflows/Release.yml)

0 comments on commit 3e65e65

Please sign in to comment.