From 55abf20645ba6edc6ca39e77a7fc0358932b9f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Thu, 11 Apr 2019 13:29:37 +0200 Subject: [PATCH] modify .env settings using Jenkins params MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski Signed-off-by: Igor Mandrigin --- ci/Jenkinsfile.android | 2 +- ci/Jenkinsfile.combined | 1 - ci/Jenkinsfile.ios | 2 +- ci/android.groovy | 9 +++++---- ci/common.groovy | 17 +++++------------ ci/ios.groovy | 16 +++++++--------- ci/jenkins.groovy | 30 ++++++++++++++++++++++-------- ci/utils.groovy | 27 ++++++++++++++++++++++++++- 8 files changed, 67 insertions(+), 37 deletions(-) diff --git a/ci/Jenkinsfile.android b/ci/Jenkinsfile.android index 2952858bc0..b8e87d4d85 100644 --- a/ci/Jenkinsfile.android +++ b/ci/Jenkinsfile.android @@ -80,7 +80,7 @@ pipeline { } stage('Bundle') { steps { - script { apk = mobile.android.bundle(btype) } + script { apk = mobile.android.bundle() } } } stage('Archive') { diff --git a/ci/Jenkinsfile.combined b/ci/Jenkinsfile.combined index 9116a4d714..6d8584028d 100644 --- a/ci/Jenkinsfile.combined +++ b/ci/Jenkinsfile.combined @@ -14,7 +14,6 @@ pipeline { )) } - stages { stage('Prep') { steps { script { diff --git a/ci/Jenkinsfile.ios b/ci/Jenkinsfile.ios index 0ec5fbfb0f..0ca612a4f8 100644 --- a/ci/Jenkinsfile.ios +++ b/ci/Jenkinsfile.ios @@ -65,7 +65,7 @@ pipeline { } stage('Bundle') { steps { - script { api = mobile.ios.bundle(btype) } + script { api = mobile.ios.bundle() } } } stage('Archive') { diff --git a/ci/android.groovy b/ci/android.groovy index 48ce13fdd1..078e6d31fa 100644 --- a/ci/android.groovy +++ b/ci/android.groovy @@ -1,14 +1,15 @@ utils = load 'ci/utils.groovy' -def bundle(type = 'nightly') { +def bundle() { + def btype = utils.getBuildType() /* Disable Gradle Daemon https://stackoverflow.com/questions/38710327/jenkins-builds-fail-using-the-gradle-daemon */ def gradleOpt = "-PbuildUrl='${currentBuild.absoluteUrl}' -Dorg.gradle.daemon=false " def target = "release" - if (type in ['pr', 'e2e']) { + if (btype in ['pr', 'e2e']) { /* PR builds shouldn't replace normal releases */ target = 'pr' - } else if (type == 'release') { + } else if (btype == 'release') { gradleOpt += "-PreleaseVersion='${utils.getVersion('mobile_files')}'" } dir('android') { @@ -28,7 +29,7 @@ def bundle(type = 'nightly') { } sh 'find android/app/build/outputs/apk' def outApk = "android/app/build/outputs/apk/${target}/app-${target}.apk" - def pkg = utils.pkgFilename(type, 'apk') + def pkg = utils.pkgFilename(btype, 'apk') /* rename for upload */ sh "cp ${outApk} ${pkg}" /* necessary for Fastlane */ diff --git a/ci/common.groovy b/ci/common.groovy index dbd4df4164..758ce61093 100644 --- a/ci/common.groovy +++ b/ci/common.groovy @@ -53,22 +53,15 @@ def prepNixEnvironment() { } def prep(type = 'nightly') { + /* build/downloads all nix deps in advance */ prepNixEnvironment() - + /* rebase unless this is a release build */ utils.doGitRebase() /* ensure that we start from a known state */ sh 'make clean' - /* select type of build */ - switch (type) { - case 'nightly': - sh 'cp .env.nightly .env'; break - case 'release': - sh 'cp .env.prod .env'; break - case 'e2e': - sh 'cp .env.e2e .env'; break - default: - sh 'cp .env.jenkins .env'; break - } + /* pick right .env and update from params */ + utils.updateEnv(type) + if (env.TARGET_PLATFORM == 'android' || env.TARGET_PLATFORM == 'ios') { /* Run at start to void mismatched numbers */ utils.genBuildNumber() diff --git a/ci/ios.groovy b/ci/ios.groovy index 25a8d77b23..80b99f32ff 100644 --- a/ci/ios.groovy +++ b/ci/ios.groovy @@ -6,12 +6,10 @@ def plutil(name, value) { """ } -def bundle(type) { - if (!type) { - type = utils.getBuildType() - } +def bundle() { + def btype = utils.getBuildType() def target - switch (type) { + switch (btype) { case 'release': target = 'release'; break; case 'testflight': target = 'release'; break; case 'e2e': target = 'e2e'; break; @@ -39,14 +37,14 @@ def bundle(type) { } /* rename built file for uploads and archivization */ def pkg = '' - if (type == 'release') { + if (btype == 'release') { pkg = utils.pkgFilename('release', 'ipa') sh "cp status_appstore/StatusIm.ipa ${pkg}" - } else if (type == 'e2e') { + } else if (btype == 'e2e') { pkg = utils.pkgFilename('e2e', 'app.zip') sh "cp status-e2e/StatusIm.app.zip ${pkg}" - } else if (type != 'testflight') { - pkg = utils.pkgFilename(type, 'ipa') + } else if (btype != 'testflight') { + pkg = utils.pkgFilename(btype, 'ipa') sh "cp status-adhoc/StatusIm.ipa ${pkg}" } /* necessary for Diawi upload */ diff --git a/ci/jenkins.groovy b/ci/jenkins.groovy index 2c0fb67745..dd10cbec71 100644 --- a/ci/jenkins.groovy +++ b/ci/jenkins.groovy @@ -28,21 +28,35 @@ def abortPreviousRunningBuilds() { } } -def Build(name = null, buildType = null) { +def Build(name = null) { + /** + * Generate parameters to pass from current params + * This allows utils.updateEnv() to work in sub-jobs + **/ + parameters = params.keySet().collectEntries { key -> + [(key): [ + name: key, + value: params.get(key), + $class: 'StringParameterValue' + ]] + } /* default to current build type */ - buildType = buildType ? buildType : utils.getBuildType() + parameters['BUILD_TYPE'].value = utils.getBuildType() /* need to drop origin/ to match definitions of child jobs */ - def branchName = utils.branchName() + parameters['BRANCH'].value = utils.branchName() + /* necessary for updating GitHub PRs */ + parameters['CHANGE_ID'] = [ + name: 'CHANGE_ID', + value: env.CHANGE_ID, + $class: 'StringParameterValue' + ] /* always pass the BRANCH and BUILD_TYPE params with current branch */ def b = build( job: name, /* this allows us to analize the job even after failure */ propagate: false, - parameters: [ - [name: 'BRANCH', value: branchName, $class: 'StringParameterValue'], - [name: 'BUILD_TYPE', value: buildType, $class: 'StringParameterValue'], - [name: 'CHANGE_ID', value: env.CHANGE_ID, $class: 'StringParameterValue'], - ]) + parameters: parameters.values() + ) /* BlueOcean seems to not show child-build links */ println "Build: ${b.getAbsoluteUrl()} (${b.result})" if (b.result != 'SUCCESS') { diff --git a/ci/utils.groovy b/ci/utils.groovy index 3e81cfc749..ecc82d86ae 100644 --- a/ci/utils.groovy +++ b/ci/utils.groovy @@ -154,7 +154,7 @@ def getBuildType() { if (jobName.contains('e2e')) { return 'e2e' } - if (jobName.startsWith('status-react/pull requests')) { + if (jobName.startsWith('status-react/prs')) { return 'pr' } if (jobName.startsWith('status-react/nightly')) { @@ -185,4 +185,29 @@ def changeId() { return changeId } +def updateEnv(type) { + def envFile = "${env.WORKSPACE}/.env" + /* select .env based on type of build */ + def selectedEnv = '.env.jenkins' + switch (type) { + case 'nightly': selectedEnv = '.env.nightly'; break + case 'release': selectedEnv = '.env.prod'; break + case 'e2e': selectedEnv = '.env.e2e'; break + } + sh "cp ${selectedEnv} .env" + /* find a list of .env settings to check for them in params */ + def envContents = readFile(envFile) + def envLines = envContents.split() + def envVars = envLines.collect { it.split('=').first() } + /* for each var available in params modify the .env file */ + envVars.each { var -> + if (params.get(var)) { /* var exists in params and is not empty */ + println("Changing setting: ${var}=${params.get(var)}") + sh "sed -i'.bkp' 's/${var}=.*/${var}=${params.get(var)}/' ${envFile}" + } + } + /* show contents for debugging purposes */ + sh "cat ${envFile}" +} + return this