From 7eb51d915a3143ba929abdc3b1fa17a7863795da Mon Sep 17 00:00:00 2001 From: Andrey Arapov Date: Tue, 19 Sep 2017 20:55:07 +0200 Subject: [PATCH] add a CI part --- Jenkinsfile | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 21 ++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 Jenkinsfile create mode 100644 Makefile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..5b9da8c --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,64 @@ +// https://jenkins.io/doc/book/pipeline/ +// Inspired by Lachlan Evenson https://github.com/lachie83/croc-hunter/blob/master/Jenkinsfile + +//Lets define a unique label for this build. +def label = "buildpod.${env.JOB_NAME}.${env.BUILD_NUMBER}".replace('-', '_').replace('/', '_') + +podTemplate(label: label, containers: [ + containerTemplate(name: 'jnlp', image: 'jenkinsci/jnlp-slave:3.10-1-alpine', args: '${computer.jnlpmac} ${computer.name}', workingDir: '/home/jenkins', resourceRequestCpu: '200m', resourceLimitCpu: '200m', resourceRequestMemory: '256Mi', resourceLimitMemory: '256Mi'), + containerTemplate(name: 'docker', image: 'docker:1.12.6', command: 'cat', ttyEnabled: true), + containerTemplate(name: 'make', image: 'andrey01/make:0.2', command: 'cat', ttyEnabled: true), + ], + volumes:[ + hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'), + ],) +{ + + node (label) { + + stage ('Checkout repo') { + checkout scm + } + + sh 'git rev-parse HEAD > git_commit_id.txt' + try { + env.GIT_COMMIT_ID = readFile('git_commit_id.txt').trim() + env.GIT_SHA = env.GIT_COMMIT_ID.substring(0, 7) + } catch (e) { + error "${e}" + } + println "env.GIT_COMMIT_ID ==> ${env.GIT_COMMIT_ID}" + + container('make') { + stage ('Build') { + sh "VERSION=${env.GIT_SHA} make" + } + + stage ('Test') { + sh "VERSION=${env.GIT_SHA} make check" + } + + if (env.BRANCH_NAME == 'master') { + // perform docker login to Docker Hub as the docker-pipeline-plugin doesn't work with the next auth json format + // withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: config.container_repo.jenkins_creds_id, + // sh "docker login -e ${config.container_repo.dockeremail} -u ${env.USERNAME} -p ${env.PASSWORD}" + withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'my-dockerhub-creds', + usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { + sh "docker login -u ${env.USERNAME} -p ${env.PASSWORD}" + } + + stage ('Deploy') { + sh "VERSION=${env.GIT_SHA} make publish" + } + + sh 'docker logout' + + } else { + println "Current branch ${env.BRANCH_NAME}" + } + + } // node + + } // PodTemplate + +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d0a1168 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +NS ?= andrey01 +NAME ?= rainloop +VERSION ?= 0.1 + +default: build + +build: + docker build --pull -t $(NS)/$(NAME):$(VERSION) -t $(NS)/$(NAME):latest -f Dockerfile . + +publish: + docker push $(NS)/$(NAME):$(VERSION) + docker push $(NS)/$(NAME):latest + +check: + docker run --rm -i $(NS)/$(NAME):$(VERSION) sh -c "set -x; exit 0" + +console: + docker run --rm -ti --entrypoint sh $(NS)/$(NAME):$(VERSION) + +clean: + docker rmi $(NS)/$(NAME):$(VERSION) $(NS)/$(NAME):latest