From d89420b516e365abee7682499885130f27dddbcf Mon Sep 17 00:00:00 2001 From: Gall Zvyagin Date: Wed, 5 Dec 2018 17:58:59 +0200 Subject: [PATCH 1/5] DEVOPS-203 add Jenkinsfile --- Jenkinsfile | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..05d40e2 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,152 @@ +def label = "${UUID.randomUUID().toString()}" +def BUILD_FOLDER = "/go" +def github_user = "gkirok" +def docker_user = "gallziguazio" +def git_project = "iguazio_api_examples" + +properties([pipelineTriggers([[$class: 'PeriodicFolderTrigger', interval: '2m']])]) +podTemplate(label: "${git_project}-${label}", yaml: """ +apiVersion: v1 +kind: Pod +metadata: + name: "${git_project}-${label}" + labels: + jenkins/kube-default: "true" + app: "jenkins" + component: "agent" +spec: + shareProcessNamespace: true + containers: + - name: jnlp + image: jenkinsci/jnlp-slave + resources: + limits: + cpu: 1 + memory: 2Gi + requests: + cpu: 1 + memory: 2Gi + volumeMounts: + - name: go-shared + mountPath: /go + - name: docker-cmd + image: docker + command: [ "/bin/sh", "-c", "--" ] + args: [ "while true; do sleep 30; done;" ] + volumeMounts: + - name: docker-sock + mountPath: /var/run + - name: go-shared + mountPath: /go + volumes: + - name: docker-sock + hostPath: + path: /var/run + - name: go-shared + emptyDir: {} +""" + ) { + node("${git_project}-${label}") { +// currentBuild.displayName = "${git_project}" +// currentBuild.description = "Will not run with tags created before 4 hours and more." + + withCredentials([ + usernamePassword(credentialsId: '4318b7db-a1af-4775-b871-5a35d3e75c21', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME'), + string(credentialsId: 'dd7f75c5-f055-4eb3-9365-e7d04e644211', variable: 'GIT_TOKEN') + ]) { + def AUTO_TAG + def TAG_VERSION + + stage('get tag data') { + container('jnlp') { + TAG_VERSION = sh( + script: "echo ${TAG_NAME} | tr -d '\\n' | egrep '^v[\\.0-9]*.*\$' | sed 's/v//'", + returnStdout: true + ).trim() + + sh "curl -v -H \"Authorization: token ${GIT_TOKEN}\" https://api.github.com/repos/gkirok/${git_project}/releases/tags/v${TAG_VERSION} > ~/tag_version" + + AUTO_TAG = sh( + script: "cat ~/tag_version | python -c 'import json,sys;obj=json.load(sys.stdin);print obj[\"body\"]'", + returnStdout: true + ).trim() + + PUBLISHED_BEFORE = sh( + script: "tag_published_at=\$(cat ~/tag_version | python -c 'import json,sys;obj=json.load(sys.stdin);print obj[\"published_at\"]'); SECONDS=\$(expr \$(date +%s) - \$(date -d \"\$tag_published_at\" +%s)); expr \$SECONDS / 60 + 1", + returnStdout: true + ).trim() + + echo "$AUTO_TAG" + echo "$TAG_VERSION" + echo "$PUBLISHED_BEFORE" + } + } + + if ( TAG_VERSION && PUBLISHED_BEFORE < 240 ) { +// def V3IO_TSDB_VERSION = sh( +// script: "echo ${TAG_VERSION} | awk -F '-v' '{print \"v\"\$2}'", +// returnStdout: true +// ).trim() + + stage('prepare sources') { + container('jnlp') { + sh """ + cd ${BUILD_FOLDER} + git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${github_user}/${git_project}.git src/github.com/v3io/${git_project} + cd ${BUILD_FOLDER}/src/github.com/v3io/${git_project}/netops_demo/golang/src/github.com/v3io/demos + rm -rf vendor/github.com/v3io/v3io-tsdb/ + git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${github_user}/v3io-tsdb.git vendor/github.com/v3io/v3io-tsdb + cd vendor/github.com/v3io/v3io-tsdb + rm -rf .git vendor/github.com/v3io vendor/github.com/nuclio + """ + +// git checkout ${V3IO_TSDB_VERSION} + } + } + + stage('build in dood') { + container('docker-cmd') { + sh """ + cd ${BUILD_FOLDER}/src/github.com/v3io/${git_project}/netops_demo/golang/src/github.com/v3io/demos + docker build . --tag netops-demo-golang:latest --tag ${docker_user}/netops-demo-golang:${TAG_VERSION} --build-arg NUCLIO_BUILD_OFFLINE=true --build-arg NUCLIO_BUILD_IMAGE_HANDLER_DIR=github.com/v3io/demos + + cd ${BUILD_FOLDER}/src/github.com/v3io/${git_project}/netops_demo/py + docker build . --tag netops-demo-py:latest --tag ${docker_user}/netops-demo-py:${TAG_VERSION} + """ + withDockerRegistry([credentialsId: "472293cc-61bc-4e9f-aecb-1d8a73827fae", url: ""]) { + sh "docker push ${docker_user}/netops-demo-golang:${TAG_VERSION}" + sh "docker push ${docker_user}/netops-demo-py:${TAG_VERSION}" + } + } + } + + stage('git push') { + container('jnlp') { + try { + sh """ + git config --global user.email '${GIT_USERNAME}@iguazio.com' + git config --global user.name '${GIT_USERNAME}' + cd ${BUILD_FOLDER}/src/github.com/v3io/${git_project}/netops_demo + git add * + git commit -am 'Updated TSDB to latest'; + git push origin master + """ + } catch (err) { + echo "Can not push code to git" + } + } + } + } else { + stage('warning') { + if (PUBLISHED_BEFORE >= 240) { + echo "Tag too old, published before $PUBLISHED_BEFORE minutes." + } else if (AUTO_TAG.startsWith("Autorelease")) { + echo "Autorelease does not trigger this job." + } else { + echo "${TAG_VERSION} is not release tag." + } + } + } + } + } +} \ No newline at end of file From 435e23eaf1ef74fd2dc6af6f74b594c97969ce6f Mon Sep 17 00:00:00 2001 From: Gall Zvyagin Date: Wed, 5 Dec 2018 21:06:47 +0200 Subject: [PATCH 2/5] DEVOPS-203 update jenkins cred ids --- Jenkinsfile | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 05d40e2..4d562b0 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,8 +1,11 @@ -def label = "${UUID.randomUUID().toString()}" -def BUILD_FOLDER = "/go" -def github_user = "gkirok" -def docker_user = "gallziguazio" -def git_project = "iguazio_api_examples" +label = "${UUID.randomUUID().toString()}" +BUILD_FOLDER = "/go" +docker_user = "iguaziodocker" +docker_credentials = "iguazio-prod-docker-credentials" +git_project = "iguazio_api_examples" +git_project_user = "v3io" +git_deploy_user = "iguazio-prod-git-user" +git_deploy_user_token = "iguazio-prod-git-user-token" properties([pipelineTriggers([[$class: 'PeriodicFolderTrigger', interval: '2m']])]) podTemplate(label: "${git_project}-${label}", yaml: """ @@ -47,12 +50,9 @@ spec: """ ) { node("${git_project}-${label}") { -// currentBuild.displayName = "${git_project}" -// currentBuild.description = "Will not run with tags created before 4 hours and more." - withCredentials([ - usernamePassword(credentialsId: '4318b7db-a1af-4775-b871-5a35d3e75c21', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME'), - string(credentialsId: 'dd7f75c5-f055-4eb3-9365-e7d04e644211', variable: 'GIT_TOKEN') + usernamePassword(credentialsId: git_deploy_user, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME'), + string(credentialsId: git_deploy_user_token, variable: 'GIT_TOKEN') ]) { def AUTO_TAG def TAG_VERSION @@ -83,23 +83,17 @@ spec: } if ( TAG_VERSION && PUBLISHED_BEFORE < 240 ) { -// def V3IO_TSDB_VERSION = sh( -// script: "echo ${TAG_VERSION} | awk -F '-v' '{print \"v\"\$2}'", -// returnStdout: true -// ).trim() - stage('prepare sources') { container('jnlp') { sh """ cd ${BUILD_FOLDER} - git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${github_user}/${git_project}.git src/github.com/v3io/${git_project} + git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${git_project_user}/${git_project}.git src/github.com/v3io/${git_project} cd ${BUILD_FOLDER}/src/github.com/v3io/${git_project}/netops_demo/golang/src/github.com/v3io/demos rm -rf vendor/github.com/v3io/v3io-tsdb/ - git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${github_user}/v3io-tsdb.git vendor/github.com/v3io/v3io-tsdb + git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${git_project_user}/v3io-tsdb.git vendor/github.com/v3io/v3io-tsdb cd vendor/github.com/v3io/v3io-tsdb rm -rf .git vendor/github.com/v3io vendor/github.com/nuclio """ - // git checkout ${V3IO_TSDB_VERSION} } } @@ -113,7 +107,7 @@ spec: cd ${BUILD_FOLDER}/src/github.com/v3io/${git_project}/netops_demo/py docker build . --tag netops-demo-py:latest --tag ${docker_user}/netops-demo-py:${TAG_VERSION} """ - withDockerRegistry([credentialsId: "472293cc-61bc-4e9f-aecb-1d8a73827fae", url: ""]) { + withDockerRegistry([credentialsId: docker_credentials, url: ""]) { sh "docker push ${docker_user}/netops-demo-golang:${TAG_VERSION}" sh "docker push ${docker_user}/netops-demo-py:${TAG_VERSION}" } From 50c9bcbb88dfd61e7ee40021dd4543c06061f182 Mon Sep 17 00:00:00 2001 From: Gall Zvyagin Date: Wed, 5 Dec 2018 21:11:40 +0200 Subject: [PATCH 3/5] DEVOPS-203 remove git update --- Jenkinsfile | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 4d562b0..15f9464 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -88,13 +88,7 @@ spec: sh """ cd ${BUILD_FOLDER} git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${git_project_user}/${git_project}.git src/github.com/v3io/${git_project} - cd ${BUILD_FOLDER}/src/github.com/v3io/${git_project}/netops_demo/golang/src/github.com/v3io/demos - rm -rf vendor/github.com/v3io/v3io-tsdb/ - git clone https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${git_project_user}/v3io-tsdb.git vendor/github.com/v3io/v3io-tsdb - cd vendor/github.com/v3io/v3io-tsdb - rm -rf .git vendor/github.com/v3io vendor/github.com/nuclio """ -// git checkout ${V3IO_TSDB_VERSION} } } @@ -113,23 +107,6 @@ spec: } } } - - stage('git push') { - container('jnlp') { - try { - sh """ - git config --global user.email '${GIT_USERNAME}@iguazio.com' - git config --global user.name '${GIT_USERNAME}' - cd ${BUILD_FOLDER}/src/github.com/v3io/${git_project}/netops_demo - git add * - git commit -am 'Updated TSDB to latest'; - git push origin master - """ - } catch (err) { - echo "Can not push code to git" - } - } - } } else { stage('warning') { if (PUBLISHED_BEFORE >= 240) { From 2f5d8387153b75459cde33c39aafb22946c9256c Mon Sep 17 00:00:00 2001 From: Gall Zvyagin Date: Wed, 5 Dec 2018 21:29:55 +0200 Subject: [PATCH 4/5] DEVOPS-203 push to hub stage --- Jenkinsfile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 15f9464..28793bd 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -101,6 +101,11 @@ spec: cd ${BUILD_FOLDER}/src/github.com/v3io/${git_project}/netops_demo/py docker build . --tag netops-demo-py:latest --tag ${docker_user}/netops-demo-py:${TAG_VERSION} """ + } + } + + stage('push to hub') { + container('docker-cmd') { withDockerRegistry([credentialsId: docker_credentials, url: ""]) { sh "docker push ${docker_user}/netops-demo-golang:${TAG_VERSION}" sh "docker push ${docker_user}/netops-demo-py:${TAG_VERSION}" From fa8df45ba68e30400dd0cbd15f78535b8ef46d66 Mon Sep 17 00:00:00 2001 From: Gall Zvyagin Date: Wed, 5 Dec 2018 23:06:39 +0200 Subject: [PATCH 5/5] DEVOPS-203 update curl git path --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 28793bd..dd30278 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -64,7 +64,7 @@ spec: returnStdout: true ).trim() - sh "curl -v -H \"Authorization: token ${GIT_TOKEN}\" https://api.github.com/repos/gkirok/${git_project}/releases/tags/v${TAG_VERSION} > ~/tag_version" + sh "curl -v -H \"Authorization: token ${GIT_TOKEN}\" https://api.github.com/repos/${git_project_user}/${git_project}/releases/tags/v${TAG_VERSION} > ~/tag_version" AUTO_TAG = sh( script: "cat ~/tag_version | python -c 'import json,sys;obj=json.load(sys.stdin);print obj[\"body\"]'",