Skip to content

Commit 54922e3

Browse files
authored
feat: add semantic-release, swagger and publish to DockerHub (#10)
1 parent 8e32081 commit 54922e3

20 files changed

+725
-0
lines changed

.github/semantic.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Always validate the PR title AND all the commits
2+
titleAndCommits: true
3+
# Require at least one commit to be valid
4+
# this is only relevant when using commitsOnly: true or titleAndCommits: true,
5+
# which validate all commits by default
6+
anyCommit: true
7+
# Allow use of Merge commits (eg on github: "Merge branch 'master' into feature/ride-unicorns")
8+
# this is only relevant when using commitsOnly: true (or titleAndCommits: true)
9+
allowMergeCommits: false
10+
# Allow use of Revert commits (eg on github: "Revert "feat: ride unicorns"")
11+
# this is only relevant when using commitsOnly: true (or titleAndCommits: true)
12+
allowRevertCommits: false

.github/workflows/build.yml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: Build
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
backend:
7+
name: Back-end
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v3
11+
- uses: actions/setup-go@v4
12+
with:
13+
go-version: '1.23.6'
14+
cache-dependency-path: ./go.mod
15+
- run: go version
16+
- name: Build
17+
run: |
18+
go build -race -ldflags "-extldflags '-static'"
19+
working-directory: ./
20+
21+
linter:
22+
name: Go-Linter
23+
runs-on: ubuntu-latest
24+
needs: [ backend ]
25+
steps:
26+
- uses: actions/checkout@v3
27+
- uses: actions/setup-go@v4
28+
with:
29+
go-version: '1.23.6'
30+
cache: false
31+
32+
# gen a dummy config file
33+
- run: touch dummy.yml
34+
35+
- name: golangci-lint
36+
uses: golangci/golangci-lint-action@v3
37+
with:
38+
version: latest
39+
args: --disable-all -c dummy.yml -E=gofumpt --max-same-issues=0 --timeout 5m --modules-download-mode=mod
40+
41+
release-and-push:
42+
name: Release And Push
43+
runs-on: ubuntu-latest
44+
permissions:
45+
contents: write
46+
issues: write
47+
if: github.repository == 'casibase/chainserver' && github.event_name == 'push'
48+
needs: [ backend, linter ]
49+
steps:
50+
- name: Checkout
51+
uses: actions/checkout@v3
52+
with:
53+
fetch-depth: -1
54+
- name: Setup Node.js
55+
uses: actions/setup-node@v3
56+
with:
57+
node-version: 20
58+
59+
- name: Fetch Previous version
60+
id: get-previous-tag
61+
uses: actions-ecosystem/action-get-latest-tag@v1.6.0
62+
63+
- name: Release
64+
run: yarn global add semantic-release@17.4.4 && semantic-release
65+
env:
66+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
67+
68+
- name: Fetch Current version
69+
id: get-current-tag
70+
uses: actions-ecosystem/action-get-latest-tag@v1.6.0
71+
72+
- name: Decide Should_Push Or Not
73+
id: should_push
74+
run: |
75+
old_version=${{steps.get-previous-tag.outputs.tag}}
76+
new_version=${{steps.get-current-tag.outputs.tag }}
77+
78+
old_array=(${old_version//\./ })
79+
new_array=(${new_version//\./ })
80+
81+
if [ ${old_array[0]} != ${new_array[0]} ]
82+
then
83+
echo ::set-output name=push::'true'
84+
elif [ ${old_array[1]} != ${new_array[1]} ]
85+
then
86+
echo ::set-output name=push::'true'
87+
else
88+
echo ::set-output name=push::'false'
89+
fi
90+
91+
- name: Set up QEMU
92+
uses: docker/setup-qemu-action@v2
93+
94+
- name: Set up buildx
95+
id: buildx
96+
uses: docker/setup-buildx-action@v2
97+
with:
98+
version: latest
99+
100+
- name: Log in to Docker Hub
101+
uses: docker/login-action@v1
102+
if: github.repository == 'casibase/chainserver' && github.event_name == 'push' && steps.should_push.outputs.push=='true'
103+
with:
104+
username: ${{ secrets.DOCKERHUB_USERNAME }}
105+
password: ${{ secrets.DOCKERHUB_PASSWORD }}
106+
107+
- name: Push to Docker Hub
108+
uses: docker/build-push-action@v3
109+
if: github.repository == 'casibase/chainserver' && github.event_name == 'push' && steps.should_push.outputs.push=='true'
110+
with:
111+
context: .
112+
target: STANDARD
113+
platforms: linux/amd64
114+
push: true
115+
tags: casbin/chainserver:${{steps.get-current-tag.outputs.tag }},casbin/chainserver:latest

.releaserc.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"debug": true,
3+
"branches": [
4+
"+([0-9])?(.{+([0-9]),x}).x",
5+
"master",
6+
{
7+
"name": "rc"
8+
},
9+
{
10+
"name": "beta",
11+
"prerelease": true
12+
},
13+
{
14+
"name": "alpha",
15+
"prerelease": true
16+
}
17+
],
18+
"plugins": [
19+
"@semantic-release/commit-analyzer",
20+
"@semantic-release/release-notes-generator",
21+
"@semantic-release/github"
22+
]
23+
}

Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM golang:1.23.6 AS BACK
2+
WORKDIR /go/src/chainserver
3+
COPY . .
4+
RUN chmod +x ./build.sh
5+
RUN ./build.sh
6+
7+
FROM alpine:latest AS STANDARD
8+
LABEL MAINTAINER="https://casibase.org/"
9+
ARG USER=chainserver
10+
11+
RUN sed -i 's/https/http/' /etc/apk/repositories
12+
RUN apk add --update sudo
13+
RUN apk add curl
14+
RUN apk add ca-certificates && update-ca-certificates
15+
16+
RUN adduser -D $USER -u 1000 \
17+
&& echo "$USER ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/$USER \
18+
&& chmod 0440 /etc/sudoers.d/$USER \
19+
&& mkdir logs \
20+
&& chown -R $USER:$USER logs
21+
22+
USER 1000
23+
WORKDIR /
24+
COPY --from=BACK --chown=$USER:$USER /go/src/chainserver/server ./server
25+
26+
ENTRYPOINT ["/server"]

build.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
#try to connect to google to determine whether user need to use proxy
3+
curl www.google.com -o /dev/null --connect-timeout 5 2> /dev/null
4+
if [ $? == 0 ]
5+
then
6+
echo "Successfully connected to Google, no need to use Go proxy"
7+
else
8+
echo "Google is blocked, Go proxy is enabled: GOPROXY=https://goproxy.cn,direct"
9+
export GOPROXY="https://goproxy.cn,direct"
10+
fi
11+
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o server .

controllers/util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package controllers
1616

1717
import (
1818
"fmt"
19+
1920
"github.com/beego/beego/context"
2021
)
2122

docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.1'
2+
services:
3+
chainserver:
4+
restart: always
5+
build:
6+
context: ./
7+
dockerfile: Dockerfile
8+
target: STANDARD
9+
entrypoint: /bin/sh -c './server'
10+
ports:
11+
- "8000:8000"
12+
environment:
13+
RUNNING_IN_DOCKER: "true"

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func main() {
2727
beego.BConfig.CopyRequestBody = true
2828
beego.BConfig.RunMode = "dev"
2929

30+
beego.SetStaticPath("/swagger", "swagger")
3031
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
3132
AllowOrigins: []string{"*"},
3233
AllowMethods: []string{"GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS"},

swagger/favicon-16x16.png

665 Bytes
Loading

swagger/favicon-32x32.png

628 Bytes
Loading

0 commit comments

Comments
 (0)