Compare commits
5 commits
52559a3072
...
339bb81ad1
Author | SHA1 | Date | |
---|---|---|---|
339bb81ad1 | |||
dd5135511b | |||
1efd61340f | |||
b1c886c1cf | |||
de293b5f37 |
6 changed files with 131 additions and 1 deletions
93
.forgejo/workflows/build-project.yml
Normal file
93
.forgejo/workflows/build-project.yml
Normal file
|
@ -0,0 +1,93 @@
|
|||
# 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
|
||||
|
||||
# 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}
|
||||
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
|
10
README.md
10
README.md
|
@ -1,3 +1,11 @@
|
|||
# workflow-sample-go
|
||||
|
||||
Demonstrates how to configure a typical Go project.
|
||||
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.
|
||||
|
|
15
cmd/sample/main.go
Normal file
15
cmd/sample/main.go
Normal file
|
@ -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()
|
||||
}
|
2
go.mod
Normal file
2
go.mod
Normal file
|
@ -0,0 +1,2 @@
|
|||
module repobase.net/j/workflow-sample-go
|
||||
go 1.22
|
5
internal/greet/greet.go
Normal file
5
internal/greet/greet.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package greet
|
||||
|
||||
func Hello(name string) string {
|
||||
return "Hello, " + name + "!"
|
||||
}
|
7
pkg/util/util.go
Normal file
7
pkg/util/util.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package util
|
||||
|
||||
import fmt
|
||||
|
||||
func PrintVersion() {
|
||||
fmt.Println("v4.2.0")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue