Skip to content

Commit 6b6e31a

Browse files
GenesisANGenesisAN
authored andcommitted
CICD更新,增加注释
1 parent 2d6d1ef commit 6b6e31a

File tree

5 files changed

+192
-10
lines changed

5 files changed

+192
-10
lines changed

.env

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 服务端口
2-
ADDRESS=":5050"
2+
ADDRESS=":5544"
33
# 工作目录,用于存放上传和下载的文件
4-
WORK_PATH="C:/FileServer"
4+
WORK_PATH="D:/"
55
# 是否启用HTTPS
66
HTTPS="false"
77
# HTTPS证书路径
@@ -13,7 +13,7 @@ AUTH_CONFIG_FILE="./auth.yaml"
1313
# Donwload Relative Path
1414
# if DOWNLOAD_RELATIVE_PATH is "/download", then
1515
# URL(NO HTTPS) is: http://IP:ADDRESS/download/xxx
16-
DOWNLOAD_RELATIVE_PATH="/download"
16+
DOWNLOAD_RELATIVE_PATH="/d"
1717

1818
# Upload Relative Path
1919
# if UPLOAD_RELATIVE_PATH is "/upload", then
@@ -23,5 +23,5 @@ DOWNLOAD_RELATIVE_PATH="/download"
2323
# to: Path to save
2424
#
2525
# if to param is "/test/" than file will be save to WORK_PATH/test/filename
26-
UPLOAD_RELATIVE_PATH="/uplod"
26+
UPLOAD_RELATIVE_PATH="/u"
2727

.github/workflows/build.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
tags:
7+
- 'v*'
8+
paths-ignore:
9+
- '**/*.md' # 忽略所有.md文件的更改
10+
jobs:
11+
build: # 构建
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Set up Go
15+
uses: actions/setup-go@v4
16+
with:
17+
go-version: '1.20'
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
- name: Grant execute permission for build.sh
23+
run: chmod +x ./build.sh
24+
- name: Build
25+
run: ./build.sh
26+
- name: Upload build GoFileService-linux-amd64
27+
uses: actions/upload-artifact@v3
28+
with:
29+
name: GoFileService-linux-amd64.tar.gz
30+
path: output/GoFileService-linux-amd64.tar.gz
31+
- name: Upload build GoFileService-linux-arm
32+
uses: actions/upload-artifact@v3
33+
with:
34+
name: GoFileService-linux-arm.tar.gz
35+
path: output/GoFileService-linux-arm.tar.gz
36+
- name: Upload build GoFileService-windows-amd64
37+
uses: actions/upload-artifact@v3
38+
with:
39+
name: GoFileService-windows-amd64.tar.gz
40+
path: output/GoFileService-windows-amd64.tar.gz
41+
release: # 发布
42+
needs: build
43+
# 只有在创建tag时才会触发
44+
if: startsWith(github.ref, 'refs/tags/')
45+
runs-on: ubuntu-latest
46+
steps:
47+
- name: Download GoFileService-linux-amd64 artifact # 下载构建好的GoFileService-linux-amd64
48+
uses: actions/download-artifact@v3
49+
with:
50+
name: GoFileService-linux-amd64.tar.gz
51+
path: output/
52+
53+
- name: Download GoFileService-linux-arm artifact # 下载构建好的GoFileService-linux-arm
54+
uses: actions/download-artifact@v3
55+
with:
56+
name: GoFileService-linux-arm.tar.gz
57+
path: output/
58+
59+
- name: Download GoFileService-windows-amd64 artifact # 下载构建好的GoFileService-windows-amd64
60+
uses: actions/download-artifact@v3
61+
with:
62+
name: GoFileService-windows-amd64.tar.gz
63+
path: output/
64+
65+
- uses: "marvinpinto/action-automatic-releases@latest" # 发布到GitHub Release
66+
with:
67+
repo_token: "${{ secrets.GITHUB_TOKEN }}"
68+
prerelease: true
69+
files: |
70+
output/*.tar.gz

README.md

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,42 @@ scp和sftp可能是更好选择
1818

1919
### 编译流程
2020

21-
````
21+
```sh
2222
go mod tidy
23-
2423
go build .
25-
````
24+
# 或者直接运行build.sh脚本
25+
```
2626

27+
### 使用方法
28+
29+
```yaml
30+
# 发起下载请求
31+
# 如果WORK_PATH是D:/, 文件路径是test/1.txt
32+
# 那么就可以下载 D:/test/1.txt
33+
GET:
34+
- 请求URL: http://IP:ADDRESS/DOWNLOAD_RELATIVE_PATH/文件路径
35+
- HEAD:
36+
- Authorization: 身份验证代码
37+
- Origin: 请求来源地址
38+
39+
# 发起上传请求
40+
# 如果WORK_PATH是D:/, 上传路径是test/,文件名称是1.txt
41+
# 那么就可以把文件上传到 D:/test/1.txt
42+
POST:
43+
- 请求URL: http://IP:ADDRESS/UPLOAD_RELATIVE_PATH
44+
- HEAD:
45+
- Authorization: 身份验证代码
46+
- Origin: 请求来源地址
47+
- BODY: # form-data格式
48+
- file: 要上传的文件
49+
- to: 上传路径
50+
51+
# ADDRESS,DOWNLOAD_RELATIVE_PATH,UPLOAD_RELATIVE_PATH在.env文件中配置
52+
# Authorization和Origin需要填的值在auth.yaml文件中配置
53+
```
2754
### .env配置文件说明
2855

29-
```sh
56+
```dotenv
3057
# 开启服务的端口
3158
ADDRESS=":5050"
3259
# 工作目录,用于存放上传和下载的文件
@@ -59,7 +86,7 @@ UPLOAD_RELATIVE_PATH="/upload"
5986

6087
### auth.yaml配置文件说明
6188

62-
```
89+
```yaml
6390
# 授权可访问的Origin,如果地址在这里配置了
6491
# 那么就不会使用AuthorizationHeader进行身份验证,而直接允许访问
6592
AuthorizedIPs:

build.sh

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/bash
2+
3+
REPO_NAME=$(basename $(git rev-parse --show-toplevel))
4+
5+
# 获取最新提交的哈希值
6+
LATEST_COMMIT_HASH=$(git rev-parse HEAD)
7+
8+
# 获取最新提交的标签
9+
LATEST_COMMIT_TAG=$(git tag --contains $LATEST_COMMIT_HASH)
10+
11+
# 获取最近的v开头的标签
12+
LATEST_V_TAG=$(git describe --tags --abbrev=0 `git rev-list --tags --max-count=1` 2>/dev/null)
13+
14+
if [ -z "$LATEST_COMMIT_TAG" ]; then
15+
# 当最新的提交没有tag时
16+
if [[ "$LATEST_V_TAG" == v* ]]; then
17+
# 如果最近的标签以v开头,则取最近的v开头的tag然后在Tag后面加上-dev-最新的提交的短哈希
18+
VERSION="$LATEST_V_TAG-dev-$(git rev-parse --short $LATEST_COMMIT_HASH)"
19+
else
20+
# 如果没有找到v开头的标签,设置默认值
21+
VERSION="1.0.0"
22+
fi
23+
else
24+
# 当最新的提交存在tag时
25+
if [[ "$LATEST_COMMIT_TAG" == v* ]]; then
26+
# 并且v开头时,则取Tag的内容为version
27+
VERSION=$LATEST_COMMIT_TAG
28+
else
29+
# 如果标签不是以v开头,设置默认值
30+
VERSION="1.0.0"
31+
fi
32+
fi
33+
34+
35+
# 其他变量
36+
GIT_HASH=$(git rev-parse HEAD)
37+
BUILD_DATE=$(date +%Y-%m-%dT%H:%M:%S)
38+
LD_FLAGS="-X main.version=$VERSION -X main.buildDate=$BUILD_DATE -X main.gitHash=$GIT_HASH"
39+
40+
echo "REPO_NAME=$REPO_NAME"
41+
echo "VERSION=$VERSION"
42+
echo "BUILD_DATE=$BUILD_DATE"
43+
echo "GIT_HASH=$GIT_HASH"
44+
45+
echo "Building $REPO_NAME-linux-arm"
46+
47+
go mod tidy
48+
49+
# 编译 ARM、x64 的 Linux 版本
50+
GOOS=linux GOARCH=arm go build -ldflags "$LD_FLAGS" -o "output/$REPO_NAME-linux-arm" ./...
51+
52+
echo "Building $REPO_NAME-linux-amd64"
53+
GOOS=linux GOARCH=amd64 go build -ldflags "$LD_FLAGS" -o "output/$REPO_NAME-linux-amd64" ./...
54+
55+
echo "Building $REPO_NAME-windows-amd64.exe"
56+
# 编译 x64 的 Windows 版本并附加 .exe 扩展名
57+
GOOS=windows GOARCH=amd64 go build -ldflags "$LD_FLAGS" -o "output/$REPO_NAME-windows-amd64.exe" ./...
58+
59+
cd "output"
60+
61+
# 打包输出文件并携带 .env 和 auth.yaml 文件
62+
echo "Packaging $REPO_NAME-linux-arm.tar.gz"
63+
tar -czf "$REPO_NAME-linux-arm.tar.gz" "$REPO_NAME-linux-arm" ../.env ../auth.yaml
64+
65+
echo "Packaging $REPO_NAME-linux-amd64.tar.gz"
66+
tar -czf "$REPO_NAME-linux-amd64.tar.gz" "$REPO_NAME-linux-amd64" ../.env ../auth.yaml
67+
68+
echo "Packaging $REPO_NAME-windows-amd64.tar.gz"
69+
tar -czf "$REPO_NAME-windows-amd64.tar.gz" "$REPO_NAME-windows-amd64.exe" ../.env ../auth.yaml
70+
71+
rm "$REPO_NAME-linux-arm"
72+
rm "$REPO_NAME-linux-amd64"
73+
rm "$REPO_NAME-windows-amd64.exe"
74+
75+
echo "Packaging complete!"

main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@ import (
2020

2121
const mb = 1024 * 1024
2222

23-
var workPath string
23+
var (
24+
version string
25+
buildDate string
26+
gitHash string
27+
workPath string
28+
)
2429

2530
func main() {
31+
if len(os.Args) > 1 && os.Args[1] == "-version" {
32+
fmt.Printf("Version: %s\nBuild Date: %s\nGit Commit: %s\n", version, buildDate, gitHash)
33+
return
34+
}
35+
2636
// Load environment variables
2737
if err := loadEnv(); err != nil {
2838
log.Fatalf("Error loading .env file: %v", err)

0 commit comments

Comments
 (0)