├── Jenkinsfile ├── Jenkinsfile.realExample.groovy └── README.md /Jenkinsfile: -------------------------------------------------------------------------------- 1 | node { 2 | // Clean workspace before doing anything 3 | deleteDir() 4 | 5 | try { 6 | stage ('Clone') { 7 | checkout scm 8 | } 9 | stage ('Build') { 10 | sh "echo 'shell scripts to build project...'" 11 | } 12 | stage ('Tests') { 13 | parallel 'static': { 14 | sh "echo 'shell scripts to run static tests...'" 15 | }, 16 | 'unit': { 17 | sh "echo 'shell scripts to run unit tests...'" 18 | }, 19 | 'integration': { 20 | sh "echo 'shell scripts to run integration tests...'" 21 | } 22 | } 23 | stage ('Deploy') { 24 | sh "echo 'shell scripts to deploy to server...'" 25 | } 26 | } catch (err) { 27 | currentBuild.result = 'FAILED' 28 | throw err 29 | } 30 | } 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Jenkinsfile.realExample.groovy: -------------------------------------------------------------------------------- 1 | node { 2 | def PROJECT_NAME = "project_name" 3 | 4 | // Clean workspace before doing anything 5 | deleteDir() 6 | 7 | propertiesData = [disableConcurrentBuilds()] 8 | if (isValidDeployBranch()) { 9 | propertiesData = propertiesData + parameters([ 10 | choice(choices: 'none\nIGR\nPRD', description: 'Target server to deploy', name: 'deployServer'), 11 | ]) 12 | } 13 | properties(propertiesData) 14 | 15 | try { 16 | stage ('Clone') { 17 | checkout scm 18 | } 19 | stage ('preparations') { 20 | try { 21 | deploySettings = getDeploySettings() 22 | echo 'Deploy settings were set' 23 | } catch(err) { 24 | println(err.getMessage()); 25 | throw err 26 | } 27 | } 28 | stage('Build') { 29 | sh "mg2-builder install -Dproject.name=${PROJECT_NAME} -Dproject.environment=local -Dinstall.type=clean -Ddatabase.admin.username=${env.DATABASE_USER} -Ddatabase.admin.password=${env.DATABASE_PASS} -Denvironment.server.type=none" 30 | } 31 | stage ('Tests') { 32 | parallel 'static': { 33 | sh "bin/grumphp run --testsuite=magento2testsuite" 34 | }, 35 | 'unit': { 36 | sh "magento/bin/magento dev:test:run unit" 37 | }, 38 | 'integration': { 39 | sh "magento/bin/magento dev:test:run integration" 40 | } 41 | } 42 | if (deploySettings) { 43 | stage ('Deploy') { 44 | if (deploySettings.type && deploySettings.version) { 45 | // Deploy specific version to a specifc server (IGR or PRD) 46 | sh "mg2-builder release:finish -Drelease.type=${deploySettings.type} -Drelease.version=${deploySettings.version}" 47 | sh "ssh ${deploySettings.ssh} 'mg2-deployer release -Drelease.version=${deploySettings.version}'" 48 | notifyDeployedVersion(deploySettings.version) 49 | } else { 50 | // Deploy to develop branch into IGR server 51 | sh "ssh ${deploySettings.ssh} 'mg2-deployer release'" 52 | } 53 | } 54 | } 55 | } catch (err) { 56 | currentBuild.result = 'FAILED' 57 | notifyFailed() 58 | throw err 59 | } 60 | } 61 | 62 | def isValidDeployBranch() { 63 | branchDetails = getBranchDetails() 64 | if (branchDetails.type == 'hotfix' || branchDetails.type == 'release') { 65 | return true 66 | } 67 | return false 68 | } 69 | 70 | def getBranchDetails() { 71 | def branchDetails = [:] 72 | branchData = BRANCH_NAME.split('/') 73 | if (branchData.size() == 2) { 74 | branchDetails['type'] = branchData[0] 75 | branchDetails['version'] = branchData[1] 76 | return branchDetails 77 | } 78 | return branchDetails 79 | } 80 | 81 | def getDeploySettings() { 82 | def deploySettings = [:] 83 | if (BRANCH_NAME == 'develop') { 84 | deploySettings['ssh'] = "user@domain-igr.com" 85 | } else if (params.deployServer && params.deployServer != 'none') { 86 | branchDetails = getBranchDetails() 87 | deploySettings['type'] = branchDetails.type 88 | deploySettings['version'] = branchDetails.version 89 | if (params.deployServer == 'PRD') { 90 | deploySettings['ssh'] = "user@domain-prd.com" 91 | } else if (params.deployServer == 'IGR') { 92 | deploySettings['ssh'] = "user@domain-igr.com" 93 | } 94 | } 95 | return deploySettings 96 | } 97 | 98 | def notifyDeployedVersion(String version) { 99 | emailext ( 100 | subject: "Deployed: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", 101 | body: "DEPLOYED VERSION '${version}': Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]': Check console output at '${env.BUILD_URL}' [${env.BUILD_NUMBER}]", 102 | to: "some-email@some-domain.com" 103 | ) 104 | } 105 | 106 | def notifyFailed() { 107 | emailext ( 108 | subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", 109 | body: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]': Check console output at '${env.BUILD_URL}' [${env.BUILD_NUMBER}]", 110 | to: "some-email@some-domain.com" 111 | ) 112 | } 113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jenkinsfile basic sample 2 | 3 | Jenkinsfile basic configuration example with stages definition to set up a Continuous Delivery Pipeline. 4 | 5 | This repository has was created to provide an example for the following tutorial about how to set up Jenkins Pipelines: 6 | 7 | * [Setup Continuos Integration/Delivery system in just 4 steps with Jenkins Pipelines and Blue Ocean](https://dev.to/jalogut/setup-continuos-integrationdelivery-system-in-just-4-steps-with-jenkins-pipelines-and-blue-ocean) 8 | --------------------------------------------------------------------------------