Skip to content

Commit

Permalink
Added: custom variable
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenorzz committed May 14, 2024
1 parent 68b5d20 commit e2cf9c6
Show file tree
Hide file tree
Showing 25 changed files with 482 additions and 373 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tmp

goploy
goploy_arm64
goploy_arm64.mac
goploy.exe*
goploy.mac
goploy.pid
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ All notable changes to the "goploy" extension will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

## [1.17.0] - 2024-05-14

### Added
- [custom variable](https://github.com/zhenorzz/goploy/issues/101)
- custom login lock time

### Changed

- change deploy dialog ui

### Bug fixed
- [fix quote in rsync remote shell](https://github.com/zhenorzz/goploy/pull/107)


## [1.16.3] - 2024-03-29

### Bug fixed
Expand Down
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ env GOOS=linux go build -o goploy cmd/server/main.go

env GOOS=linux GOARCH=arm64 go build -o goploy_arm64 cmd/server/main.go

env GOOS=darwin GOARCH=arm64 go build -o goploy_arm64.mac cmd/server/main.go

env GOOS=darwin go build -o goploy.mac cmd/server/main.go

env GOOS=windows go build -o goploy.exe cmd/server/main.go
Expand Down
106 changes: 46 additions & 60 deletions cmd/server/api/deploy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,11 @@ func (Deploy) ManageProcess(gp *server.Goploy) server.Response {
// @Router /deploy/publish [post]
func (Deploy) Publish(gp *server.Goploy) server.Response {
type ReqData struct {
ProjectID int64 `json:"projectId" validate:"required,gt=0"`
Commit string `json:"commit"`
Branch string `json:"branch"`
ProjectID int64 `json:"projectId" validate:"required,gt=0"`
Commit string `json:"commit"`
Branch string `json:"branch"`
ServerIDs []int64 `json:"serverIds"`
CustomVariables []model.ProjectScriptCustomVariable `json:"customVariables"`
}
var reqData ReqData
if err := gp.Decode(&reqData); err != nil {
Expand All @@ -554,11 +556,11 @@ func (Deploy) Publish(gp *server.Goploy) server.Response {

token := ""
if project.DeployState == model.ProjectNotDeploy {
token, err = projectDeploy(gp, project, "", "")
token, err = projectDeploy(gp, project, "", "", reqData.ServerIDs, reqData.CustomVariables)
} else if project.Review == model.Enable {
err = projectReview(gp, project, reqData.Commit, reqData.Branch)
} else {
token, err = projectDeploy(gp, project, reqData.Commit, reqData.Branch)
token, err = projectDeploy(gp, project, reqData.Commit, reqData.Branch, reqData.ServerIDs, reqData.CustomVariables)
}
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
Expand Down Expand Up @@ -617,7 +619,17 @@ func (Deploy) Rebuild(gp *server.Goploy) server.Response {
break
}

if publishTrace.Type == model.Pull {
if publishTrace.Type == model.Queue {
var queueExt model.PublishTraceQueueExt
if publishTrace.Ext != "" {
err := json.Unmarshal([]byte(publishTrace.Ext), &queueExt)
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
// set the token script
project.Script = queueExt.Script
}
} else if publishTrace.Type == model.Pull {
err := json.Unmarshal([]byte(publishTrace.Ext), &commitInfo)
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
Expand Down Expand Up @@ -647,6 +659,7 @@ func (Deploy) Rebuild(gp *server.Goploy) server.Response {

if project.Script.AfterDeploy.Content != "" {
scriptContent := project.ReplaceVars(project.Script.AfterDeploy.Content)
scriptContent = project.ReplaceCustomVars(scriptContent)
scriptContent = projectServer.ReplaceVars(scriptContent)

if project.Script.AfterDeploy.Mode == "yaml" {
Expand Down Expand Up @@ -815,56 +828,8 @@ func (Deploy) Rebuild(gp *server.Goploy) server.Response {
// @Param request body deploy.GreyPublish.ReqData true "body params"
// @Success 200 {object} response.JSON
// @Router /deploy/greyPublish [post]
func (Deploy) GreyPublish(gp *server.Goploy) server.Response {
type ReqData struct {
ProjectID int64 `json:"projectId" validate:"required,gt=0"`
Commit string `json:"commit"`
Branch string `json:"branch"`
ServerIDs []int64 `json:"serverIds"`
}
var reqData ReqData
if err := gp.Decode(&reqData); err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
project, err := model.Project{ID: reqData.ProjectID}.GetData()

if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}

bindProjectServers, err := model.ProjectServer{ProjectID: project.ID}.GetBindServerListByProjectID()

if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}

projectServers := model.ProjectServers{}

for _, projectServer := range bindProjectServers {
for _, serverID := range reqData.ServerIDs {
if projectServer.ServerID == serverID {
projectServers = append(projectServers, projectServer)
}
}
}

project.PublisherID = gp.UserInfo.ID
project.PublisherName = gp.UserInfo.Name
project.DeployState = model.ProjectDeploying
project.LastPublishToken = uuid.New().String()
err = project.Publish()
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
task.AddDeployTask(task.Gsync{
UserInfo: gp.UserInfo,
Project: project,
ProjectServers: projectServers,
CommitID: reqData.Commit,
Branch: reqData.Branch,
})

return response.JSON{}
func (d Deploy) GreyPublish(gp *server.Goploy) server.Response {
return d.Publish(gp)
}

// Review reviews the project
Expand Down Expand Up @@ -906,7 +871,7 @@ func (Deploy) Review(gp *server.Goploy) server.Response {
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
if _, err := projectDeploy(gp, project, pr.CommitID, pr.Branch); err != nil {
if _, err := projectDeploy(gp, project, pr.CommitID, pr.Branch, nil, nil); err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
}
Expand Down Expand Up @@ -1005,7 +970,7 @@ func (Deploy) Callback(gp *server.Goploy) server.Response {
if err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}
if _, err := projectDeploy(gp, project, pr.CommitID, pr.Branch); err != nil {
if _, err := projectDeploy(gp, project, pr.CommitID, pr.Branch, nil, nil); err != nil {
return response.JSON{Code: response.Error, Message: err.Error()}
}

Expand All @@ -1016,11 +981,32 @@ func (Deploy) Callback(gp *server.Goploy) server.Response {
return response.JSON{}
}

func projectDeploy(gp *server.Goploy, project model.Project, commitID string, branch string) (string, error) {
projectServers, err := model.ProjectServer{ProjectID: project.ID}.GetBindServerListByProjectID()
func projectDeploy(gp *server.Goploy, project model.Project, commitID string, branch string, serverIDs []int64, customVariables []model.ProjectScriptCustomVariable) (string, error) {
bindProjectServers, err := model.ProjectServer{ProjectID: project.ID}.GetBindServerListByProjectID()

if err != nil {
return "", err
}

projectServers := bindProjectServers

if len(serverIDs) > 0 {
projectServers = model.ProjectServers{}
for _, projectServer := range bindProjectServers {
for _, serverID := range serverIDs {
if projectServer.ServerID == serverID {
projectServers = append(projectServers, projectServer)
}
}
}
}

if len(customVariables) > 0 {
project.Script.CustomVariables = customVariables
} else {
project.Script.CustomVariables = []model.ProjectScriptCustomVariable{}
}

project.PublisherID = gp.UserInfo.ID
project.PublisherName = gp.UserInfo.Name
project.DeployState = model.ProjectDeploying
Expand Down
4 changes: 2 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (
s string
)

const appVersion = "1.16.3"
const appVersion = "1.17.0"

func init() {
flag.StringVar(&config.AssetDir, "asset-dir", "", "default: ./")
Expand All @@ -64,7 +64,7 @@ func init() {
}

// @title Goploy
// @version 1.16.3
// @version 1.17.0
// @description A web deployment system tool!
// @contact.name zhenorzz
// @contact.url https://github.com/zhenorzz/goploy
Expand Down
15 changes: 12 additions & 3 deletions cmd/server/task/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,18 @@ func AddDeployTask(gsync Gsync) {
}{gsync.Project.LastPublishToken},
},
})

queueExt := model.PublishTraceQueueExt{
Script: gsync.Project.Script,
}
ext, _ := json.Marshal(queueExt)
gsync.PublishTrace = model.PublishTrace{
Token: gsync.Project.LastPublishToken,
ProjectID: gsync.Project.ID,
ProjectName: gsync.Project.Name,
PublisherID: gsync.UserInfo.ID,
PublisherName: gsync.UserInfo.Name,
Ext: string(ext),
InsertTime: time.Now().Format("20060102150405"),
Type: model.Queue,
State: model.Success,
Expand Down Expand Up @@ -208,9 +214,9 @@ func (gsync *Gsync) repoStage() error {
var err error
r, _ := repo.GetRepo(gsync.Project.RepoType)
if len(gsync.CommitID) == 0 {
err = r.Follow(gsync.Project, "origin/"+gsync.Project.Branch)
err = r.Follow(gsync.Project.ID, "origin/"+gsync.Project.Branch, gsync.Project.URL, gsync.Project.Branch)
} else {
err = r.Follow(gsync.Project, gsync.CommitID)
err = r.Follow(gsync.Project.ID, gsync.CommitID, gsync.Project.URL, gsync.Project.Branch)
}

if err != nil {
Expand Down Expand Up @@ -286,6 +292,7 @@ func (gsync *Gsync) serverStage() error {
scriptContent := ""
if project.Script.AfterDeploy.Content != "" {
scriptContent = project.ReplaceVars(project.Script.AfterDeploy.Content)
scriptContent = project.ReplaceCustomVars(scriptContent)
scriptContent = projectServer.ReplaceVars(scriptContent)
scriptContent = strings.Replace(scriptContent, "${SERVER_TOTAL_NUMBER}", strconv.Itoa(len(gsync.ProjectServers)), -1)
scriptContent = strings.Replace(scriptContent, "${SERVER_SERIAL_NUMBER}", strconv.Itoa(index), -1)
Expand Down Expand Up @@ -575,7 +582,9 @@ func (gsync *Gsync) runLocalScript() error {
if mode != "" {
scriptMode = mode
}
scriptText := project.ReplaceVars(commitInfo.ReplaceVars(content))
scriptText := commitInfo.ReplaceVars(content)
scriptText = project.ReplaceVars(scriptText)
scriptText = project.ReplaceCustomVars(scriptText)

// run yaml script by docker
if mode == "yaml" {
Expand Down
4 changes: 1 addition & 3 deletions database/goploy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ CREATE TABLE IF NOT EXISTS `terminal_log` (
INSERT IGNORE INTO `user`(`id`, `account`, `password`, `name`, `contact`, `state`, `super_manager`) VALUES (1, 'admin', '$2a$10$89ZJ2xeJj35GOw11Qiucr.phaEZP4.kBX6aKTs7oWFp1xcGBBgijm', '超管', '', 1, 1);
INSERT IGNORE INTO `namespace`(`id`, `name`) VALUES (1, 'goploy');
INSERT IGNORE INTO `namespace_user`(`id`, `namespace_id`, `user_id`, `role_id`) VALUES (1, 1, 1, 0);
INSERT IGNORE INTO `system_config` (`id`, `key`, `value`) VALUES (1, 'version', '1.16.3');
INSERT IGNORE INTO `system_config` (`id`, `key`, `value`) VALUES (1, 'version', '1.17.0');
INSERT IGNORE INTO `role`(`id`, `name`, `description`) VALUES (1, 'manager', '');
INSERT IGNORE INTO `role`(`id`, `name`, `description`) VALUES (2, 'member', '');
INSERT IGNORE INTO `permission`(`id`, `pid`, `name`, `sort`, `description`) VALUES (1, 0, 'Log', 0, '');
Expand Down Expand Up @@ -457,7 +457,6 @@ INSERT IGNORE INTO `permission`(`id`, `pid`, `name`, `sort`, `description`) VALU
INSERT IGNORE INTO `permission`(`id`, `pid`, `name`, `sort`, `description`) VALUES (60, 56, 'DeployDetail', 0, '');
INSERT IGNORE INTO `permission`(`id`, `pid`, `name`, `sort`, `description`) VALUES (61, 56, 'DeployProject', 0, '');
INSERT IGNORE INTO `permission`(`id`, `pid`, `name`, `sort`, `description`) VALUES (62, 56, 'DeployResetState', 0, '');
INSERT IGNORE INTO `permission`(`id`, `pid`, `name`, `sort`, `description`) VALUES (63, 56, 'GreyDeploy', 0, '');
INSERT IGNORE INTO `permission`(`id`, `pid`, `name`, `sort`, `description`) VALUES (64, 56, 'DeployRollback', 0, '');
INSERT IGNORE INTO `permission`(`id`, `pid`, `name`, `sort`, `description`) VALUES (65, 56, 'DeployReview', 0, '');
INSERT IGNORE INTO `permission`(`id`, `pid`, `name`, `sort`, `description`) VALUES (66, 56, 'DeployTask', 0, '');
Expand Down Expand Up @@ -525,7 +524,6 @@ INSERT IGNORE INTO `role_permission`(`role_id`, `permission_id`) VALUES (1, 59);
INSERT IGNORE INTO `role_permission`(`role_id`, `permission_id`) VALUES (1, 60);
INSERT IGNORE INTO `role_permission`(`role_id`, `permission_id`) VALUES (1, 61);
INSERT IGNORE INTO `role_permission`(`role_id`, `permission_id`) VALUES (1, 62);
INSERT IGNORE INTO `role_permission`(`role_id`, `permission_id`) VALUES (1, 63);
INSERT IGNORE INTO `role_permission`(`role_id`, `permission_id`) VALUES (1, 64);
INSERT IGNORE INTO `role_permission`(`role_id`, `permission_id`) VALUES (1, 65);
INSERT IGNORE INTO `role_permission`(`role_id`, `permission_id`) VALUES (1, 66);
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Import sql manually https://github.com/zhenorzz/goploy/blob/master/model/sql/goploy.sql
FROM alpine
LABEL maintainer="zhenorzz@gmail.com"
ARG GOPLOY_VER=v1.16.3
ARG GOPLOY_VER=v1.17.0
ENV GOPLOY_VER=${GOPLOY_VER}

RUN echo "https://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories
Expand Down
15 changes: 15 additions & 0 deletions internal/model/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ type ProjectScript struct {
Mode string `json:"mode"`
Content string `json:"content"`
} `json:"deployFinish"`
CustomVariables []ProjectScriptCustomVariable `json:"customVariables"`
}

type ProjectScriptCustomVariable struct {
Name string `json:"name"`
Value string `json:"value"`
Type string `json:"type"`
}

type Project struct {
Expand Down Expand Up @@ -588,3 +595,11 @@ func (p Project) ReplaceVars(script string) string {
}
return script
}

func (p Project) ReplaceCustomVars(script string) string {
for _, variable := range p.Script.CustomVariables {
key := fmt.Sprintf("${%s}", variable.Name)
script = strings.Replace(script, key, variable.Value, -1)
}
return script
}
4 changes: 4 additions & 0 deletions internal/model/publish_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type PublishTrace struct {
UpdateTime string `json:"updateTime"`
}

type PublishTraceQueueExt struct {
Script ProjectScript `json:"script"`
}

// PublishTraces -
type PublishTraces []PublishTrace

Expand Down
4 changes: 1 addition & 3 deletions internal/repo/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import (

type Repo interface {
Ping(url string) error
// Create one repository
Create(projectID int64) error
// Follow the repository code and update to latest
Follow(project model.Project, target string) error
Follow(projectID int64, target string, url string, branch string) error
// RemoteBranchList list remote branches in the url
RemoteBranchList(url string) ([]string, error)
// BranchList list the local repository's branches
Expand Down
16 changes: 2 additions & 14 deletions internal/repo/ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/jlaffaye/ftp"
log "github.com/sirupsen/logrus"
"github.com/zhenorzz/goploy/config"
"github.com/zhenorzz/goploy/internal/model"
"io"
"net/url"
"os"
Expand All @@ -34,26 +33,15 @@ func (ftpRepo FtpRepo) Ping(url string) error {
return nil
}

// Create -
func (ftpRepo FtpRepo) Create(projectID int64) error {
project, err := model.Project{ID: projectID}.GetData()
if err != nil {
log.Error(fmt.Sprintf("The project does not exist, projectID:%d", projectID))
return err
}
return ftpRepo.Follow(project, "")
}

func (ftpRepo FtpRepo) Follow(project model.Project, _ string) error {
projectID := project.ID
func (ftpRepo FtpRepo) Follow(projectID int64, _ string, projectURL string, _ string) error {
srcPath := config.GetProjectPath(projectID)
_ = os.RemoveAll(srcPath)
if err := os.MkdirAll(srcPath, 0755); err != nil {
log.Error(fmt.Sprintf("The project fail to mkdir, projectID:%d, error:%s", projectID, err.Error()))
return err
}

c, err := ftpRepo.dial(project.URL)
c, err := ftpRepo.dial(projectURL)
if err != nil {
log.Error(fmt.Sprintf("The project fail to connect ftp, projectID:%d, error:%s", projectID, err.Error()))
return err
Expand Down
Loading

0 comments on commit e2cf9c6

Please sign in to comment.