Skip to content

Commit

Permalink
build script
Browse files Browse the repository at this point in the history
  • Loading branch information
mpawlowski committed Jun 12, 2024
1 parent cde4126 commit 7a478c9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/build_continuous.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
10 changes: 2 additions & 8 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
7 changes: 1 addition & 6 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 != "" {
Expand Down
8 changes: 8 additions & 0 deletions src/bin/build-scripts/build.sh
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 7a478c9

Please sign in to comment.