From 7a478c9e8ec6fb3cd1687a17fd47c84a7e14b24f Mon Sep 17 00:00:00 2001 From: Marcin Date: Tue, 11 Jun 2024 18:34:33 -0700 Subject: [PATCH] build script --- .github/workflows/build_continuous.yaml | 8 +------- .github/workflows/release.yaml | 10 ++-------- .vscode/tasks.json | 7 +------ build.go | 8 +++++++- src/bin/build-scripts/build.sh | 8 ++++++++ 5 files changed, 19 insertions(+), 22 deletions(-) create mode 100755 src/bin/build-scripts/build.sh diff --git a/.github/workflows/build_continuous.yaml b/.github/workflows/build_continuous.yaml index d2df423..827416c 100644 --- a/.github/workflows/build_continuous.yaml +++ b/.github/workflows/build_continuous.yaml @@ -21,16 +21,10 @@ jobs: - name: Dependencies run: | go get . - - - name: Build Info - run: | - echo "GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> "$GITHUB_ENV" - echo "GIT_HASH=$(git rev-parse --verify HEAD)" >> "$GITHUB_ENV" - echo "GIT_DIRTY=$(git diff --quiet; [ $? -eq 0 ] && echo false || echo true)" >> "$GITHUB_ENV" - name: Build run: | - CGO_ENABLED=0 go build -ldflags "-X main.gitBranch=$GIT_BRANCH -X main.gitHash=$GIT_HASH -X main.gitDirty=$GIT_DIRTY" + ./src/bin/build-scripts/build.sh - name: Copy Binary run: | diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c6ad505..f386c92 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -22,17 +22,11 @@ jobs: - name: Dependencies run: | go get . - - - name: Build Info - run: | - echo "GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> "$GITHUB_ENV" - echo "GIT_HASH=$(git rev-parse --verify HEAD)" >> "$GITHUB_ENV" - echo "GIT_DIRTY=$(git diff --quiet; [ $? -eq 0 ] && echo false || echo true)" >> "$GITHUB_ENV" - name: Build run: | - CGO_ENABLED=0 go build -ldflags "-X main.gitBranch=$GIT_BRANCH -X main.gitHash=$GIT_HASH -X main.gitDirty=$GIT_DIRTY" - + ./src/bin/build-scripts/build.sh + - name: Copy Binary run: | mkdir -p release-bin/ diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 01f499a..a8a988c 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,12 +4,7 @@ { "label": "Build", "type": "shell", - "command": "go", - "args": [ - "build", - "-ldflags", - "\"-X main.gitBranch=`git rev-parse --abbrev-ref HEAD` -X main.gitHash=`git rev-parse --verify HEAD` -X main.gitDirty=`git diff --quiet; [ $? -eq 0 ] && echo false || echo true`\"", - ], + "command": "./src/bin/build-scripts/build.sh", "group": { "kind": "build", "isDefault": true diff --git a/build.go b/build.go index 6058329..70b5144 100644 --- a/build.go +++ b/build.go @@ -2,11 +2,13 @@ package main import "encoding/json" +var gitTag string // populated with -ldflags var gitBranch string // populated with -ldflags var gitHash string // populated with -ldflags var gitDirty string // populated with -ldflags type BuildInfo struct { + GitTag string `json:"gitTag"` GitBranch string `json:"gitBranch"` GitHash string `json:"gitHash"` GitDirty bool `json:"gitDirty"` @@ -23,14 +25,18 @@ func (b *BuildInfo) toJson() string { func GetBuildInfo() BuildInfo { info := BuildInfo{ + GitTag: "unknown", GitBranch: "unknown", GitHash: "unknown", GitDirty: false, } + if gitTag != "" { + info.GitTag = gitTag + } + if gitBranch != "" { info.GitBranch = gitBranch - } if gitHash != "" { diff --git a/src/bin/build-scripts/build.sh b/src/bin/build-scripts/build.sh new file mode 100755 index 0000000..115a90d --- /dev/null +++ b/src/bin/build-scripts/build.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +GIT_TAG=$(git describe --tags 2>/dev/null) +GIT_BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null) +GIT_HASH=$(git rev-parse --verify HEAD 2>/dev/null) +GIT_DIRTY=$(git diff --quiet; [ $? -eq 0 ] && echo false || echo true) + +CGO_ENABLED=0 go build -ldflags "-X main.gitTag=$GIT_TAG -X main.gitBranch=$GIT_BRANCH -X main.gitHash=$GIT_HASH -X main.gitDirty=$GIT_DIRTY"