Create job to manage Jenkins Global Variables
[releng/global-jjb.git] / shell / jenkins-configure-global-vars.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 # Pulls global variable definitions out of a file.
12 #
13 # Configuration is read from $WORKSPACE/jenkins-config/global-vars-$silo.sh
14 #
15 # Requirements: lftools must be installed to /tmp/v/lftools
16 # Parameters:
17 #     jenkins_silos:  Space separated list of Jenkins silos to push global-vars
18 #                     configuration to. (default: jenkins)
19 echo "---> jenkins-configure-global-vars.sh"
20
21 GROOVY_SCRIPT_FILE="jjb/global-jjb/jenkins-admin/set_global_properties.groovy"
22
23 # shellcheck source=/tmp/v/lftools/bin/activate disable=SC1091
24 source "/tmp/v/lftools/bin/activate"
25 silos="${jenkins_silos:-jenkins}"
26
27 set -eu -o pipefail
28
29 for silo in $silos; do
30     set +x  # Ensure that no other scripts add `set -x` and print passwords
31     echo "Configuring $silo"
32
33     JENKINS_URL=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" url)
34     JENKINS_USER=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" user)
35     JENKINS_PASSWORD=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" password)
36     export JENKINS_URL
37     export JENKINS_USER
38     export JENKINS_PASSWORD
39
40     global_vars="$WORKSPACE/jenkins-config/global-vars-$silo.sh"
41
42     if [ ! -f "$global_vars" ]; then
43         echo "ERROR: Configuration file $global_vars not found."
44         exit 1
45     fi
46
47     mapfile -t vars < <(cat $global_vars)
48
49     rm -f insert.txt
50     for var in "${vars[@]}"; do
51         # Ignore comments and blank lines
52         if [[ $var == '#'* ]] || [ -z "$var" ]; then
53             continue
54         fi
55
56         key=$(echo $var | cut -d\= -f1)
57         value=$(echo $var | cut -d\= -f2)
58         echo "    '$key': '$value'," >> insert.txt
59     done
60
61     # Insert variables and remove first occurrence of JENKINS_URL variable
62     echo "-----> script.groovy"
63     sed "/'JENKINS_URL'/r insert.txt" "$GROOVY_SCRIPT_FILE" \
64         | sed "0,/'JENKINS_URL'/{/'JENKINS_URL'/d}" \
65         > script.groovy
66     cat script.groovy
67
68     lftools jenkins groovy script.groovy
69 done