Skip to content

Commit 08f20c6

Browse files
committed
replace go generate into bash script
1 parent 0c61356 commit 08f20c6

File tree

7 files changed

+54
-81
lines changed

7 files changed

+54
-81
lines changed

.github/workflows/build.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ jobs:
1414
runs-on: ubuntu-24.04
1515
steps:
1616
- uses: actions/checkout@v4
17-
- uses: actions/setup-go@v5
1817
- uses: docker/setup-qemu-action@v3
1918
- uses: docker/setup-buildx-action@v3
2019

@@ -25,7 +24,10 @@ jobs:
2524
username: ${{ github.repository_owner }}
2625
password: ${{ secrets.GITHUB_TOKEN }}
2726

28-
- run: go generate ./...
27+
- name: Prepare
28+
run: ./scripts/gen-version-file.sh
29+
env:
30+
BRING_VERSION: edge
2931

3032
- name: Build and push
3133
uses: docker/build-push-action@v4

.github/workflows/release.yaml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ on:
1515
jobs:
1616
build:
1717
runs-on: ubuntu-24.04
18+
env:
19+
VERSION_NAME: ${{ github.event_name == 'workflow_dispatch' && inputs.version || github.ref_name }}
1820
steps:
1921
- uses: actions/checkout@v4
20-
- uses: actions/setup-go@v5
2122
- uses: docker/setup-qemu-action@v3
2223
- uses: docker/setup-buildx-action@v3
2324

@@ -28,28 +29,29 @@ jobs:
2829
username: ${{ github.repository_owner }}
2930
password: ${{ secrets.GITHUB_TOKEN }}
3031

31-
- name: Prepare
32-
run: |
33-
go generate ./...
34-
cat ./cmd/version.g.go
35-
env:
36-
BRING_VERSION: ${{ github.event_name != 'workflow_dispatch' && github.ref_name || inputs.version }}
37-
3832
- name: Image meta
3933
id: meta
4034
uses: docker/metadata-action@v5
4135
with:
42-
images: ghcr.io/lesomnus/bring:edge
36+
images: ghcr.io/lesomnus/bring
4337
tags: |
4438
type=semver,pattern={{version}}
4539
type=semver,pattern={{major}}.{{minor}}
4640
41+
- name: Prepare
42+
run: ./scripts/gen-version-file.sh
43+
env:
44+
BRING_VERSION: ${{ env.VERSION_NAME }}
45+
4746
- name: Build and push
4847
uses: docker/build-push-action@v4
4948
with:
5049
context: .
50+
tags: ${{ steps.meta.outputs.tags }}
51+
labels: ${{ steps.meta.outputs.labels }}
5152
platforms: linux/amd64,linux/arm64
52-
tags: ghcr.io/lesomnus/bring:edge
5353
push: true
54+
build-args: |
55+
VERSION_NAME=${{ env.VERSION_NAME }}
5456
secrets: |
55-
"github_token=${{ secrets.GITHUB_TOKEN }}"
57+
github_token=${{ secrets.GITHUB_TOKEN }}

Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
FROM golang:1.23-alpine3.20 AS builder
33

44
ARG TARGETARCH
5-
ARG GITHUB_REF_NAME
5+
ARG VERSION_NAME
66

77
WORKDIR /app
88

@@ -20,8 +20,8 @@ RUN --mount=type=secret,id=github_token,env=GH_TOKEN \
2020
else \
2121
echo push assets to GitHub release \
2222
&& eval $(./bring version) \
23-
&& echo "?" "$BRING_VERSION" "==" "$GITHUB_REF_NAME" \
24-
&& [ "$BRING_VERSION" = "$GITHUB_REF_NAME" ] \
23+
&& echo "?" "$BRING_VERSION" "==" "$VERSION_NAME" \
24+
&& [ "$BRING_VERSION" = "$VERSION_NAME" ] \
2525
&& apk add --no-cache \
2626
curl \
2727
&& curl -sSL "https://github.com/cli/cli/releases/download/v2.62.0/gh_2.62.0_linux_$TARGETARCH.tar.gz" -o ./gh.tar.gz \
@@ -32,7 +32,7 @@ RUN --mount=type=secret,id=github_token,env=GH_TOKEN \
3232
&& ./gh version \
3333
&& ./gh auth status \
3434
&& ./gh release --repo lesomnus/bring \
35-
upload "$GITHUB_REF_NAME" "$BRING_NAME" \
35+
upload "$VERSION_NAME" "$BRING_NAME" \
3636
--clobber \
3737
&& rm -rf \
3838
./gh.tar.gz \

cmd/gen/version/main.go

Lines changed: 0 additions & 61 deletions
This file was deleted.

cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type buildInfo struct {
1515
GitDirty bool
1616
}
1717

18-
//go:generate go run ./gen/version
18+
//go:generate bash -c "../scripts/gen-version-file.sh > /dev/null"
1919
var _buildInfo = buildInfo{
2020
Version: "v0.0.0-edge",
2121
TimeBuild: time.Now().Format(time.RFC3339),

scripts/docker-build.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Directory where this scr
99
__root="$(cd "$(dirname "${__dir}")" && pwd)" # Root directory of project.
1010

1111
export GITHUB_TOKEN=""
12-
GITHUB_REF_NAME=${BRING_VERSION:-"v0.0.0-test"}
12+
VERSION_NAME=${BRING_VERSION:-"v0.0.0-test"}
1313

1414
cd "$__root"
1515

1616
docker buildx build \
1717
--no-cache \
1818
--progress=plain \
1919
--secret id=github_token,env=GITHUB_TOKEN \
20-
--build-arg GITHUB_REF_NAME="$GITHUB_REF_NAME" \
20+
--build-arg "VERSION_NAME=$VERSION_NAME" \
2121
--tag bring:test \
2222
.

scripts/gen-version-file.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
set -o pipefail
5+
set -o nounset
6+
# set -o xtrace
7+
8+
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Directory where this script exists.
9+
__root="$(cd "$(dirname "${__dir}")" && pwd)" # Root directory of project.
10+
11+
BRING_VERSION=${BRING_VERSION:-"v0.0.0-test"}
12+
TIME_BUILD=$(date --rfc-3339=seconds)
13+
GIT_REV=$(git rev-parse HEAD)
14+
GIT_DIRTY=$(git status --porcelain | read -t 0 && echo "true" || echo "false")
15+
16+
cd "$__root"
17+
18+
(cat | tee ./cmd/version.g.go) <<EOL
19+
// Code generated by scripts/gen-version-file.sh; DO NOT EDIT.
20+
package cmd
21+
22+
func init(){
23+
b := &_buildInfo
24+
b.Version = "$BRING_VERSION"
25+
b.TimeBuild = "$TIME_BUILD"
26+
b.GitRev = "$GIT_REV"
27+
b.GitDirty = $GIT_DIRTY
28+
}
29+
EOL
30+

0 commit comments

Comments
 (0)