Skip to content

Commit

Permalink
release binaries
Browse files Browse the repository at this point in the history
improve the way the version is configured so it can be set by the
docker build.

Signed-off-by: Yves Brissaud <[email protected]>
  • Loading branch information
eunomie committed Oct 6, 2024
1 parent 051bc96 commit c8409b7
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 7 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Release on tag

on:
push:
tags:
- 'v*'

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

- name: Install Task
uses: arduino/setup-task@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Set up containerd
uses: crazy-max/ghaction-setup-docker@v3
with:
set-host: true
daemon-config: |
{
"features": {
"containerd-snapshotter": true
}
}
- name: Binaries
run: task go:bin:all

- name: Create Release
uses: softprops/action-gh-release@v1
with:
files: |
dist/*/docker-runx-*
46 changes: 46 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# syntax=docker/dockerfile:1.4

ARG XX_VERSION=1.2.1
ARG ALPINE_VERSION=3.20
ARG GO_VERSION=1.23.1

ARG BIN_NAME

FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx

FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS build-base
COPY --from=xx / /
RUN apk add --no-cache curl
RUN sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin
RUN apk add --no-cache git ca-certificates openssh-client

FROM build-base AS build
ARG TARGETPLATFORM
RUN xx-go --wrap
WORKDIR /go/src/
COPY go.mod ./
COPY go.sum ./
RUN --mount=type=ssh \
--mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/go/pkg/mod \
go mod download
COPY . ./

FROM build AS binary
ENV CGO_ENABLED=0
ARG BIN_NAME
RUN --mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/go/pkg/mod \
GIT_VERSION=$(git describe --tags | cut -c 2-) && \
xx-go build \
-o dist/${BIN_NAME} \
-ldflags="-w -s \
-X {{.PKG_NAME}}/internal/constants.Version=$GIT_VERSION" \
./cmd/${BIN_NAME} && \
xx-verify dist/${BIN_NAME}

FROM scratch AS export-bin
ARG BIN_NAME
ARG TARGETOS
ARG TARGETARCH
COPY --from=binary /go/src/dist/${BIN_NAME} /${BIN_NAME}-${TARGETOS}-${TARGETARCH}
16 changes: 16 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ version: '3'
vars:
BIN_NAME: docker-runx
PKG_NAME: github.com/eunomie/docker-runx
BIN_PLATFORMS: linux/amd64,linux/arm64,darwin/amd64,darwin/arm64,windows/amd64,windows/arm64
GOLANGCI_LINT_IMAGE_NAME: docker/golangci-lint:1.61.0-go1.23.1

tasks:
Expand Down Expand Up @@ -42,6 +43,17 @@ tasks:
VERSION:
sh: git describe --tags | cut -c 2-

go:bin:all:
cmds:
- |
docker buildx build \
--build-arg BIN_NAME={{.BIN_NAME}} \
-f Dockerfile \
--platform {{.BIN_PLATFORMS}} \
--target export-bin \
-o type=local,dest=dist \
.
go:fmt:
cmds:
- find . -type f -name "*.go" -exec .github/remove_empty_imports.sh "{}" \;
Expand Down Expand Up @@ -84,3 +96,7 @@ tasks:
- go:lint
- go:test
- go:gendocs

clean:
cmds:
- rm -rf dist
2 changes: 1 addition & 1 deletion internal/commands/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func NewCmd(_ command.Cli) *cobra.Command {
Use: "version",
Short: "Show Docker RunX version information",
Run: func(_ *cobra.Command, _ []string) {
fmt.Println(constants.Version)
fmt.Println(constants.Short())
},
}

Expand Down
40 changes: 34 additions & 6 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"runtime"
"runtime/debug"
"strings"
"time"
)

const (
Expand All @@ -17,19 +19,45 @@ const (
)

var (
Version string = "devel"
commit string
UserAgent string
Version string = "(devel)"
Revision string
LastCommit time.Time
DirtyBuild bool
UserAgent string
)

func init() {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
commit = setting.Value
if setting.Value == "" {
continue
}
switch setting.Key {
case "vcs.revision":
Revision = setting.Value
case "vcs.time":
LastCommit, _ = time.Parse(time.RFC3339, setting.Value)
case "vcs.modified":
DirtyBuild = setting.Value == "true"
}
}
}

UserAgent = fmt.Sprintf("%s/%s go/%s git-commit/%s", BinaryName, Version, runtime.Version(), commit)
UserAgent = fmt.Sprintf("%s/%s go/%s git-commit/%s", BinaryName, Version, runtime.Version(), Revision)
}

func Short() string {
parts := make([]string, 0, 3)
parts = append(parts, Version)
if Revision != "unknown" && Revision != "" {
commit := Revision
if len(commit) > 7 {
commit = commit[:7]
}
parts = append(parts, commit)
if DirtyBuild {
parts = append(parts, "dirty")
}
}
return strings.Join(parts, "-")
}

0 comments on commit c8409b7

Please sign in to comment.