From 3a03970b1125d0a5ecd0b204b20df94e087ccd54 Mon Sep 17 00:00:00 2001 From: Zobair Qauomi Date: Sat, 17 Oct 2020 17:12:56 +0200 Subject: [PATCH 1/7] Webhook support added to the sync project. - Webhook flag is int like in ANSI-C - it starts listening for requests on / --- main.go | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index fafa4ba..c58acda 100644 --- a/main.go +++ b/main.go @@ -16,12 +16,13 @@ limitations under the License. // git-sync is a command that pull a git repository to a local directory. -package main // import "k8s.io/contrib/git-sync" +package main import ( "flag" "fmt" "log" + "net/http" "os" "os/exec" "path" @@ -30,6 +31,7 @@ import ( "time" ) +var flWebhook = flag.Int("webhook", envInt("GIT_SYNC_WEBHOOK", 0), "Flag to whether start the webhook listener or not") var flRepo = flag.String("repo", envString("GIT_SYNC_REPO", ""), "git repo url") var flBranch = flag.String("branch", envString("GIT_SYNC_BRANCH", "master"), "git branch") var flRev = flag.String("rev", envString("GIT_SYNC_REV", "HEAD"), "git rev") @@ -57,6 +59,16 @@ func envInt(key string, def int) int { const usage = "usage: GIT_SYNC_REPO= GIT_SYNC_DEST= [GIT_SYNC_BRANCH= GIT_SYNC_WAIT=] git-sync -repo GIT_REPO_URL -dest PATH [-branch -wait]" +func syncRepoOnWebhook(w http.ResponseWriter, r *http.Request) { + var err error = syncRepo(*flRepo, *flDest, *flBranch, *flRev) + if err != nil { + fmt.Fprintf(w, err.Error()) + } +} +func handleRequest() { + http.HandleFunc("/", syncRepoOnWebhook) +} + func main() { flag.Parse() if *flRepo == "" || *flDest == "" { @@ -66,13 +78,19 @@ func main() { if _, err := exec.LookPath("git"); err != nil { log.Fatalf("required git executable not found: %v", err) } - for { - if err := syncRepo(*flRepo, *flDest, *flBranch, *flRev); err != nil { - log.Fatalf("error syncing repo: %v", err) + + if *flWebhook > 0 { + handleRequest() + } else { + + for { + if err := syncRepo(*flRepo, *flDest, *flBranch, *flRev); err != nil { + log.Fatalf("error syncing repo: %v", err) + } + log.Printf("wait %d seconds", *flWait) + time.Sleep(time.Duration(*flWait) * time.Second) + log.Println("done") } - log.Printf("wait %d seconds", *flWait) - time.Sleep(time.Duration(*flWait) * time.Second) - log.Println("done") } } @@ -120,4 +138,4 @@ func syncRepo(repo, dest, branch, rev string) error { } return nil -} \ No newline at end of file +} From 6d9cff05900fc0fe1dcb1e67d8a418f09ba590c7 Mon Sep 17 00:00:00 2001 From: Zobair Qauomi Date: Sat, 17 Oct 2020 17:38:04 +0200 Subject: [PATCH 2/7] The documentation is updated - I updated the documentation - I have corrected the errors in the compose example --- README.md | 88 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 79 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a073593..5be0576 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,36 @@ Forked from [kubernetes contrib] # Usage -## without docker compose + +- [Usage](#usage) + - [Without docker-compose using Webhook](#without-docker-compose-using-webhook) + - [Without docker-dompose](#without-docker-dompose) + - [With docker-compose](#with-docker-compose) + - [With docker-compose using Webhook](#with-docker-compose-using-webhook) + +## Without docker-compose using Webhook + +You can use this sync with webhooks as well, you need to specfiy webhook variable. +The webhook variables is number. Where 0 means that you do not want to use webhooks and everything greater than 0 means you do want to use webhooks. +The __default__ value is __0__ + +```bash +docker volume create -d local --name website_sources + +docker run --name git-sync -d \ + -e GIT_SYNC_REPO=https://github.com/openweb-nl/website-static.git \ + -e GIT_SYNC_DEST=/git \ + -e GIT_SYNC_BRANCH=master \ + -e GIT_SYNC_REV=FETCH_HEAD \ + -e GIT_SYNC_WEBHOOK=1 \ + -v website_sources:/git openweb/git-sync:0.0.1 + +docker run --name nginx \ + -d -p 8080:80 -v website_sources:/usr/share/nginx/html nginx +``` + +## Without docker-dompose + ```bash docker volume create -d local --name website_sources @@ -16,11 +45,13 @@ docker run --name git-sync -d \ docker run --name nginx \ -d -p 8080:80 -v website_sources:/usr/share/nginx/html nginx ``` -## with docker compose + +## With docker-compose Create a file called docker-compose.yml with the following content. + ```yml -version: '2' +version: "3" services: nginx: @@ -35,11 +66,11 @@ services: git-sync: image: openweb/git-sync:0.0.1 environment: - GIT_SYNC_REPO: "https://github.com/openweb-nl/website-static.git" - GIT_SYNC_DEST: "/git" - GIT_SYNC_BRANCH: "master" - GIT_SYNC_REV: "FETCH_HEAD" - GIT_SYNC_WAIT: "10" + - GIT_SYNC_REPO= "https://github.com/openweb-nl/website-static.git" + - GIT_SYNC_DEST= "/git" + - GIT_SYNC_BRANCH= "master" + - GIT_SYNC_REV= "FETCH_HEAD" + - GIT_SYNC_WAIT= "10" volumes: - website_sources:/git:z restart: always @@ -47,9 +78,48 @@ volumes: website_sources: driver: local ``` + Then start the containers with running the following command + ```bash docker-compose up -d ``` -[kubernetes contrib]: \ No newline at end of file +## With docker-compose using Webhook + +You can create a file called docker-compose.yml. + +```yml +version: "3" + +services: + nginx: + image: nginx:latest + ports: + - "8080:80" + volumes: + - website_sources:/usr/share/nginx/html:z + depends_on: + - git-sync + restart: always + git-sync: + image: openweb/git-sync:0.0.1 + environment: + - GIT_SYNC_REPO= "https://github.com/openweb-nl/website-static.git" + - GIT_SYNC_DEST= "/git" + - GIT_SYNC_BRANCH= "master" + - GIT_SYNC_REV= "FETCH_HEAD" + - GIT_SYNC_WEBHOOK "1" + volumes: + - website_sources:/git:z + restart: always +volumes: + website_sources: + driver: local +``` + +Then start the containers with running the following command + + docker-compose up -d + +[kubernetes contrib]: https://github.com/joemccann/dillinger From 1debd386004a07e896f4fb125b9db12a646d0ae5 Mon Sep 17 00:00:00 2001 From: Zobair Qauomi Date: Sat, 17 Oct 2020 17:41:54 +0200 Subject: [PATCH 3/7] Updating the documentation - Even more refactor on the documentation and better navigation. --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5be0576..790510b 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,12 @@ -Forked from [kubernetes contrib] +# Git-Sync -# Usage +This has been forked from [kubernetes contrib] and now do support webhooks. -- [Usage](#usage) +## Usage + +__Table of content__ +- [Git-Sync](#git-sync) + - [Usage](#usage) - [Without docker-compose using Webhook](#without-docker-compose-using-webhook) - [Without docker-dompose](#without-docker-dompose) - [With docker-compose](#with-docker-compose) From 0a219774954d89c4cc6f8401af207ec350f40455 Mon Sep 17 00:00:00 2001 From: Zobair Qauomi Date: Sat, 17 Oct 2020 17:45:51 +0200 Subject: [PATCH 4/7] Added the port that we should be listening to --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index c58acda..90d382c 100644 --- a/main.go +++ b/main.go @@ -67,6 +67,7 @@ func syncRepoOnWebhook(w http.ResponseWriter, r *http.Request) { } func handleRequest() { http.HandleFunc("/", syncRepoOnWebhook) + log.Fatal(http.ListenAndServe(":8081", nil)) } func main() { From f01dab8c820802a44768d97f5fd2934fed0582f6 Mon Sep 17 00:00:00 2001 From: Zobair Qauomi Date: Sat, 17 Oct 2020 19:43:04 +0200 Subject: [PATCH 5/7] Dockerfile correct. I was not working before --- Dockerfile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 7789b9a..81ea080 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,10 @@ #openweb/git-sync:0.0.1 FROM golang:1.6-onbuild + VOLUME ["/git"] ENV GIT_SYNC_DEST /git -ENTRYPOINT ["/go/bin/git-sync"] \ No newline at end of file + +COPY main.go /tmp +WORKDIR /tmp + +ENTRYPOINT [ "go", "run", "./main.go" ] \ No newline at end of file From 4cd29fd5a3fd55526d0bacd0215efc2ea9a9f7c0 Mon Sep 17 00:00:00 2001 From: Zobair Qauomi Date: Sat, 17 Oct 2020 19:45:20 +0200 Subject: [PATCH 6/7] Labels added to the dockerfile --- Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 81ea080..3250cae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,8 @@ -#openweb/git-sync:0.0.1 FROM golang:1.6-onbuild +LABEL maintainer="Zobair Qauomi" +LABEL version="0.0.2" + VOLUME ["/git"] ENV GIT_SYNC_DEST /git From 63464e3c932da935d2b2242d801db182291df652 Mon Sep 17 00:00:00 2001 From: Zobair Qauomi Date: Sat, 17 Oct 2020 19:49:21 +0200 Subject: [PATCH 7/7] I have corrected the documentation to not refere to the old image --- README.md | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 790510b..43460cf 100644 --- a/README.md +++ b/README.md @@ -5,13 +5,21 @@ This has been forked from [kubernetes contrib] and now do support webhooks. ## Usage __Table of content__ + - [Git-Sync](#git-sync) - [Usage](#usage) + - [Build The Image](#build-the-image) - [Without docker-compose using Webhook](#without-docker-compose-using-webhook) - [Without docker-dompose](#without-docker-dompose) - [With docker-compose](#with-docker-compose) - [With docker-compose using Webhook](#with-docker-compose-using-webhook) +## Build The Image + +Navigate to the directory where the Dockerfile is located then execute follwing + + docker build -t git-sync . + ## Without docker-compose using Webhook You can use this sync with webhooks as well, you need to specfiy webhook variable. @@ -27,7 +35,7 @@ docker run --name git-sync -d \ -e GIT_SYNC_BRANCH=master \ -e GIT_SYNC_REV=FETCH_HEAD \ -e GIT_SYNC_WEBHOOK=1 \ - -v website_sources:/git openweb/git-sync:0.0.1 + -v website_sources:/git git-sync docker run --name nginx \ -d -p 8080:80 -v website_sources:/usr/share/nginx/html nginx @@ -44,7 +52,7 @@ docker run --name git-sync -d \ -e GIT_SYNC_BRANCH=master \ -e GIT_SYNC_REV=FETCH_HEAD \ -e GIT_SYNC_WAIT=10 \ - -v website_sources:/git openweb/git-sync:0.0.1 + -v website_sources:/git git-sync docker run --name nginx \ -d -p 8080:80 -v website_sources:/usr/share/nginx/html nginx @@ -68,13 +76,13 @@ services: - git-sync restart: always git-sync: - image: openweb/git-sync:0.0.1 + image: git-sync environment: - - GIT_SYNC_REPO= "https://github.com/openweb-nl/website-static.git" - - GIT_SYNC_DEST= "/git" - - GIT_SYNC_BRANCH= "master" - - GIT_SYNC_REV= "FETCH_HEAD" - - GIT_SYNC_WAIT= "10" + - GIT_SYNC_REPO=https://github.com/openweb-nl/website-static.git + - GIT_SYNC_DEST=/git + - GIT_SYNC_BRANCH=master + - GIT_SYNC_REV=FETCH_HEAD + - GIT_SYNC_WAIT=10 volumes: - website_sources:/git:z restart: always @@ -107,13 +115,13 @@ services: - git-sync restart: always git-sync: - image: openweb/git-sync:0.0.1 + image: git-sync environment: - - GIT_SYNC_REPO= "https://github.com/openweb-nl/website-static.git" - - GIT_SYNC_DEST= "/git" - - GIT_SYNC_BRANCH= "master" - - GIT_SYNC_REV= "FETCH_HEAD" - - GIT_SYNC_WEBHOOK "1" + - GIT_SYNC_REPO=https://github.com/openweb-nl/website-static.git + - GIT_SYNC_DEST=/git + - GIT_SYNC_BRANCH=master + - GIT_SYNC_REV=FETCH_HEAD + - GIT_SYNC_WEBHOOK=1 volumes: - website_sources:/git:z restart: always