1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 # bash function to set up jenkins sandbox environment
16 # See LF Sandbox documentation:
17 # https://docs.releng.linuxfoundation.org/en/latest/jenkins-sandbox.html
20 # 1. Create jenkins sandbox token and add it to your local jenkins.ini file
21 # Either specify the location of the init file in $JENKINS_INI or
22 # JENKINS_INI will be initialized to either
23 # ~/.config/jenkins_jobs/jenkins.ini
24 # $WS_ROOT/jenkins.ini
25 # 2. Clone ci-management workspace from gerrit.fd.io
26 # 3. export WS_ROOT=<local ci-management workspace>
28 # 5. git submodule update --init --recursive
32 local jjb_version=${JJB_VERSION:-"5.0.2"}
34 if [ -z "$WS_ROOT" ] ; then
35 echo "ERROR: WS_ROOT is not set!"
37 elif [ ! -d "$WS_ROOT/jjb" ] ; then
38 echo "ERROR: WS_ROOT is not set to a ci-management workspace:"
43 if [ -n "$(declare -f deactivate)" ]; then
44 echo "Deactivating Python Virtualenv!"
48 if [ -z "$JENKINS_INI" ] ; then
49 local user_jenkins_ini="/home/$USER/.config/jenkins_jobs/jenkins.ini"
50 if [ -f "$user_jenkins_ini" ] ; then
51 export JENKINS_INI=$user_jenkins_ini
52 elif [ -f "$WS_ROOT/jenkins.ini" ] ; then
53 export JENKINS_INI="$WS_ROOT/jenkins.ini"
55 echo "ERROR: Unable to find 'jenkins.ini'!"
58 echo "Exporting JENKINS_INI=$JENKINS_INI"
59 elif [ ! -f "$JENKINS_INI" ] ; then
60 echo "ERROR: file specified in JENKINS_INI ($JENKINS_INI) not found!"
64 if [ -n "$(declare -f deactivate)" ]; then
65 echo "Deactivating Python Virtualenv!"
69 git submodule update --init --recursive
71 local VENV_DIR=$WS_ROOT/venv
73 && python3 -m venv $VENV_DIR \
74 && source $VENV_DIR/bin/activate \
75 && pip3 install --upgrade pip \
76 && pip3 install wheel \
77 && pip3 install jenkins-job-builder==$jjb_version
79 alias jjsb='jenkins-jobs --conf $JENKINS_INI'
80 function jjsb-test() {
81 if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
82 echo "jenkins-jobs not found! Run jjb-sandbox-env to activate."
85 local grep_job_names=""
86 if [ "$1" = "-n" ] ; then
91 echo "Usage: $FUNCNAME <jenkins-job-name>"
94 if [ -z "$grep_job_names" ]; then
95 jenkins-jobs --conf $JENKINS_INI test $WS_ROOT/jjb $@
97 jenkins-jobs --conf $JENKINS_INI test $WS_ROOT/jjb $@ 2>&1 | grep -e'Number of jobs' -e'Job name' | sed -e 's/INFO:jenkins_jobs.builder://g'
100 function jjsb-update() {
101 if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
102 echo "jenkins-jobs not found! Run jjb-sandbox-env to activate."
105 local grep_num_jobs=""
106 if [ "$1" = "-n" ] ; then
110 if [ -z "$1" ] ; then
111 echo "Usage: $FUNCNAME <jenkins-job-name>"
114 if [ -z "grep_num_jobs" ] ; then
115 jenkins-jobs --conf $JENKINS_INI update $WS_ROOT/jjb $@
117 jenkins-jobs --conf $JENKINS_INI update $WS_ROOT/jjb $@ 2>&1 | grep -e'Number of jobs'
121 jenkins-jobs --version
124 # Get the refspec for the specified project branch at HEAD
128 # $2 - project (Optional: defaults to 'vpp')
129 get_gerrit_refspec() {
130 local branch=${1:-"master"}
131 local project=${2:-"vpp"}
132 local query="$(ssh -p 29418 gerrit.fd.io gerrit query status:merged project:$project branch:$branch limit:1 --format=JSON --current-patch-set | tr ',' '\n' | grep refs | cut -d'"' -f4)"
134 if [ -z "$query" ] ; then
135 echo "ERROR: Invalid argument(s): branch ($1) project ($2)"
136 echo "Usage: $0 <branch> <project>"