mirror of
https://github.com/kolaente/Simple-Git-Deploy.git
synced 2023-09-04 21:52:08 +00:00
Enabled script trigger on post-request
This commit is contained in:
parent
0f448136c9
commit
7b7b40a5c1
7
form.htm
Normal file
7
form.htm
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<form action="http://localhost:8080/" method="post">
|
||||||
|
<input type="hidden" name="payload" value='{
|
||||||
|
"secret":"Baum",
|
||||||
|
"ref":"feature"
|
||||||
|
}'/>
|
||||||
|
<input type="submit"/>
|
||||||
|
</form>
|
|
@ -2,4 +2,6 @@ ssh_key=/home/konrad/Schreibtisch/life-wob/life-deploy
|
||||||
git_url=ssh://git@git.mowie.cc:9022/Websites/Life-Homepage.git
|
git_url=ssh://git@git.mowie.cc:9022/Websites/Life-Homepage.git
|
||||||
deploy_dir=/home/konrad/Schreibtisch/life-wob/html
|
deploy_dir=/home/konrad/Schreibtisch/life-wob/html
|
||||||
container_name=
|
container_name=
|
||||||
secret=
|
secret=Baum
|
||||||
|
interface=":8080"
|
||||||
|
branch_name=refs/heads/master
|
|
@ -10,13 +10,13 @@ while getopts ':k:g:d:c:' OPTION ; do
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "-----------------------------"
|
#echo "-----------------------------"
|
||||||
echo "Started at:"
|
#echo "Started at:"
|
||||||
date
|
#date
|
||||||
echo "Key: ${KEY}"
|
#echo "Key: ${KEY}"
|
||||||
echo "Git-Url: ${GIT_URL}"
|
#echo "Git-Url: ${GIT_URL}"
|
||||||
echo "Deploy-Dir: ${DEPLOY_DIR}"
|
#echo "Deploy-Dir: ${DEPLOY_DIR}"
|
||||||
echo "----------------------------"
|
#echo "----------------------------"
|
||||||
|
|
||||||
#Check for Key
|
#Check for Key
|
||||||
if [ -z "$KEY" ]
|
if [ -z "$KEY" ]
|
||||||
|
@ -54,6 +54,6 @@ if [ ! -z "$DCONTAINER_NAME" ]
|
||||||
docker restart $DCONTAINER_NAME
|
docker restart $DCONTAINER_NAME
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Finished at:"
|
#echo "Finished at:"
|
||||||
date
|
#date
|
||||||
echo "-----------------------------"
|
#echo "-----------------------------"
|
|
@ -2,37 +2,86 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
//"os/exec"
|
||||||
|
|
||||||
"github.com/go-ini/ini"
|
"github.com/go-ini/ini"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
|
//"encoding/json"
|
||||||
|
//"os/exec"
|
||||||
|
//"github.com/go-playground/form"
|
||||||
|
//"encoding/json"
|
||||||
|
"github.com/go-playground/form"
|
||||||
|
"io/ioutil"
|
||||||
|
"encoding/json"
|
||||||
|
"net/url"
|
||||||
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SSH_Key string
|
SSH_Key string
|
||||||
Git_url string
|
Git_url string
|
||||||
Deploy_dir string
|
Deploy_dir string
|
||||||
Container_name string
|
Container_name string
|
||||||
Secret string
|
Secret string
|
||||||
|
Interface string
|
||||||
|
}
|
||||||
|
|
||||||
|
func config(key string) string {
|
||||||
|
cfg, _ := ini.Load("config.ini")
|
||||||
|
return cfg.Section("").Key(key).String()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Payload struct {
|
||||||
|
Secret string `json:"secret"`
|
||||||
|
Ref string `json:"ref"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//Init Config
|
|
||||||
cfg, _ := ini.Load("config.ini")
|
if config("git_url") != "" && config("deploy_dir") != "" && config("ssh_key") != "" {
|
||||||
var Conf Config
|
|
||||||
Conf.SSH_Key = cfg.Section("").Key("ssh_key").String()
|
|
||||||
Conf.Deploy_dir = cfg.Section("").Key("deploy_dir").String()
|
|
||||||
Conf.Git_url = cfg.Section("").Key("git_url").String()
|
|
||||||
Conf.Container_name = cfg.Section("").Key("container_name").String()
|
|
||||||
Conf.Secret = cfg.Section("").Key("secret").String()
|
|
||||||
|
|
||||||
if Conf.Git_url != "" && Conf.Deploy_dir != "" && Conf.SSH_Key != "" {
|
// Server
|
||||||
cmd, err := exec.Command("/bin/bash", "deploy.sh", "-k", Conf.SSH_Key, "-g", Conf.Git_url, "-d", Conf.Deploy_dir, "-c", Conf.Container_name).Output()
|
http.HandleFunc("/", handleHook)
|
||||||
|
err := http.ListenAndServe(config("interface"), nil) // setting listening port
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal("ListenAndServe: ", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("%s\n", cmd)
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("You must provide at least a Git-Url, Deploy-Dir and SSH-Key!")
|
fmt.Println("You must provide at least a Git-Url, Deploy-Dir and SSH-Key!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var decoder *form.Decoder
|
||||||
|
|
||||||
|
func handleHook(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == "POST" {
|
||||||
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error reading response: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
encoded, _ := url.QueryUnescape(string(body[8:]))
|
||||||
|
//log.Println(encoded)
|
||||||
|
|
||||||
|
var hook_data Payload
|
||||||
|
err = json.Unmarshal([]byte(encoded), &hook_data)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error decoding response: %v", err)
|
||||||
|
}
|
||||||
|
log.Println("Recived payload, Secret:", hook_data.Secret, ", Ref:", hook_data.Ref)
|
||||||
|
|
||||||
|
if hook_data.Secret == config("secret") {
|
||||||
|
if hook_data.Ref == config("branch_name") || hook_data.Ref == "refs/heads/" + config("secret") {
|
||||||
|
log.Println("Recived corresponding secret: ", hook_data.Secret)
|
||||||
|
log.Println("Starting update...")
|
||||||
|
cmd, err := exec.Command("/bin/bash", "deploy.sh", "-k", config("ssh_key"), "-g", config("git_url"), "-d", config("deploy_dir"), "-c", config("container_name")).Output()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Printf("%s\n", cmd)
|
||||||
|
log.Println("Finished update.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
1
src/github.com/go-playground/form
Submodule
1
src/github.com/go-playground/form
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 20d04065dbc6f79aac03bbfd49b6a9e0658f5842
|
Loading…
Reference in New Issue
Block a user