From 192e6c19749fc1ed8ad90416a8fa0efcddb3bbc5 Mon Sep 17 00:00:00 2001 From: William Petit Date: Tue, 12 Mar 2019 10:32:55 +0100 Subject: [PATCH] Build and publish image with Jenkins Add a Jenkins pipeline to build and publish docker images on the DockerHub. The images are built on every branches but only pusblished on staging and stable branches (i.e. develop and master by default). Images are published with multiples tags: - : - :-latest --- Jenkinsfile | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++ ci/Dockerfile | 1 + 2 files changed, 116 insertions(+) create mode 100644 Jenkinsfile create mode 100644 ci/Dockerfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..5edc6f4 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,115 @@ +pipeline { + + agent { + dockerfile { + filename 'Dockerfile' + dir 'ci' + args '--privileged -v /var/run/docker.sock:/var/run/docker.sock' + } + } + + triggers { + gitlab( + triggerOnPush: true, + triggerOnMergeRequest: true, + branchFilterType: 'All', + cancelPendingBuildsOnUpdate: false + ) + } + + options { + gitLabConnection('Gitlab MIM') + gitlabBuilds(builds: []) + } + + parameters { + string(defaultValue: "develop", description: 'Branche "staging"', name: 'stagingBranch') + string(defaultValue: "master", description: 'Branche "stable"', name: 'stableBranch') + } + + environment { + IMAGE_NAME = "zephir-server-manager" + IMAGE_COMMIT_TAG = env.GIT_COMMIT.substring(0,8) + } + + stages { + + stage('Build image') { + steps { + gitlabStage(STAGE_NAME) { + withDockerHubCredentials { + sh """ + # Construction de l'image + docker build -t '${DOCKER_USERNAME}/${IMAGE_NAME}' . + + # Ajout du tag de commit + docker tag '${DOCKER_USERNAME}/${IMAGE_NAME}:latest' '${DOCKER_USERNAME}/${IMAGE_NAME}:${IMAGE_COMMIT_TAG}' + """ + } + } + } + } + + stage('Publish staging image') { + when { + branch params.stagingBranch + } + steps { + gitlabStage(STAGE_NAME) { + addTagAndPublishDockerImage( + env.IMAGE_COMMIT_TAG, + "staging-latest" + ) + } + } + } + + stage('Publish stable image') { + when { + branch params.stableBranch + } + steps { + gitlabStage(STAGE_NAME) { + addTagAndPublishDockerImage( + env.IMAGE_COMMIT_TAG, + "stable-latest" + ) + } + } + } + + } + +} + + +def gitlabStage(String stageName, Closure fn) { + gitlabBuilds(builds: [stageName]) { + gitlabCommitStatus(stageName) { + fn() + } + } +} + +def withDockerHubCredentials(Closure fn) { + withCredentials([ + usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')] + ){ + fn() + } +} + +def addTagAndPublishDockerImage(String originalTag, String newTag) { + withDockerHubCredentials { + sh """ + # Ajout du nouveau tag + docker tag '${DOCKER_USERNAME}/${IMAGE_NAME}:${originalTag}' '${DOCKER_USERNAME}/${IMAGE_NAME}:${newTag}' + + # Publication de l'image sur le DockerHub + echo "${DOCKER_PASSWORD}" | docker login --username '${DOCKER_USERNAME}' --password-stdin + docker push '${DOCKER_USERNAME}/${IMAGE_NAME}:${newTag}' + docker push '${DOCKER_USERNAME}/${IMAGE_NAME}:${originalTag}' + docker logout + """ + } +} \ No newline at end of file diff --git a/ci/Dockerfile b/ci/Dockerfile new file mode 100644 index 0000000..c72ac8e --- /dev/null +++ b/ci/Dockerfile @@ -0,0 +1 @@ +FROM docker:stable -- GitLab