cleanup: update docs, minor code cleanup

This commit is contained in:
AJ ONeal 2020-10-20 23:54:33 -06:00
parent 7186eceb46
commit 3fbb2edf7f
3 changed files with 65 additions and 18 deletions

View File

@ -22,7 +22,32 @@ don't want to use it.
## Usage ## Usage
```bash ```bash
gitdeploy run --listen :3000 --serve-path ./public_overrides --exec ./scripts/ gitdeploy init
gitdeploy run --listen :3000 --scripts ./scripts/
```
```txt
Usage of gitdeploy run:
-listen string
the address and port on which to listen (default :4483)
-github-secret string
secret for github webhooks (same as GITHUB_SECRET=)
-bitbucket-secret string
secret for bitbucket webhooks (same as BITBUCKET_SECRET=)
-gitea-secret string
secret for gitea webhooks (same as GITEA_SECRET=)
-scripts string
path to ./scripts/{deploy.sh,promote.sh,etc}
-trust-repos string
list of repos (ex: 'github.com/org/repo', or '*' for all) for which to run '.gitdeploy/deploy.sh'
-compress
enable compression for text,html,js,css,etc (default true)
-promotions string
a list of promotable branches in descending order (default 'production,staging,master')
-serve-path string
path to serve, falls back to built-in web app
-trust-proxy
trust X-Forwarded-For header
``` ```
## Install ## Install
@ -66,6 +91,21 @@ as an incoming webhook, it runs it.
The example deploy scripts are a good start, but you'll probably The example deploy scripts are a good start, but you'll probably
need to update them to suit your build process for your project. need to update them to suit your build process for your project.
### In-repo .gitdeploy scripts
A repo my have its own `.gitdeploy/deploy.sh` at its root, but by default these are ignored.
You can set `--trust-repos` (or `TRUST_REPOS`) to allow deploy scripts to be run directly
from a repository.
```bash
# trust a few repos to run their own deploy scripts
gitdeploy run --listen :3000 --trust-repos 'github.com/org/one,github.com/org/two'
# trust all repos
gitdeploy run --listen :3000 --trust-repos '*'
```
### Git Info ### Git Info
These ENVs are set before each script is run: These ENVs are set before each script is run:

View File

@ -7,12 +7,12 @@ import (
var Server *ServerConfig var Server *ServerConfig
type ServerConfig struct { type ServerConfig struct {
Addr string Addr string
TrustProxy bool TrustProxy bool
RepoList string RepoList string
Compress bool Compress bool
ServePath string ServePath string
Exec string ScriptsPath string
} }
var ServerFlags *flag.FlagSet var ServerFlags *flag.FlagSet
@ -22,4 +22,5 @@ var DefaultMaxBodySize int64 = 1024 * 1024
func init() { func init() {
Server = &ServerConfig{} Server = &ServerConfig{}
ServerFlags = flag.NewFlagSet("run", flag.ExitOnError) ServerFlags = flag.NewFlagSet("run", flag.ExitOnError)
InitFlags = flag.NewFlagSet("init", flag.ExitOnError)
} }

28
main.go
View File

@ -62,12 +62,15 @@ var oldScripts string
func init() { func init() {
runOpts = options.Server runOpts = options.Server
runFlags = options.ServerFlags
initFlags = options.InitFlags initFlags = options.InitFlags
_ = initFlags.Bool("TODO", false, "init will eventually copy default assets into a local directory")
runFlags = options.ServerFlags
runFlags.StringVar(&runOpts.Addr, "listen", "", "the address and port on which to listen (default :4483)") runFlags.StringVar(&runOpts.Addr, "listen", "", "the address and port on which to listen (default :4483)")
runFlags.BoolVar(&runOpts.TrustProxy, "trust-proxy", false, "trust X-Forwarded-For header") runFlags.BoolVar(&runOpts.TrustProxy, "trust-proxy", false, "trust X-Forwarded-For header")
runFlags.StringVar(&runOpts.RepoList, "trust-repos", "", runFlags.StringVar(&runOpts.RepoList, "trust-repos", "",
"run '.gitdeploy/deploy.sh' directly from these repos if no local script is present (example: 'git.example.com/org/repo')") "list of repos (ex: 'github.com/org/repo', or '*' for all) for which to run '.gitdeploy/deploy.sh'")
runFlags.BoolVar(&runOpts.Compress, "compress", true, "enable compression for text,html,js,css,etc") runFlags.BoolVar(&runOpts.Compress, "compress", true, "enable compression for text,html,js,css,etc")
runFlags.StringVar( runFlags.StringVar(
&runOpts.ServePath, "serve-path", "", &runOpts.ServePath, "serve-path", "",
@ -76,7 +79,7 @@ func init() {
&oldScripts, "exec", "", &oldScripts, "exec", "",
"old alias for --scripts") "old alias for --scripts")
runFlags.StringVar( runFlags.StringVar(
&runOpts.Exec, "scripts", "", &runOpts.ScriptsPath, "scripts", "",
"path to ./scripts/{deploy.sh,promote.sh,etc}") "path to ./scripts/{deploy.sh,promote.sh,etc}")
//"path to bash script to run with git info as arguments") //"path to bash script to run with git info as arguments")
runFlags.StringVar(&promotionList, "promotions", "", runFlags.StringVar(&promotionList, "promotions", "",
@ -119,16 +122,19 @@ func main() {
return return
case "init": case "init":
_ = initFlags.Parse(args[2:]) _ = initFlags.Parse(args[2:])
fmt.Fprintf(os.Stderr, "%s init: not implemented\n", name)
os.Exit(0)
return
case "run": case "run":
_ = runFlags.Parse(args[2:]) _ = runFlags.Parse(args[2:])
if "" == runOpts.Exec { if "" == runOpts.ScriptsPath {
if "" != oldScripts { if "" != oldScripts {
fmt.Fprintf(os.Stderr, "--exec is deprecated and will be removed. Please use --scripts instead.\n") fmt.Fprintf(os.Stderr, "--exec is deprecated and will be removed. Please use --scripts instead.\n")
runOpts.Exec = oldScripts runOpts.ScriptsPath = oldScripts
} }
} }
if "" == runOpts.Exec { if "" == runOpts.ScriptsPath {
runOpts.Exec = "./scripts" runOpts.ScriptsPath = "./scripts"
pathname, _ := filepath.Abs("./scripts") pathname, _ := filepath.Abs("./scripts")
if info, _ := os.Stat("./scripts/deploy.sh"); nil == info || !info.Mode().IsRegular() { if info, _ := os.Stat("./scripts/deploy.sh"); nil == info || !info.Mode().IsRegular() {
fmt.Printf( fmt.Printf(
@ -265,7 +271,7 @@ func serve() {
Promotions: promotions, Promotions: promotions,
}) })
} }
err := filepath.Walk(runOpts.Exec, func(path string, info os.FileInfo, err error) error { err := filepath.Walk(runOpts.ScriptsPath, func(path string, info os.FileInfo, err error) error {
if nil != err { if nil != err {
fmt.Printf("error walking %q: %v\n", path, err) fmt.Printf("error walking %q: %v\n", path, err)
return nil return nil
@ -276,7 +282,7 @@ func serve() {
return nil return nil
} }
path = strings.Join(parts[1:], "/") path = strings.Join(parts[1:], "/")
if info.Mode().IsRegular() && "deploy.sh" == info.Name() && runOpts.Exec != path { if info.Mode().IsRegular() && "deploy.sh" == info.Name() && runOpts.ScriptsPath != path {
id := filepath.Dir(path) id := filepath.Dir(path)
repos = append(repos, Repo{ repos = append(repos, Repo{
ID: id, ID: id,
@ -424,7 +430,7 @@ func runHook(hook webhooks.Ref) {
)) ))
args := []string{ args := []string{
runOpts.Exec + "/deploy.sh", runOpts.ScriptsPath + "/deploy.sh",
jobID, jobID,
hook.RefName, hook.RefName,
hook.RefType, hook.RefType,
@ -517,7 +523,7 @@ func runPromote(hook webhooks.Ref, promoteTo string) {
)) ))
args := []string{ args := []string{
runOpts.Exec + "/promote.sh", runOpts.ScriptsPath + "/promote.sh",
jobID1, jobID1,
promoteTo, promoteTo,
hook.RefName, hook.RefName,