Last active
September 8, 2019 19:19
-
-
Save pinkeen/fc97960344cc2683eca715071859c09f to your computer and use it in GitHub Desktop.
Docker Hub Build Jenkins Pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This pipeline kind of simulates the Docker Hub's execution environment | |
* hence the build hook is used and the same env vars are provided. | |
*/ | |
pipeline { | |
agent any | |
environment { | |
DOCKER_REPO="${params.DOCKER_REPO}" | |
DOCKER_TAG="${params.DOCKER_TAG}" | |
DOCKERFILE_PATH="${params.DOCKERFILE_PATH}" | |
IMAGE_NAME="${params.DOCKER_REPO}:${params.DOCKER_TAG}" | |
} | |
options { | |
ansiColor('xterm') | |
buildDiscarder( | |
logRotator(numToKeepStr: '10') | |
) | |
} | |
parameters { | |
booleanParam( name: 'ENABLE_STAGE_BUILD', defaultValue: true, description: 'Enable Build Stage') | |
booleanParam( name: 'ENABLE_STAGE_TEST', defaultValue: true, description: 'Enable Test Stage') | |
booleanParam( name: 'ENABLE_STAGE_PUSH', defaultValue: true, description: 'Enable Push Stage') | |
string( name: 'DOCKER_REPO', defaultValue: params.DOCKER_REPO ?: 'magesuite/run-tests', description: 'Target docker repo name') | |
string( name: 'DOCKER_TAG', defaultValue: params.DOCKER_TAG ?: 'latest', description: 'Target docker tag') | |
string( name: 'DOCKERFILE_PATH', defaultValue: params.DOCKERFILE_PATH ?: 'Dockerfile', description: 'Location of the Dockefile') | |
string( name: 'VCS_URL', defaultValue: params.VCS_URL ?: 'git@github.com:magesuite/docker-run-magento-tests.git', description: 'Source git repo url') | |
string( name: 'VCS_REF', defaultValue: params.VCS_REF ?: '*/master', description: 'Branch, tag or commt to build') | |
credentials( name: 'VCS_CREDENTIALS', defaultValue: params.VCS_CREDENTIALS ?: null, description: 'Credentials for access to git repo') | |
} | |
stages { | |
stage ('Checkout') { | |
steps { | |
script { | |
VCS_CHECKOUT = checkout([ | |
$class: 'GitSCM', | |
branches: [[ | |
name: params.VCS_REF | |
]], | |
userRemoteConfigs: [[ | |
credentialsId: params.VCS_CREDENTIALS, | |
url: params.VCS_URL | |
]] | |
]) | |
VCS_COMMIT = VCS_CHECKOUT.GIT_COMMIT | |
VCS_COMMIT_MSG = sh(script: 'git log --format=%B -n 1', returnStdout: true).trim() | |
VCS_BRANCH = VCS_CHECKOUT.GIT_BRANCH | |
VCS_TAG = sh(returnStdout: true, script: "git describe --exact-match ${VCS_COMMIT} || true").trim() | |
// Set docker hub env-vars | |
env.SOURCE_COMMIT = VCS_COMMIT | |
env.SOURCE_BRANCH = VCS_BRANCH | |
env.COMMIT_MSG = VCS_COMMIT_MSG | |
sh '[[ -f hooks/post_checkout ]] && bash hooks/post_checkout || echo "No post_checkout hook, skipping..."' | |
} | |
} | |
} | |
stage ('Auto Build') { | |
when { | |
expression { ! fileExists('hooks/build') && params.ENABLE_STAGE_BUILD } | |
} | |
steps { | |
script { | |
sh '[[ -f hooks/pre_build ]] && bash hooks/pre_build || echo "No pre_build hook, skipping..."' | |
BUILT_IMAGE = docker.build(env.IMAGE_NAME, params.DOCKERFILE_PATH) | |
sh '[[ -f hooks/post_build ]] && bash hooks/post_build || echo "No post_build hook, skipping..."' | |
} | |
} | |
} | |
stage ('Cutom Build Hook') { | |
when { | |
expression { fileExists('hooks/build') && params.ENABLE_STAGE_BUILD } | |
} | |
steps { | |
script { | |
sh '[[ -f hooks/pre_build ]] && bash hooks/pre_build || echo "No pre_build hook, skipping..."' | |
sh 'bash hooks/build' | |
sh '[[ -f hooks/post_build ]] && bash hooks/post_build || echo "No post_build hook, skipping..."' | |
} | |
} | |
} | |
stage ('Auto Test') { | |
when { | |
expression { ! fileExists('hooks/test') && params.ENABLE_STAGE_TEST } | |
} | |
steps { | |
script { | |
sh '[[ -f hooks/pre_test ]] && bash hooks/pre_test || echo "No pre_test hook, skipping..."' | |
echo 'Not implemented 😅' | |
sh '[[ -f hooks/post_test ]] && bash hooks/post_test || echo "No post_test hook, skipping..."' | |
} | |
} | |
} | |
stage ('Custom Test Hook') { | |
when { | |
expression { fileExists('hooks/test') && params.ENABLE_STAGE_TEST} | |
} | |
steps { | |
script { | |
sh '[[ -f hooks/pre_test ]] && bash hooks/pre_test || echo "No pre_test hook, skipping..."' | |
sh 'bash hooks/test' | |
sh '[[ -f hooks/post_test ]] && bash hooks/post_test || echo "No post_test hook, skipping..."' | |
} | |
} | |
} | |
stage ('Auto Push') { | |
when { | |
expression { ! fileExists('hooks/push') && params.ENABLE_STAGE_PUSH } | |
} | |
steps { | |
script { | |
sh '[[ -f hooks/pre_test ]] && bash hooks/pre_test || echo "No pre_test hook, skipping..."' | |
BUILT_IMAGE.push() | |
sh '[[ -f hooks/post_test ]] && bash hooks/post_test || echo "No post_test hook, skipping..."' | |
} | |
} | |
} | |
stage ('Custom Push Hook') { | |
when { | |
expression { fileExists('hooks/push') && params.ENABLE_STAGE_PUSH } | |
} | |
steps { | |
script { | |
sh '[[ -f hooks/pre_push ]] && bash hooks/pre_push || echo "No pre_push hook, skipping..."' | |
sh 'bash hooks/push' | |
sh '[[ -f hooks/post_push ]] && bash hooks/post_push || echo "No post_push hook, skipping..."' | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment