Skip to content

Commit 28e0268

Browse files
build scripts
1 parent 8b64328 commit 28e0268

File tree

6 files changed

+134
-2
lines changed

6 files changed

+134
-2
lines changed

.editorconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
root = true
22

3+
[Makefile, *.yml, *.yaml]
4+
indent_style = tab
5+
indent_size = 4
6+
37
[*]
48
charset = utf-8
59
indent_style = space
610
indent_size = 2
711
end_of_line = lf
812
insert_final_newline = true
913
trim_trailing_whitespace = true
14+
max_line_length = 120
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: GoReleaser
1+
name: Release
22

33
on:
44
push:
@@ -18,7 +18,7 @@ jobs:
1818
- name: Set up Go
1919
uses: actions/setup-go@v5
2020
with:
21-
go-version: 1.23
21+
go-version: 1.24
2222

2323
- name: Run GoReleaser
2424
uses: goreleaser/goreleaser-action@v6

.goreleaser.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: 2
2+
project_name: git-hours
3+
before:
4+
hooks:
5+
- go mod tidy
6+
builds:
7+
- main: "main.go"
8+
binary: git-hours
9+
env:
10+
- CGO_ENABLED=0
11+
goos:
12+
- windows
13+
- linux
14+
- darwin
15+
goarch:
16+
- amd64
17+
- "386"
18+
- arm64
19+
ignore:
20+
- goos: darwin
21+
goarch: "386"
22+
- goos: windows
23+
goarch: "arm64"
24+
archives:
25+
- name_template: >-
26+
{{ .ProjectName }}_
27+
{{- title .Os }}_
28+
{{- if eq .Arch "amd64" }}x86_64
29+
{{- else if eq .Arch "386" }}i386
30+
{{- else }}{{ .Arch }}{{ end }}
31+
format_overrides:
32+
- goos: windows
33+
format: zip
34+
release:
35+
prerelease: auto

Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
NAME := git-hours
2+
PACKAGE := github.com/trinhminhtriet/$(NAME)
3+
DATE :=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
4+
GIT := $(shell [ -d .git ] && git rev-parse --short HEAD)
5+
VERSION := $(shell git describe --tags)
6+
7+
default: build
8+
9+
tidy:
10+
go get -u && go mod tidy
11+
12+
build:
13+
CGO_ENABLED=0 go build \
14+
-a -tags netgo -o dist/${NAME} main.go
15+
16+
build-link:
17+
CGO_ENABLED=0 go build \
18+
-a -tags netgo -o dist/${NAME} main.go
19+
ln -sf ${PWD}/dist/${NAME} /usr/local/bin/${NAME}
20+
21+
release:
22+
goreleaser build --clean --snapshot --single-target
23+
24+
release-all:
25+
goreleaser build --clean --snapshot
26+
27+
link:
28+
ln -sf ${PWD}/dist/${NAME} /usr/local/bin/${NAME}
29+
which ${NAME}
30+
31+
clean:
32+
$(RM) -rf dist
33+
34+
.PHONY: default tidy build build-link release release-all

build.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
set -e
3+
4+
APP_NAME="git-hours"
5+
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
6+
PACKAGE_NAME=github.com/trinhminhtriet/$APP_NAME
7+
8+
CURRENT_TAG_NAME="$(git describe --tags --abbrev=0)"
9+
CURRENT_VERSION="${CURRENT_TAG_NAME//v/}"
10+
11+
IFS='.' read -r major minor patch <<<"$CURRENT_VERSION"
12+
echo "Current version: $CURRENT_VERSION"
13+
new_patch=$((patch + 1))
14+
NEW_VERSION="$major.$minor.$new_patch"
15+
16+
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
17+
if [[ $GIT_BRANCH == "master" ]]; then
18+
TAG_NAME="v$NEW_VERSION"
19+
else
20+
TAG_NAME="$NEW_VERSION-$GIT_BRANCH"
21+
fi
22+
23+
# Create the new version.go content
24+
VERSION_FILE_CONTENT="package internal
25+
26+
var (
27+
\tVERSION = \"$NEW_VERSION\"
28+
\tDATE = \"$BUILD_DATE\"
29+
)
30+
"
31+
echo -e "$VERSION_FILE_CONTENT" >internal/version.go
32+
33+
git add internal/version.go
34+
git commit -m "Update version to $NEW_VERSION"
35+
git tag -a $TAG_NAME -m "Tag version $TAG_NAME"
36+
git push origin $TAG_NAME
37+
git push origin $GIT_BRANCH
38+
39+
GIT_COMMIT=$(git rev-parse --short HEAD)
40+
41+
echo "New version: $NEW_VERSION"
42+
echo "New tag name: $TAG_NAME"
43+
echo "Git branch: $GIT_BRANCH"
44+
echo "Build date: $BUILD_DATE"
45+
46+
export CGO_ENABLED=0
47+
LDFLAGS="-s -w \
48+
-X \"$PACKAGE_NAME/internal.VERSION=$NEW_VERSION\" \
49+
-X \"$PACKAGE_NAME/internal.DATE=$BUILD_DATE\"\
50+
"
51+
52+
mkdir -p dist
53+
go build -o ./dist/git-hours main.go
54+
ln -sf $(PWD)/dist/git-hours /usr/local/bin/git-hours
55+
56+
echo "Build completed with VERSION=$NEW_VERSION, DATE=$BUILD_DATE, BRANCH=$GIT_BRANCH, COMMIT=$GIT_COMMIT."

dist/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

0 commit comments

Comments
 (0)