diff --git a/Jenkinsfile b/Jenkinsfile index 92c43c7..6149685 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -51,28 +51,39 @@ ]) ) +// Define the remotes and the working and deploy branches +def remote = 'origin' +def workingBranch = 'devel_bear' +def releaseBranch = 'release' + // Initialize configuration lazyConfig( name: 'dummy-jenkins-pl', - env: [ DRYRUN: true, VERSION: '0.0.1', PUBLISH: false, ], + env: [ + RELEASE: false, + DRYRUN: false, + VERSION: '0.0.1', + BUILD_DIR: 'target', + DEPLOY_USER: 'root', + DEPLOY_HOST_STST: 'bxtsvctwas002.boxtel', + DEPLOY_DIR: '/var/www/html/public/dummy-jenkins-pl', + DEPLOY_CRED: 'bot-ci-dgm', + ], inLabels: [ 'ubuntu-16', 'centos-7' ], onLabels: [ default: 'master', docker: 'docker', mac: 'mac', android: 'android', ], - noPoll: '.+_.+', + noPoll: '(.+_.+|release)', ) lazyStage { name = 'validate' tasks = [ [ + pre: { + def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt') + currentBuild.displayName = "#${env.BUILD_NUMBER} ${currentVersion}" + }, run: { - echo "This is my first task"/* - gitLog('27e440f290d8bf95a3908cad03a5588fc2f2fe73') - lazyGitPass('bot-ci-dgm', { - gitTag('test-0.0.1') - }) */ - echo "Creating repo with packages" - if (lazyConfig.DRYRUN) { echo "DRYRUN is true = ${env.DRYRUN}" } - if (lazyConfig.DRYRUN) { echo "DRYRUN is false = ${env.DRYRUN}" } + echo "This is my first task" }, ], [ run: { echo "This is my second task" }, in: '*', on: 'docker' ], @@ -106,36 +117,85 @@ [ run: { echo "Building packages"; - sh "mkdir -p target/packages && echo 'testpkg' > target/packages/\${LAZY_LABEL}.pkg" + sh "mkdir -p ${env.BUILD_DIR} && echo 'testpkg' > ${env.BUILD_DIR}/\${LAZY_LABEL}.pkg" }, in: [ 'ubuntu-16', 'centos-7', ], on: 'docker', post: { echo "Archiving packages" - archiveArtifacts(artifacts: 'target/packages/**', allowEmptyArchive: false) + archiveArtifacts(artifacts: "${env.BUILD_DIR}/**", allowEmptyArchive: false) }, ], ] } +// Release stage only only if criteria are met +lazyStage { + name = 'release' + onlyif = ( lazyConfig['branch'] == workingBranch && lazyConfig.env.RELEASE ) + // Ask version if release flag and set and we are in the branch to fork release from + input = [ + message: 'Version string', + parameters: [string(defaultValue: '', description: 'Version string to be use for the next release', name: 'VERSION')] + ] + tasks = [ + pre: { + def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt') + gitAuth('bot-ci-dgm-rsa', { + // Define next version based on optional input + // TODO: impelement/import a generic helper for semantic versionning + def nextVersion = 'new' + if (env.lazyInput) { + nextVersion = env.lazyInput + } + // Merge master into release branch + gitMerge(workingBranch, releaseBranch) + // Bump version and tag the release branch before deploying + sh("git checkout ${releaseBranch}") + writeFile(encoding: 'UTF-8', text: nextVersion, file: 'version.txt') + gitCommit("Update version to ${nextVersion}", 'version.txt') + }) + unarchive(mapping:["${env.BUILD_DIR}/" : '.']) + }, + run: { + def currentVersion = readFile(encoding: 'UTF-8', file: 'version.txt') + // Rebuild the site with the new version + echo("Rebuild for version ${currentVersion}") + }, + post: { + gitAuth('bot-ci-dgm-rsa', { + gitTag("${currentVersion}", remote) + gitPush(remote, releaseBranch) + }) + archiveArtifacts(artifacts: "${env.BUILD_DIR}/**", allowEmptyArchive: false) + }, + in: [ 'ubuntu-16', 'centos-7', ], on: 'docker', + ] +} + lazyStage { name = 'publish' - onlyif = ( env.LAZY_BRANCH == 'master' || lazyConfig.env.PUBLISH ) + onlyif = ( env.LAZY_BRANCH == 'master' || lazyConfig.env.RELEASE ) + input = 'Publish?' tasks = [ [ pre: { echo "Unarchiving packages" - unarchive(mapping:['target/packages/' : '.']) + unarchive(mapping:["${env.BUILD_DIR}/" : '.']) sh("ls -lA target/packages") }, run: { - echo "Creating repo with packages" - if (lazyConfig.env.DRYRUN) { echo "DRYRUN is true = ${lazyConfig.env.DRYRUN}" } - if (!lazyConfig.env.DRYRUN) { echo "DRYRUN is false = ${lazyConfig.env.DRYRUN}" } + echo "Creating repo with packages" }, in: [ 'ubuntu-16', 'centos-7', ], on: 'docker', - post: { echo "Publishing package repos" }, + post: { + echo "Publishing package repos" + sshagent(credentials: [env.DEPLOY_CRED]) { + sshDeploy(env.BUILD_DIR, "${env.DEPLOY_USER}@${env.DEPLOY_HOST_STST}", env.DEPLOY_DIR) + } + }, ], ] } + diff --git a/version.txt b/version.txt new file mode 100644 index 0000000..61b3725 --- /dev/null +++ b/version.txt @@ -0,0 +1 @@ +0.0.0-0 \ No newline at end of file