Groovy web console

subscribe to the feed Subscribe
to this
site
Jenkinsfile (via #groovywebconsole)
tweet this snippet Tweet
this
script

Jenkinsfile

Published 2 months ago by s.kramer
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
node {    properties([
            pipelineTriggers([pollSCM('*/5 * * * *')]),
            buildDiscarder(logRotator(artifactDaysToKeepStr: '8', artifactNumToKeepStr: '8', daysToKeepStr: '8', numToKeepStr: '8')),
            parameters([
                    booleanParam(name: 'BUILD_NOW', defaultValue: true, description: 'Build now?'),
                    string(name: 'SKIP_TEST', defaultValue: 'false', description: 'Skip unit tests?(not recommended)'),
//                    release params
                    booleanParam(name: 'RELEASE', defaultValue: false, description: 'Perform release?'),
                    booleanParam(name: 'MERGE_RELEASE', defaultValue: false, description: 'Merge release into master?'),
                    string(name: 'RELEASE_VERSION', defaultValue: '', description: 'Release version'),
                    string(name: 'NEXT_VERSION', defaultValue: '', description: 'Next version (without SNAPSHOT)'),
//                    hotfix params
                    booleanParam(name: 'HOTFIX_BRANCH', defaultValue: false, description: 'Perform hotfix branch?'),
                    string(name: 'VERSION_NEED_FOR_HOTFIX', defaultValue: '', description: 'Version which is needed for the hotfix'),
                    string(name: 'HOTFIX_VERSION', defaultValue: '', description: 'Hotfix version'),
                    booleanParam(name: 'MERGE_HOTFIX', defaultValue: false, description: 'Merge hotfix into dev and master?')
            ])
    ])
//    names of jenkins tools
    def mvn_version = 'Maven 3.3.9'
    def jdk_version = 'jdk8'

    def giturl = 'git@gitlab.i-free.com:fastgate/db-migration.git'

    stage('Clean workspace') {
        deleteDir()
    }

    stage('Checkout source code') {
        git branch: env.BRANCH_NAME, url: giturl
    }
//    Build SNAPSHOTs
    if (params.BUILD_NOW == true){
        stage('Build') {
            withEnv(["JAVA_HOME=${tool jdk_version}", "PATH+MAVEN=${tool mvn_version}/bin:${env.JAVA_HOME}/bin"]) {
                // Apache Maven related side notes:
                // --batch-mode : recommended in CI to inform maven to not run in interactive mode (less logs)
                // -V : strongly recommended in CI, will display the JDK and Maven versions in use.
                //      Very useful to be quickly sure the selected versions were the ones you think.
                // -U : force maven to update snapshots each time (default : once an hour, makes no sense in CI).
                // -Dsurefire.useFile=false : useful in CI. Displays test errors in the logs directly (instead of
                //                            having to crawl the workspace files to see the cause).
                sh "mvn clean deploy -DskipTests=${SKIP_TEST} -B -U"
            }
        }
    }
//    Release
    if (params.RELEASE == true) {
        stage('Release') {
            withEnv(["JAVA_HOME=${tool jdk_version}", "PATH+MAVEN=${tool mvn_version}/bin:${env.JAVA_HOME}/bin"]) {
//                create release tag and change pom
                sh "mvn clean release:prepare release:perform release:update-versions -DpushChanges=false " +
                        "-DdevelopmentVersion=${NEXT_VERSION}-SNAPSHOT -DreleaseVersion=${RELEASE_VERSION} " +
                        "-DskipTests=${SKIP_TEST} -DlocalCheckout=true -B -U"
//                push release tag and next dev version
                sh "git push origin ${RELEASE_VERSION} && git push origin dev"
            }
        }
    }

    if (params.MERGE_RELEASE == true) {
        stage('Merge release into master branch') {
            sh "git checkout ${RELEASE_VERSION}"
            sh "git checkout master"
            sh "git merge --no-ff ${RELEASE_VERSION}"
            sh "git push --force origin master"
        }
    }

//    Archive artifacts and build Docker images
    if (params.BUILD_NOW == true || params.RELEASE == true) {

        stage('Archive the build output artifacts') {
            archiveArtifacts artifacts: '**/target/*',
                    excludes: '**/target/*.jar,bundle-parent/target/*,**/target/checkout/**',
                    fingerprint: true
        }

        docker.withRegistry('https://gitlab.i-free.com:5005', 'SVC_Jenk-Gitlab') {
            stage ('Build and push Docker image') {
                def repo = 'fastgate/db-migration'
                pom = readMavenPom file: 'pom.xml'
                pom_postgre = readMavenPom file: 'ibs-db-migration-postgresql/pom.xml'
                pom_oracle = readMavenPom file: 'ibs-db-migration-oracle/pom.xml'
                pom_msqlserver = readMavenPom file: 'ibs-db-migration-mssqlserver/pom.xml'

                parallel (
                        "postgresql": {
                            def postgreimg = docker.build("${repo}:${pom.version}-postgresql",
                                    "-f docker/Dockerfile --build-arg VERSION=${pom.version} " +
                                            "--build-arg ARTIFACT_ID=${pom_postgre.artifactId} .")
                            postgreimg.push()
                        },
                        "oracle": {
                            def oracleimg = docker.build("${repo}:${pom.version}-oracle",
                                    "-f docker/Dockerfile --build-arg VERSION=${pom.version} " +
                                            "--build-arg ARTIFACT_ID=${pom_oracle.artifactId} .")
                            oracleimg.push()
                        },
                        "mssqlserver": {
                            def mssqlserverimg = docker.build("${repo}:${pom.version}-mssqlserver",
                                    "-f docker/Dockerfile --build-arg VERSION=${pom.version} " +
                                            "--build-arg ARTIFACT_ID=${pom_msqlserver.artifactId} .")
                            mssqlserverimg.push()
                        }
                )
            }
        }
    }

}