From de293b5f376027b0a2af56fe5efcd08c45fd38c1 Mon Sep 17 00:00:00 2001 From: j Date: Tue, 17 Jun 2025 15:45:03 +1000 Subject: [PATCH 1/5] Sample go project --- README.md | 10 +++++++++- cmd/sample/main.go | 15 +++++++++++++++ go.mod | 2 ++ internal/greet/greet.go | 5 +++++ pkg/util/util.go | 7 +++++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 cmd/sample/main.go create mode 100644 go.mod create mode 100644 internal/greet/greet.go create mode 100644 pkg/util/util.go diff --git a/README.md b/README.md index 9752505..1f25be5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ # workflow-sample-go -Demonstrates how to configure a typical Go project. \ No newline at end of file +Demonstrates how to configure a typical Go project. + +Project layout based on the +[golang-standards/project-layout](https://github.com/golang-standards/project-layout) +project layout standard (that was a mouthful) as I find it to be the most +comfortable to work within. + +The build pipelines are made for this standard and are probably difficult to +adapt. diff --git a/cmd/sample/main.go b/cmd/sample/main.go new file mode 100644 index 0000000..f911cbc --- /dev/null +++ b/cmd/sample/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" + "repobase.net/j/internal/greet" + "repobase.net/j/pkg/util" +) + +func main() { + name := "World" + message := greet.Hello(name) + fmt.Println(message) + + util.PrintVersion() +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b230720 --- /dev/null +++ b/go.mod @@ -0,0 +1,2 @@ +module repobase.net/j/workflow-sample-go +go 1.22 diff --git a/internal/greet/greet.go b/internal/greet/greet.go new file mode 100644 index 0000000..08b50cb --- /dev/null +++ b/internal/greet/greet.go @@ -0,0 +1,5 @@ +package greet + +func Hello(name string) string { + return "Hello, " + name + "!" +} diff --git a/pkg/util/util.go b/pkg/util/util.go new file mode 100644 index 0000000..e4dac5f --- /dev/null +++ b/pkg/util/util.go @@ -0,0 +1,7 @@ +package util + +import fmt + +func PrintVersion() { + fmt.Println("v4.2.0") +} From b1c886c1cfcc2eee95062b601abfc09313ed3482 Mon Sep 17 00:00:00 2001 From: j Date: Tue, 17 Jun 2025 15:47:32 +1000 Subject: [PATCH 2/5] Sample build project adapted from wordpress --- .forgejo/workflows/build-project.yml | 90 ++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 .forgejo/workflows/build-project.yml diff --git a/.forgejo/workflows/build-project.yml b/.forgejo/workflows/build-project.yml new file mode 100644 index 0000000..a14bb80 --- /dev/null +++ b/.forgejo/workflows/build-project.yml @@ -0,0 +1,90 @@ +# This workflow will build a standard Go project structure into +# a binary and release it via the Forgejo Releases tab. +# +# The anticipated project structure references something like below, +# however I would expect you to change "go-project" to "src". +# +# . +# ├── cmd +# │   └── sample +# │   └── main.go +# ├── internal +# │   └── greet +# │   └── greet.go +# ├── pkg +# │   └── util +# │   └── util.go +# ├── go.mod +# └── README.md +# +# We expect go.mod to exist and be correct. + +name: Build & Release Go Project + +on: + push: + tags: + - 'v*' + +jobs: + build: + runs-on: docker + container: debian:12 + + steps: + + - name: Prepare Build Environment + run: | + apt update && apt -y install zip nodejs curl jq golang + + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Build All Binaries + run: | + # Create the build directory for a consistent place to store binaries + mkdir -p build + + # Iterate over the cmd directory to compile all binaries found there + ls ${SOURCE_DIR}/cmd | while read BINARY ; do + go build -v -o build/${BINARY} ./cmd/${BINARY} + done + + - name: Create a release if it doesn't exist + run: | + # Curl request that uses the repos token & Forgejo's built-in variables + # to create a new release based on the tag that you have pushed + # + # It doesn't care if the release exists or not. + curl -X POST "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases" \ + -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ + -H "Content-Type: application/json" \ + -d '{ + "tag_name": "'"${GITHUB_REF##*/}"'", + "target_commitish": "main", + "name": "'"${GITHUB_REF##*/}"'", + "body": "Auto release", + "draft": false, + "prerelease": false + }' || true + + - name: Upload Binaries To Release Tab + run: | + # Set some variables because it's cleaner than in-line + TAG_NAME="${GITHUB_REF##*/}" + REPO_NAME="$(basename $GITHUB_REPOSITORY)" + + # Get the release ID created in the last step + RELEASE_ID=$(curl -s \ + -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ + "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/tags/${TAG_NAME}" \ + | jq -r '.id') + + # We're going to want to push each binary, so we'll iterate over them and + # push them one-by-one. + ls build | while read BINARY ; do + curl -X POST "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/${RELEASE_ID}/assets?name=${BINARY}" \ + -H "Authorization: token ${{ secrets.GITEA_TOKEN }}" \ + -H "Content-Type: application/zip" \ + --data-binary "@build/${BINARY}" + done From 1efd61340f895956cda9fd98a2a19c6f08b5cc2d Mon Sep 17 00:00:00 2001 From: j Date: Tue, 17 Jun 2025 15:49:35 +1000 Subject: [PATCH 3/5] Removed source_dir use --- .forgejo/workflows/build-project.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.forgejo/workflows/build-project.yml b/.forgejo/workflows/build-project.yml index a14bb80..536c913 100644 --- a/.forgejo/workflows/build-project.yml +++ b/.forgejo/workflows/build-project.yml @@ -46,7 +46,7 @@ jobs: mkdir -p build # Iterate over the cmd directory to compile all binaries found there - ls ${SOURCE_DIR}/cmd | while read BINARY ; do + ls ./cmd | while read BINARY ; do go build -v -o build/${BINARY} ./cmd/${BINARY} done From dd5135511b2787b4c46d1c46283bf0b3796e0ee9 Mon Sep 17 00:00:00 2001 From: j Date: Tue, 17 Jun 2025 15:52:24 +1000 Subject: [PATCH 4/5] Remove tag for now --- .forgejo/workflows/build-project.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.forgejo/workflows/build-project.yml b/.forgejo/workflows/build-project.yml index 536c913..9df2106 100644 --- a/.forgejo/workflows/build-project.yml +++ b/.forgejo/workflows/build-project.yml @@ -23,8 +23,8 @@ name: Build & Release Go Project on: push: - tags: - - 'v*' +# tags: +# - 'v*' jobs: build: @@ -45,6 +45,9 @@ jobs: # Create the build directory for a consistent place to store binaries mkdir -p build + # Do a quick tidy + go mod tidy + # Iterate over the cmd directory to compile all binaries found there ls ./cmd | while read BINARY ; do go build -v -o build/${BINARY} ./cmd/${BINARY} From 339bb81ad1bc0135b63e4812455dd69dc5f4eef5 Mon Sep 17 00:00:00 2001 From: j Date: Tue, 17 Jun 2025 15:52:31 +1000 Subject: [PATCH 5/5] wait no i need tag --- .forgejo/workflows/build-project.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.forgejo/workflows/build-project.yml b/.forgejo/workflows/build-project.yml index 9df2106..fe8649b 100644 --- a/.forgejo/workflows/build-project.yml +++ b/.forgejo/workflows/build-project.yml @@ -23,8 +23,8 @@ name: Build & Release Go Project on: push: -# tags: -# - 'v*' + tags: + - 'v*' jobs: build: