I would like to do building image based on PullRequest, run tests on it and
destroy container. I attach Jenkinsfile where is whole pipeline which works
on Jenkins master, which is installed on physical machine. There is also
installed Docker. I have used image from
https://hub.docker.com/r/jenkinsci/jnlp-slave/
<https://hub.docker.com/r/jenkinsci/jnlp-slave/?fbclid=IwAR0JIecuCmA4Ke80nwzownF02X4b0lgB67wrw5td5kX3MHoNV90x4Fk4Eqw>
with some extras added by me - ruby, rails, postgres to create
jenkins-slave. As next step I attached it to Jenkins master. Of course it
works perfect. Each step from pipeline execute properly. But it's not what
I want to reach. I would like to build docker image based on PullRequest,
then run step with tests on it. After finish this part with success I would
like to move next steps in Jenkins master and destroy/delete container and
image build from PR.

-- 
*Pozdrawiam / Kind regards,*
Piotr Bracha
Administrator Systemów
*Vasco Electronics sp. z o.o. S.K.A.*
al. Pokoju 1, CTA/350
31-548 Kraków
NIP 677 236 91 51

E-mail:  [email protected]
[image: Vasco Electronics]  <https://vasco-electronics.com/>[image:
wKarteczkach Logo]  <http://wkarteczkach.pl/>[image: Tłumacze Elektroniczne
Logo] <https://tlumacze-elektroniczne.pl/>
Vasco Electronics Spółka z ograniczoną odpowiedzialnością Spółka
Komandytowo-Akcyjna, Al. Pokoju 1, CTA/350, 31-548 Kraków, Polska, NIP:
6772369151, REGON: 122581850, zarejestrowana w Sądzie Rejonowym dla Krakowa
Śródmieścia XI Wydział KRS pod nr KRS: 0000421705, Kapitał zakładowy 50 000
zł (słownie: pięćdziesiąt tysięcy złotych) w całości wpłacony. Klauzula
Bezpieczeństwa: treść tej wiadomości wraz z załącznikami stanowią
informacje chronione przed ujawnieniem. Jeśli wiadomość ta nie jest
przeznaczona dla Ciebie uprzedzamy, że ujawnianie, kopiowanie,
rozpowszechnianie lub korzystanie z niej lub z załączników jest zabronione.
Jeśli otrzymałeś tę wiadomość przez pomyłkę, uprzejmie prosimy o
niezwłoczne zawiadomienie nadawcy i odesłanie jej z powrotem wraz z
załącznikami a także usunięcie ze swoich systemów.

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/CAKfpvwc0%2BbEsm756RSk%3D%3DCFU-uwUprna1SSaeQAMj8VzdWhJQg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
def handleStatus(key, status) {
    if (status) {
        currentBuild.result = 'SUCCESS'
    } else {
        currentBuild.result = 'FAILED'
    }
}

pipeline {
    agent { label 'slave-1' }
    options {
        disableConcurrentBuilds()
    }
    stages {
        stage('Setup build variables') {
            steps {
                script {
                    currentBuild.displayName = env.CHANGE_ID ? 
                        "PR-${env.CHANGE_ID}_${env.BUILD_NUMBER}" : 
                        "PR-${env.BRANCH_NAME}_${env.BUILD_NUMBER}"

                    currentBuild.description = env.CHANGE_ID ?
                        "Cordeo-api build #${env.BUILD_NUMBER} of pull request 
#${env.CHANGE_ID}" : 
                        "Cordeo-api build #${env.BUILD_NUMBER} of branch 
${env.BRANCH_NAME}"
                }
            }
        }
        stage('Prepare Env') {
            steps {    
                configFileProvider([configFile(fileId: 'master-key', variable: 
'MASTER_KEY')]) {
                    sh "mkdir -p ${WORKSPACE}/config"
                    sh "cp ${MASTER_KEY} ${WORKSPACE}/config/master.key"
                }
                sh "bundle install"
                sh "bundle exec rake db:migrate RAILS_ENV=test"
                
            }
            post {
                success {
                    handleStatus(STAGE_NAME, true);
                }
                failure {
                    handleStatus(STAGE_NAME, false);   
                }
            }
        }

        stage('Invoke Rake') {
            steps {
                sh "bundle exec rake rspec spec"
                junit 'spec/reports/*.xml'
            }
            post {
                always {
                    archiveArtifacts 'coverage/*'
                    publishHTML (target: [
                          allowMissing: false,
                          alwaysLinkToLastBuild: false,
                          keepAll: true,
                          reportDir: 'coverage',
                          reportFiles: 'index.html',
                          reportName: "Test Coverage Report"
                    ])
                }
                success {
                    handleStatus(STAGE_NAME, true);
                }
                failure {
                    handleStatus(STAGE_NAME, false);   
                }
            }
        }

        stage('Rubycritic') {
            steps {
                sh "rubycritic app lib --path spec/rubycritic"
            }
            post {
                success {
                    handleStatus(STAGE_NAME, true);
                }
                failure {
                    handleStatus(STAGE_NAME, false);   
                }
                always {
                    archiveArtifacts 'spec/rubycritic/*'
                    publishHTML (target: [
                          allowMissing: false,
                          alwaysLinkToLastBuild: false,
                          keepAll: true,
                          reportDir: 'spec/rubycritic',
                          reportFiles: 'overview.html',
                          reportName: "Rubycritic Report"
                    ])
                }
            }
        }


        // stage('Brakeman') {
        //     steps {
        //         sh "brakeman -o spec/brakeman.html"
        //     }
        //     post {
        //         success {
        //             handleStatus(STAGE_NAME, true);
        //         }
        //         failure {
        //             handleStatus(STAGE_NAME, false);
        //         }
        //         always {
        //             archiveArtifacts 'spec/brakeman.html'
        //             publishHTML (target: [
        //                   allowMissing: false,
        //                   alwaysLinkToLastBuild: false,
        //                   keepAll: true,
        //                   reportDir: 'spec',
        //                   reportFiles: 'brakeman.html',
        //                   reportName: "Brakeman Report"
        //             ])
        //         }
        //     }
        // }

        stage("Build docker image") {
            steps {
                sh "docker build . --build-arg MVN_USERNAME=${env.MVN_USERNAME} 
--build-arg MVN_PASSWORD=${env.MVN_PASSWORD} -t 
adres_repo/cordeo-api:${currentBuild.id} -t adres_repo/cordeo-api:latest"
            }
            post {
                success {
                    handleStatus(STAGE_NAME, true);
                }
                failure {
                    handleStatus(STAGE_NAME, false);   
                }
            }
        }

        stage("Publish docker image") {
            steps {
                withDockerRegistry([url: "https://adres_repo";, credentialsId: 
"identyfikator"]) {
                    sh "docker push adres_repo/cordeo-api:${currentBuild.id}"
                    sh "docker push adres_repo/cordeo-api:latest"
                }
            }
            post {
                success {
                    handleStatus(STAGE_NAME, true);
                }
                failure {
                    handleStatus(STAGE_NAME, false);   
                }
            }
        }
    }

    // 
https://support.cloudbees.com/hc/en-us/articles/226419147-How-can-I-check-previous-build-status-in-a-Pipeline-Script-
    post {
        changed {
            script {
                if (currentBuild.result == 'SUCCESS') {
                    slackSend (color: '#00FF00', message: "SUCCESSFUL: Job 
'${env.JOB_NAME} [${currentBuild.displayName}]' (${env.BUILD_URL})")
                } else {
                    slackSend (color: '#FF0000', message: "FAILED: Job 
'${env.JOB_NAME} [${currentBuild.displayName}]' (${env.BUILD_URL})")
                }
            }
        }
    }
}

Reply via email to