mirror of
https://gitea.com/action-examples/Go.git
synced 2026-03-27 16:30:08 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
741442ba9a | ||
|
|
bfe56a1c59 | ||
|
|
79e439dbc7 | ||
|
|
a4b8f12321 | ||
|
|
058d36bc64 | ||
|
|
f171daa81a | ||
|
|
9d1d40c08a | ||
|
|
0d1a3ae368 | ||
|
|
bf749b4823 | ||
|
|
082886c85b |
101
.gitea/workflows/docker.yml
Normal file
101
.gitea/workflows/docker.yml
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
name: Docker Image
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
tags:
|
||||||
|
- "v*"
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- "main"
|
||||||
|
|
||||||
|
env:
|
||||||
|
BUILDKIT_NO_CLIENT_TOKEN: 1
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-docker:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: catthehacker/ubuntu:act-20.04
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0 # all history for all branches and tags
|
||||||
|
|
||||||
|
- uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version-file: go.mod
|
||||||
|
check-latest: true
|
||||||
|
|
||||||
|
- uses: https://gitea.com/actions/go-hashfiles@v0.0.1
|
||||||
|
id: hash-go
|
||||||
|
with:
|
||||||
|
patterns: |
|
||||||
|
go.mod
|
||||||
|
go.sum
|
||||||
|
- name: cache go
|
||||||
|
id: cache-go
|
||||||
|
uses: https://github.com/actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
/go_path
|
||||||
|
/go_cache
|
||||||
|
key: go_path-${{ steps.hash-go.outputs.hash }
|
||||||
|
|
||||||
|
- name: Build binary
|
||||||
|
env:
|
||||||
|
TAGS: ""
|
||||||
|
run: |
|
||||||
|
make docker-build-linux-amd64
|
||||||
|
make docker-build-linux-arm64
|
||||||
|
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
|
||||||
|
- name: Setup docker context for buildx
|
||||||
|
id: buildx-context
|
||||||
|
run: docker context create builders || docker context use builders
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
with:
|
||||||
|
endpoint: builders
|
||||||
|
config-inline: |
|
||||||
|
debug = true
|
||||||
|
insecure-entitlements = [ "network.host", "security.insecure" ]
|
||||||
|
|
||||||
|
- name: Login to Docker Hub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Docker meta
|
||||||
|
id: docker-meta
|
||||||
|
uses: docker/metadata-action@v5
|
||||||
|
with:
|
||||||
|
images: |
|
||||||
|
gitea/example-go
|
||||||
|
tags: |
|
||||||
|
type=raw,value=latest,enable={{is_default_branch}}
|
||||||
|
type=semver,pattern={{version}}
|
||||||
|
type=semver,pattern={{major}}.{{minor}}
|
||||||
|
type=semver,pattern={{major}}
|
||||||
|
|
||||||
|
- name: Build and push
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
env:
|
||||||
|
ACTIONS_RUNTIME_TOKEN: ""
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
platforms: linux/amd64, linux/arm64
|
||||||
|
file: docker/Dockerfile
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.docker-meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.docker-meta.outputs.labels }}
|
||||||
|
provenance: false
|
||||||
|
sbom: false
|
||||||
|
cache-from: type=registry,ref=gitea/example-go:buildcache
|
||||||
|
cache-to: type=registry,ref=gitea/example-go:buildcache,mode=max
|
||||||
@@ -22,7 +22,8 @@ jobs:
|
|||||||
|
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version: "^1"
|
go-version-file: go.mod
|
||||||
|
check-latest: true
|
||||||
- uses: https://gitea.com/actions/go-hashfiles@v0.0.1
|
- uses: https://gitea.com/actions/go-hashfiles@v0.0.1
|
||||||
id: hash-go
|
id: hash-go
|
||||||
with:
|
with:
|
||||||
@@ -39,11 +40,11 @@ jobs:
|
|||||||
key: go_path-${{ steps.hash-go.outputs.hash }
|
key: go_path-${{ steps.hash-go.outputs.hash }
|
||||||
|
|
||||||
- name: Run GoReleaser
|
- name: Run GoReleaser
|
||||||
uses: goreleaser/goreleaser-action@v5
|
uses: goreleaser/goreleaser-action@v6
|
||||||
with:
|
with:
|
||||||
# either 'goreleaser' (default) or 'goreleaser-pro'
|
# either 'goreleaser' (default) or 'goreleaser-pro'
|
||||||
distribution: goreleaser
|
distribution: goreleaser
|
||||||
version: latest
|
version: "~> v2"
|
||||||
args: release --clean
|
args: release --clean
|
||||||
env:
|
env:
|
||||||
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ jobs:
|
|||||||
fetch-depth: 0 # all history for all branches and tags
|
fetch-depth: 0 # all history for all branches and tags
|
||||||
- uses: actions/setup-go@v5
|
- uses: actions/setup-go@v5
|
||||||
with:
|
with:
|
||||||
go-version-file: "go.mod"
|
go-version-file: go.mod
|
||||||
|
check-latest: true
|
||||||
- uses: https://gitea.com/actions/go-hashfiles@v0.0.1
|
- uses: https://gitea.com/actions/go-hashfiles@v0.0.1
|
||||||
id: hash-go
|
id: hash-go
|
||||||
with:
|
with:
|
||||||
@@ -33,7 +34,38 @@ jobs:
|
|||||||
/go_cache
|
/go_cache
|
||||||
key: go_path-${{ steps.hash-go.outputs.hash }}
|
key: go_path-${{ steps.hash-go.outputs.hash }}
|
||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v3
|
uses: golangci/golangci-lint-action@v6
|
||||||
with:
|
with:
|
||||||
version: v1.56.2
|
|
||||||
args: --timeout 5m
|
args: --timeout 5m
|
||||||
|
testing:
|
||||||
|
name: check and test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0 # all history for all branches and tags
|
||||||
|
|
||||||
|
- name: Setup go
|
||||||
|
uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version-file: go.mod
|
||||||
|
check-latest: true
|
||||||
|
- uses: https://gitea.com/actions/go-hashfiles@v0.0.1
|
||||||
|
id: hash-go
|
||||||
|
with:
|
||||||
|
patterns: |
|
||||||
|
go.mod
|
||||||
|
go.sum
|
||||||
|
- name: cache go
|
||||||
|
id: cache-go
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
/go_path
|
||||||
|
/go_cache
|
||||||
|
key: go_path-${{ steps.hash-go.outputs.hash }}
|
||||||
|
- name: build
|
||||||
|
run: make build
|
||||||
|
- name: test
|
||||||
|
run: make test
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -20,4 +20,5 @@
|
|||||||
|
|
||||||
# Go workspace file
|
# Go workspace file
|
||||||
go.work
|
go.work
|
||||||
|
release
|
||||||
|
bin
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
project_name: go
|
|
||||||
|
|
||||||
builds:
|
builds:
|
||||||
- # If true, skip the build.
|
- # If true, skip the build.
|
||||||
# Useful for library projects.
|
# Useful for library projects.
|
||||||
@@ -20,13 +18,15 @@ changelog:
|
|||||||
# - `github-native`: uses the GitHub release notes generation API, disables the groups feature.
|
# - `github-native`: uses the GitHub release notes generation API, disables the groups feature.
|
||||||
#
|
#
|
||||||
# Defaults to `git`.
|
# Defaults to `git`.
|
||||||
use: git
|
use: gitea
|
||||||
|
|
||||||
# Sorts the changelog by the commit's messages.
|
# Sorts the changelog by the commit's messages.
|
||||||
# Could either be asc, desc or empty
|
# Could either be asc, desc or empty
|
||||||
# Default is empty
|
# Default is empty
|
||||||
sort: asc
|
sort: asc
|
||||||
|
|
||||||
|
format: "{{ .SHA }} {{ .Message }} @{{ .AuthorUsername }} ({{ .AuthorName }})"
|
||||||
|
|
||||||
# Group commits messages by given regex and title.
|
# Group commits messages by given regex and title.
|
||||||
# Order value defines the order of the groups.
|
# Order value defines the order of the groups.
|
||||||
# Proving no regex means all commits will be grouped under the default group.
|
# Proving no regex means all commits will be grouped under the default group.
|
||||||
@@ -43,17 +43,16 @@ changelog:
|
|||||||
- title: "Enhancements"
|
- title: "Enhancements"
|
||||||
regexp: "^.*chore[(\\w)]*:+.*$"
|
regexp: "^.*chore[(\\w)]*:+.*$"
|
||||||
order: 2
|
order: 2
|
||||||
|
- title: "Refactor"
|
||||||
|
regexp: "^.*refactor[(\\w)]*:+.*$"
|
||||||
|
order: 3
|
||||||
|
- title: "Build process updates"
|
||||||
|
regexp: ^.*?(build|ci)(\(.+\))??!?:.+$
|
||||||
|
order: 4
|
||||||
|
- title: "Documentation updates"
|
||||||
|
regexp: ^.*?docs?(\(.+\))??!?:.+$
|
||||||
|
order: 4
|
||||||
- title: Others
|
- title: Others
|
||||||
order: 999
|
|
||||||
|
|
||||||
filters:
|
|
||||||
# Commit messages matching the regexp listed here will be removed from
|
|
||||||
# the changelog
|
|
||||||
# Default is empty
|
|
||||||
exclude:
|
|
||||||
- "^docs"
|
|
||||||
- "CICD"
|
|
||||||
- typo
|
|
||||||
|
|
||||||
gitea_urls:
|
gitea_urls:
|
||||||
api: https://gitea.com/api/v1
|
api: https://gitea.com/api/v1
|
||||||
|
|||||||
87
Makefile
Normal file
87
Makefile
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
EXECUTABLE ?= example-go
|
||||||
|
SOURCES ?= $(shell find . -name "*.go" -type f)
|
||||||
|
DIST := dist
|
||||||
|
DIST_DIRS := $(DIST)/binaries $(DIST)/release
|
||||||
|
GO ?= go
|
||||||
|
TAGS ?=
|
||||||
|
HAS_GO = $(shell hash $(GO) > /dev/null 2>&1 && echo "GO" || echo "NOGO" )
|
||||||
|
|
||||||
|
ifneq ($(shell uname), Darwin)
|
||||||
|
EXTLDFLAGS = -extldflags "-static" $(null)
|
||||||
|
else
|
||||||
|
EXTLDFLAGS =
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(HAS_GO), GO)
|
||||||
|
GOPATH ?= $(shell $(GO) env GOPATH)
|
||||||
|
export PATH := $(GOPATH)/bin:$(PATH)
|
||||||
|
|
||||||
|
CGO_EXTRA_CFLAGS := -DSQLITE_MAX_VARIABLE_NUMBER=32766
|
||||||
|
CGO_CFLAGS ?= $(shell $(GO) env CGO_CFLAGS) $(CGO_EXTRA_CFLAGS)
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(OS), Windows_NT)
|
||||||
|
GOFLAGS := -v -buildmode=exe
|
||||||
|
EXECUTABLE ?= $(EXECUTABLE).exe
|
||||||
|
else ifeq ($(OS), Windows)
|
||||||
|
GOFLAGS := -v -buildmode=exe
|
||||||
|
EXECUTABLE ?= $(EXECUTABLE).exe
|
||||||
|
else
|
||||||
|
GOFLAGS := -v
|
||||||
|
EXECUTABLE ?= $(EXECUTABLE)
|
||||||
|
endif
|
||||||
|
|
||||||
|
STORED_VERSION_FILE := VERSION
|
||||||
|
|
||||||
|
ifneq ($(DRONE_TAG),)
|
||||||
|
VERSION ?= $(subst v,,$(DRONE_TAG))
|
||||||
|
RELASE_VERSION ?= $(VERSION)
|
||||||
|
else
|
||||||
|
ifneq ($(DRONE_BRANCH),)
|
||||||
|
VERSION ?= $(subst release/v,,$(DRONE_BRANCH))
|
||||||
|
else
|
||||||
|
VERSION ?= main
|
||||||
|
endif
|
||||||
|
|
||||||
|
STORED_VERSION=$(shell cat $(STORED_VERSION_FILE) 2>/dev/null)
|
||||||
|
ifneq ($(STORED_VERSION),)
|
||||||
|
RELASE_VERSION ?= $(STORED_VERSION)
|
||||||
|
else
|
||||||
|
RELASE_VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
LDFLAGS ?= -X 'main.Version=$(RELASE_VERSION)'
|
||||||
|
|
||||||
|
build: $(EXECUTABLE)
|
||||||
|
|
||||||
|
$(EXECUTABLE): $(SOURCES)
|
||||||
|
$(GO) build -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o bin/$@ ./cmd/$(EXECUTABLE)
|
||||||
|
|
||||||
|
docker-build: docker-build-linux-amd64 docker-build-linux-arm64
|
||||||
|
|
||||||
|
docker-build-linux-amd64:
|
||||||
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o release/linux/amd64/$(EXECUTABLE) ./cmd/$(EXECUTABLE)
|
||||||
|
docker-build-linux-arm64:
|
||||||
|
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 $(GO) build -v -tags '$(TAGS)' -ldflags '$(EXTLDFLAGS)-s -w $(LDFLAGS)' -o release/linux/arm64/$(EXECUTABLE) ./cmd/$(EXECUTABLE)
|
||||||
|
|
||||||
|
test:
|
||||||
|
@$(GO) test -cover -tags '$(TAGS)' ./... && echo "\n==>\033[32m Ok\033[m\n" || exit 1
|
||||||
|
|
||||||
|
# install air command
|
||||||
|
.PHONY: air
|
||||||
|
air:
|
||||||
|
@hash air > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
|
||||||
|
$(GO) install github.com/cosmtrek/air@latest; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
# run air
|
||||||
|
.PHONY: dev
|
||||||
|
dev: air
|
||||||
|
air --build.cmd "make build" --build.bin bin/$(EXECUTABLE)
|
||||||
|
|
||||||
|
.PHONY: release-clean
|
||||||
|
release-clean:
|
||||||
|
rm -rf $(DIST)
|
||||||
|
|
||||||
|
clean: release-clean
|
||||||
21
docker/Dockerfile
Normal file
21
docker/Dockerfile
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
FROM alpine:3.19
|
||||||
|
|
||||||
|
ARG TARGETOS
|
||||||
|
ARG TARGETARCH
|
||||||
|
|
||||||
|
LABEL maintainer="Bo-Yi Wu <appleboy.tw@gmail.com>" \
|
||||||
|
org.label-schema.name="api" \
|
||||||
|
org.label-schema.vendor="Bo-Yi Wu" \
|
||||||
|
org.label-schema.schema-version="1.0" \
|
||||||
|
com.centurylinklabs.watchtower.stop-signal="SIGINT" \
|
||||||
|
io.containers.autoupdate="registry"
|
||||||
|
|
||||||
|
RUN apk update && apk add --no-cache ca-certificates
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
ENV GODEBUG netdns=go
|
||||||
|
|
||||||
|
COPY release/${TARGETOS}/${TARGETARCH}/example-go /bin/example-go
|
||||||
|
|
||||||
|
ENTRYPOINT ["/bin/example-go"]
|
||||||
Reference in New Issue
Block a user