Skip to content

Commit

Permalink
UPDATE: build/release via Github Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
fileformat committed Apr 28, 2024
1 parent 543493e commit ce75196
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: build

on:
push:
branches:
- main
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set some env vars for the build
run: |
TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
echo "COMMIT=${GITHUB_SHA:0:7}" >> $GITHUB_ENV
echo "LASTMOD=${TIMESTAMP}" >> $GITHUB_ENV
echo "BUILTBY=build.yaml" >> $GITHUB_ENV
echo "IMAGE_NAME=fileformat/ghashboard" >> $GITHUB_ENV
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: |
ghcr.io/${{ env.IMAGE_NAME }}:latest
build-args: |
COMMIT=${{ env.COMMIT }}
LASTMOD=${{ env.LASTMOD }}
VERSION=custombuild
BUILTBY=${{ env.BUILTBY }}
68 changes: 68 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: release

on:
push:
# run only against version tags
tags:
- 'v*'

permissions:
contents: write
packages: write

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

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

- name: Set some env vars for the build
run: |
TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
echo "COMMIT=${GITHUB_SHA:0:7}" >> $GITHUB_ENV
echo "LASTMOD=${TIMESTAMP}" >> $GITHUB_ENV
echo "BUILTBY=build.yaml" >> $GITHUB_ENV
echo "IMAGE_NAME=fileformat/ghashboard" >> $GITHUB_ENV
echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_ENV
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: |
ghcr.io/${{ env.IMAGE_NAME }}:${{ env.VERSION }}
ghcr.io/${{ env.IMAGE_NAME }}:latest
build-args: |
COMMIT=${{ env.COMMIT }}
LASTMOD=${{ env.LASTMOD }}
VERSION=${{ env.VERSION }}
BUILTBY=${{ env.BUILTBY }}
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.22.1

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44 changes: 44 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
- go mod tidy
builds:
- env:
- CGO_ENABLED=0
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} -X main.builtBy=goreleaser
main: ./ghashboard.go
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
checksum:
name_template: 'checksums.txt'
snapshot:
name_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
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM golang:1.22-alpine AS builder

RUN apk add --no-cache upx

RUN mkdir /build
ADD . /build/
WORKDIR /build
ARG COMMIT
ARG LASTMOD
ARG VERSION
ARG BUILTBY
RUN echo "INFO: building for $COMMIT on $LASTMOD"

ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64

RUN go build \
-a \
-ldflags "-s -w -X main.commit=$COMMIT -X main.date=$LASTMOD -X main.version=$VERSION -X main.builtBy=$BUILTBY -extldflags '-static'" \
-o ghashboard \
./*.go \
&& upx ghashboard

FROM scratch
COPY --from=builder /build/ghashboard /bin/ghashboard
WORKDIR /bin
ENTRYPOINT ["/bin/ghashboard"]

7 changes: 7 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func initConfig(args []string) {
help := f.Bool("help", false, "Show help")
f.MarkHidden("help")

versionFlag := f.Bool("version", false, "Show version information")

f.String("log-level", "warn", "Log level [ debug | info | warn | error ]")
viper.BindPFlag("log-level", f.Lookup("log-level"))
viper.BindEnv("log-level", "LOG_LEVEL", "INPUT_LOG_LEVEL")
Expand Down Expand Up @@ -107,6 +109,11 @@ func initConfig(args []string) {

f.Parse(args)

if *versionFlag {
fmt.Printf("ghashboard v%s (%s built on %s by %s)\n", version, commit, date, builtBy)
os.Exit(0)
}

if *help {
usage(f)
os.Exit(0)
Expand Down
5 changes: 5 additions & 0 deletions ghashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type MetaRepo struct {
}

var (
// version info
version string
commit string
date string
builtBy string

// workflow flags
IncludeSet map[string]struct{}
Expand Down

0 comments on commit ce75196

Please sign in to comment.