debug = false stageName = '' git_link = '' script_git = 'https://git.psychobox.org/psycho/owrt_build_script.git' git_folder = 'openwrt' script_folder = 'owrt_build_script' config_path = script_folder + '/configs/' patch_path = script_folder + '/patches/' scripts_path = script_folder + '/scripts/' patch = patch_path + 'tl-wr841_16m.patch' sftp_host = '' sftp_user = '' sftp_path = '/jenkins/owrt/' isBuildNeeded = false deleteUnbuild = false force_build = false pre_clean = true post_clean = false build_node = 'linux' bin_path = 'bin' core_count_str = '$((`nproc` + 1))' statusCode = 0 mail_body = "${JOB_NAME} - Build # ${BUILD_NUMBER}:" device_conf = '' device = '' if(params.DEBUG) { debug = true } if(params.GIT) { git_link = params.GIT } if(params.SFTP_HOST) { sftp_host = params.SFTP_HOST } if(params.SFTP_USER) { sftp_user = params.SFTP_USER } if(params.SFTP_PATH) { sftp_path = params.SFTP_PATH } if(params.KNOWN_HOSTS) { known_hosts = params.KNOWN_HOSTS } if(params.DEL_UNBUILD) { deleteUnbuild = params.DEL_UNBUILD } if(params.PRE_CLEAN) { pre_clean = params.PRE_CLEAN } if(params.POST_CLEAN) { post_clean = params.POST_CLEAN } if(params.BIN_PATH) { bin_path = params.BIN_PATH } if(params.FORCE_BUILD) { force_build = params.FORCE_BUILD } if(params.BUILD_NODE) { build_node = params.BUILD_NODE } if(params.DEVICE_CONF) { device_conf = params.DEVICE_CONF } if(params.DEVICE) { device = params.DEVICE } def printDebug(msg) { println 'DEBUG: ' + msg } def add2mail(msg) { mail_body += '
' + msg + '
' } printDebug(currentBuild.id.toString()) printDebug(JOB_NAME) node(build_node) { try { checkout() if(isBuildNeeded || force_build) { pre_build() build() } } catch (e) { printDebug(e.getMessage()) add2mail(e.getMessage()) } finally { post_build() } } def checkout() { stage('checkout') { if(pre_clean) { cleanWs() } script = 'git clone ' + script_git + ' ' + script_folder printDebug(script) statusCode = sh label: 'checkout_configs', script: script, returnStatus:true if(statusCode != 0) { currentBuild.result = 'FAILURE' throw new Exception('ERROR: failed to check out scripts: ' + script_git) } script = 'test -f ' + config_path + device_conf printDebug(script) statusCode = sh label: 'check for config file', script: script, returnStatus: true if(statusCode != 0) { currentBuild.result = 'FAILURE' throw new Exception('ERROR: DEVICE_CONF: "' + device_conf + '" not found') } script = 'git clone ' + git_link + ' ' + git_folder printDebug(script) statusCode = sh label: 'checkout_openwrt', script: script, returnStatus:true if(statusCode != 0) { currentBuild.result = 'FAILURE' throw new Exception('ERROR: failed to check out OWRT: ' + git_link) } script = 'set +x; python ' + scripts_path + 'sftp_test.py -a ' + sftp_host + ' -u ' + sftp_user + ' -p ' + params.SFTP_PASSWD + ' -g ' + git_folder + ' -f ' + scripts_path + known_hosts + ' -l ' + sftp_path + '; set -x' script_print = 'set +x; python ' + scripts_path + 'sftp_test.py -a ' + sftp_host + ' -u ' + sftp_user + ' -p ************' + ' -g ' + git_folder + ' -f ' + scripts_path + known_hosts + ' -l ' + sftp_path + '; set -x' printDebug(script_print) def ret = sh label: 'is_build_needed', script: script, returnStdout: true if(ret.trim().equalsIgnoreCase('True')) { isBuildNeeded = true } else { printDebug('build is NOT necessary') } } } def pre_build() { stage('pre-build') { script = 'cd ' + git_folder + '; patch -p1 < ../' + patch printDebug(script) statusCode = sh label: 'patch_sources', script: script, returnStatus:true if(statusCode != 0) { currentBuild.result = 'FAILURE' throw new Exception('ERROR: failed patching sources') } script = 'cd ' + git_folder + '; ./scripts/feeds update -a' printDebug(script) statusCode = sh label: 'feeds_update', script: script, returnStatus:true if(statusCode != 0) { currentBuild.result = 'FAILURE' throw new Exception('ERROR: failed updating feeds') } script = 'cd ' + git_folder + '; ./scripts/feeds install -a' printDebug(script) statusCode = sh label: 'feeds_install', script: script, returnStatus:true if(statusCode != 0) { currentBuild.result = 'FAILURE' throw new Exception('ERROR: failed installing feeds') } } } def build() { stage('build') { script = 'cp ' + config_path + '/' + device_conf + ' ' + git_folder + '/.config' printDebug(script) sh label: 'copy_config', script: script if(statusCode != 0) { currentBuild.result = 'FAILURE' throw new Exception('ERROR: failed copying config: ' + device_conf) } script = 'cd ' + git_folder + '; make defconfig; make download; make -j$((`nproc` + 1))' printDebug(script) statusCode = sh label: 'build', script: script, returnStatus:true if(statusCode != 0) { add2mail('multicore failed') printDebug('multicore build failed -> trying to build verbosely on a single core') script = 'cd ' + git_folder + '; make defconfig; make download; make -j1 V=s' printDebug(script) statusCode = sh label: 'build_single_core', script: script, returnStatus:true if(statusCode != 0) { currentBuild.result = 'FAILURE' throw new Exception('ERROR: build process failed') } } } stage('move_bin_to_ftp') { script = 'python ' + scripts_path + 'sftp_test.py -a ' + sftp_host + ' -u ' + sftp_user + ' -p ' + params.SFTP_PASSWD + ' -g ' + git_folder + ' -f ' + scripts_path + known_hosts + ' -l ' + sftp_path + ' -c ' + bin_path printDebug(script) statusCode = sh label: 'copy_binaries', script: script, returnStatus:true if(statusCode != 0) { currentBuild.result = 'UNSTABLE' post_clean = false add2mail("Files not transferred to SFTP!") throw new Exception('WARNING: failed copying binaries') } } } def post_build() { if(post_clean) { cleanWs() } send_mail() if((!isBuildNeeded && !force_build && deleteUnbuild)) { //if no build is needed, then the last build is deleted and the next build number is set to previous build number def job = Jenkins.instance.getItem(JOB_NAME) job.getLastBuild().delete() job.save() } } def send_mail() { //def body_text = "${JOB_NAME} - Build # ${BUILD_NUMBER} - ${currentBuild.currentResult}:
Check console output at ${BUILD_URL} to view the results." add2mail("Overall status: ${currentBuild.result}!") add2mail("Check console output at ${BUILD_URL} to view the results.") emailext body: mail_body, subject: "${JOB_NAME} - Build # ${BUILD_NUMBER} - ${currentBuild.result}!", to: 'psychowoife@gmail.com' }