Browse Source

重命名环境变量

master
OhYee 1 year ago
parent
commit
52cf78b200
Signed by: OhYee
GPG Key ID: 5A9E1F63ED274FBB
  1. 32
      .ohyee/action.bash
  2. 123
      main.go

32
.ohyee/action.bash

@ -5,27 +5,29 @@ SHELL_FOLDER=$(cd "$(dirname "$0")";pwd) @@ -5,27 +5,29 @@ SHELL_FOLDER=$(cd "$(dirname "$0")";pwd)
LOG_FILE="${SHELL_FOLDER}/log.log"
# 支持的环境变量
# GITEA_ACTION_TOKEN WebHook 发来的密钥
# GITEA_ACTION_BRANCH 改动的分支(refs/heads/master)
# GITEA_ACTION_COMMIT 当前 commit id
# GITEA_ACTION_PUSHER 提交(执行push)人名称(OhYee)
# GITEA_ACTION_REPO 项目(OhYee/test)
# GITEA_ACTION_REPO_OWNER 项目所有者(OhYee)
# GITEA_ACTION_REPO_NAME 项目名称(test)
# GITEA_ACTION_REPO_GIT 项目 Git 地址(https://git.oyohyee.com/OhYee/code-questions.git)
# GITEA_ACTION_REPO_PAGE 项目页面(https://git.oyohyee.com/OhYee/code-questions)
# OHYEE_ACTION_TYPE 来源类型
# OHYEE_ACTION_TRIGGER 触发原因
# OHYEE_ACTION_TOKEN WebHook 发来的密钥
# OHYEE_ACTION_BRANCH 改动的分支(refs/heads/master)
# OHYEE_ACTION_COMMIT 当前 commit id
# OHYEE_ACTION_PUSHER 提交(执行push)人名称(OhYee)
# OHYEE_ACTION_REPO 项目(OhYee/test)
# OHYEE_ACTION_REPO_OWNER 项目所有者(OhYee)
# OHYEE_ACTION_REPO_NAME 项目名称(test)
# OHYEE_ACTION_REPO_GIT 项目 Git 地址(https://git.oyohyee.com/OhYee/code-questions.git)
# OHYEE_ACTION_REPO_PAGE 项目页面(https://git.oyohyee.com/OhYee/code-questions)
function func_log() {
echo $@ &>>$LOG_FILE
}
# 判断当前是脚本自己递归调用还是其他程序调用
if [[ -z $GITEA_ACTION_AFTER_PULL ]]; then
if [[ -z $OHYEE_ACTION_AFTER_PULL ]]; then
# 接收到请求,判断是否需要执行更新
func_log $(env)
func_log "Commit ${GITEA_ACTION_REPO_GIT}@${GITEA_ACTION_BRANCH} from ${GITEA_ACTION_PUSHER} $GITEA_ACTION_COMMIT $(date -u '+%Y-%m-%d %H:%M:%S')"
func_log "Commit ${OHYEE_ACTION_REPO_GIT}@${OHYEE_ACTION_BRANCH} from ${OHYEE_ACTION_PUSHER} $OHYEE_ACTION_COMMIT $(date -u '+%Y-%m-%d %H:%M:%S')"
func_log "Need update?"
if [[ ${GITEA_ACTION_BRANCH} == "refs/heads/master" ]]; then
if [[ ${OHYEE_ACTION_BRANCH} == "refs/heads/master" && ${OHYEE_ACTION_TYPE} == "gitea" ]]; then
func_log "Update files"
# 强制更新
@ -33,9 +35,9 @@ if [[ -z $GITEA_ACTION_AFTER_PULL ]]; then @@ -33,9 +35,9 @@ if [[ -z $GITEA_ACTION_AFTER_PULL ]]; then
git reset --hard origin/master
git pull
export GITEA_ACTION_AFTER_PULL="true"
export OHYEE_ACTION_AFTER_PULL="true"
bash $0
unset GITEA_ACTION_AFTER_PULL
unset OHYEE_ACTION_AFTER_PULL
else
func_log "No need"
fi
@ -45,7 +47,7 @@ else @@ -45,7 +47,7 @@ else
func_log "run action script"
# 重新编译,并关闭原有程序,在 screen 启动当前程序
SCREEN_NAME="gitea-action"
SCREEN_NAME="ohyee-action"
go build &>>$LOG_FILE
screen -S ${SCREEN_NAME} -X quit &>>$LOG_FILE
screen -dmS ${SCREEN_NAME} ./action -r "${SHELL_FOLDER}/../../../../" &>>$LOG_FILE

123
main.go

@ -3,6 +3,7 @@ package main @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
@ -24,27 +25,52 @@ type handle struct { @@ -24,27 +25,52 @@ type handle struct {
Root string
}
func (h handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m := new(WebHook)
b, err := ioutil.ReadAll(r.Body)
func readJson(r io.Reader, res interface{}) (err error) {
b, err := ioutil.ReadAll(r)
if err != nil {
log.Error.Println(errors.ShowStack(errors.NewErr(err)))
return
}
if err = json.Unmarshal(b, m); err != nil {
if err = json.Unmarshal(b, res); err != nil {
log.Error.Println(errors.ShowStack(errors.NewErr(err)))
return
}
return
}
func (h handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s, err := json.MarshalIndent(m, "", " ")
if err != nil {
log.Error.Println(errors.ShowStack(errors.NewErr(err)))
return
hook := new(Hook)
switch r.URL.Path {
case "/gitea":
m := new(GiteaHook)
readJson(r.Body, m)
hook = m.ToHook()
s, err := json.MarshalIndent(m, "", " ")
if err != nil {
log.Error.Println(errors.ShowStack(errors.NewErr(err)))
return
}
log.Debug.Printf("%s\n", s)
case "/hook":
readJson(r.Body, hook)
hook.Type = "hook"
s, err := json.MarshalIndent(hook, "", " ")
if err != nil {
log.Error.Println(errors.ShowStack(errors.NewErr(err)))
return
}
log.Debug.Printf("%s\n", s)
default:
w.WriteHeader(501)
w.Write([]byte(fmt.Sprintf("Can not solve request %s", r.RequestURI)))
}
log.Debug.Printf("%s\n", s)
m.handleGiteaWebHook(path.Join(h.Root, "repos", m.Repository.FullName))
go hook.handleGiteaWebHook(path.Join(h.Root, "repos", hook.RepoName))
}
var nocolor = color.New()
@ -52,6 +78,8 @@ var nocolor = color.New() @@ -52,6 +78,8 @@ var nocolor = color.New()
func main() {
pre := ""
root := file.AbsPath()
address := "0.0.0.0"
port := 42179
gToken := 0
for _, arg := range os.Args {
@ -66,6 +94,12 @@ func main() { @@ -66,6 +94,12 @@ func main() {
if l, err := strconv.ParseInt(arg, 10, 32); err == nil {
gToken = int(l)
}
case "-a":
address = arg
case "-p":
if l, err := strconv.ParseInt(arg, 10, 32); err == nil {
port = int(l)
}
default:
switch arg {
case "-t":
@ -90,7 +124,7 @@ func main() { @@ -90,7 +124,7 @@ func main() {
resetLog(log.Debug, f, "[Debug] ")
resetLog(log.Error, f, "[Error] ")
const addr = "0.0.0.0:9999"
addr := fmt.Sprintf("%s:%d", address, port)
fmt.Printf("Action works on %s\nFolder: %s\n", addr, root)
log.Info.Printf("Action works on %s\nFolder: %s\n", addr, root)
http.ListenAndServe(addr, handle{
@ -118,7 +152,7 @@ func run(dir string, env []string, command string, args ...string) (string, erro @@ -118,7 +152,7 @@ func run(dir string, env []string, command string, args ...string) (string, erro
return string(res), err
}
type WebHook struct {
type GiteaHook struct {
After string `json:"after"`
Before string `json:"before"`
Commits []struct {
@ -233,23 +267,64 @@ type WebHook struct { @@ -233,23 +267,64 @@ type WebHook struct {
} `json:"sender"`
}
func (hook *WebHook) handleGiteaWebHook(dirPath string) {
func (hook *GiteaHook) ToHook() *Hook {
return &Hook{
Type: "gitea",
Token: hook.Secret,
Commit: hook.After,
Branch: hook.Ref,
Pusher: hook.Pusher.Username,
Repo: hook.Repository.FullName,
RepoOwner: hook.Repository.Owner.Username,
RepoName: hook.Repository.Name,
RepoGit: hook.Repository.CloneURL,
RepoPage: hook.Repository.HTMLURL,
}
}
type Hook struct {
Type string `json:"type"`
Trigger string `json:"trigger"`
Token string `json:"token"`
Commit string `json:"commit"`
Branch string `json:"branch"`
Pusher string `json:"pusher"`
Repo string `json:"repo"`
RepoOwner string `json:"repo_owner"`
RepoName string `json:"repo_name"`
RepoGit string `json:"repo_git"`
RepoPage string `json:"page"`
Arg1 string `json:"arg1"`
Arg2 string `json:"arg2"`
Arg3 string `json:"arg3"`
Arg4 string `json:"arg4"`
Arg5 string `json:"arg5"`
}
func (hook *Hook) handleGiteaWebHook(dirPath string) {
log.Debug.Println(dirPath)
if file.IsExist(dirPath) {
// 该文件需要处理
screenName := strings.ReplaceAll(hook.Repository.FullName, "/", "-")
screenName := strings.ReplaceAll(hook.Repo, "/", "-")
// 环境变量
env := []string{
fmt.Sprintf("GITEA_ACTION_TOKEN=%s", hook.Secret),
fmt.Sprintf("GITEA_ACTION_BRANCH=%s", hook.Ref),
fmt.Sprintf("GITEA_ACTION_COMMIT=%s", hook.After),
fmt.Sprintf("GITEA_ACTION_PUSHER=%s", hook.Pusher.Username),
fmt.Sprintf("GITEA_ACTION_REPO=%s", hook.Repository.FullName),
fmt.Sprintf("GITEA_ACTION_REPO_OWNER=%s", hook.Repository.Owner.Username),
fmt.Sprintf("GITEA_ACTION_REPO_NAME=%s", hook.Repository.Name),
fmt.Sprintf("GITEA_ACTION_REPO_GIT=%s", hook.Repository.CloneURL),
fmt.Sprintf("GITEA_ACTION_REPO_PAGE=%s", hook.Repository.HTMLURL),
fmt.Sprintf("OHYEE_ACTION_TYPE=%s", hook.Type),
fmt.Sprintf("OHYEE_ACTION_TRIGGER=%s", hook.Trigger),
fmt.Sprintf("OHYEE_ACTION_TOKEN=%s", hook.Token),
fmt.Sprintf("OHYEE_ACTION_BRANCH=%s", hook.Branch),
fmt.Sprintf("OHYEE_ACTION_COMMIT=%s", hook.Commit),
fmt.Sprintf("OHYEE_ACTION_PUSHER=%s", hook.Pusher),
fmt.Sprintf("OHYEE_ACTION_REPO=%s", hook.Repo),
fmt.Sprintf("OHYEE_ACTION_REPO_OWNER=%s", hook.RepoOwner),
fmt.Sprintf("OHYEE_ACTION_REPO_NAME=%s", hook.RepoName),
fmt.Sprintf("OHYEE_ACTION_REPO_GIT=%s", hook.RepoGit),
fmt.Sprintf("OHYEE_ACTION_REPO_PAGE=%s", hook.RepoPage),
fmt.Sprintf("OHYEE_ACTION_ARG1=%s", hook.Arg1),
fmt.Sprintf("OHYEE_ACTION_ARG2=%s", hook.Arg2),
fmt.Sprintf("OHYEE_ACTION_ARG3=%s", hook.Arg3),
fmt.Sprintf("OHYEE_ACTION_ARG4=%s", hook.Arg4),
fmt.Sprintf("OHYEE_ACTION_ARG5=%s", hook.Arg5),
}
// 结束旧的的自动部署命令

Loading…
Cancel
Save