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>
29 local jjb_version=${JJB_VERSION:-"3.5.0"}
31 if [ -z "$WS_ROOT" ] ; then
32 echo "ERROR: WS_ROOT is not set!"
34 elif [ ! -d "$WS_ROOT/jjb" ] ; then
35 echo "ERROR: WS_ROOT is not set to a ci-management workspace:"
40 if [ -n "$(declare -f deactivate)" ]; then
41 echo "Deactivating Python Virtualenv!"
45 if [ -z "$JENKINS_INI" ] ; then
46 local user_jenkins_ini="/home/$USER/.config/jenkins_jobs/jenkins.ini"
47 if [ -f "$user_jenkins_ini" ] ; then
48 export JENKINS_INI=$user_jenkins_ini
49 elif [ -f "$WS_ROOT/jenkins.ini" ] ; then
50 export JENKINS_INI="$WS_ROOT/jenkins.ini"
52 echo "ERROR: Unable to find 'jenkins.ini'!"
55 echo "Exporting JENKINS_INI=$JENKINS_INI"
56 elif [ ! -f "$JENKINS_INI" ] ; then
57 echo "ERROR: file specified in JENKINS_INI ($JENKINS_INI) not found!"
61 if [ -n "$(declare -f deactivate)" ]; then
62 echo "Deactivating Python Virtualenv!"
66 git submodule update --init --recursive
68 local VENV_DIR=$WS_ROOT/venv
70 && python3 -m venv $VENV_DIR \
71 && source $VENV_DIR/bin/activate \
72 && pip3 install wheel \
73 && pip3 install jenkins-job-builder==$jjb_version
75 alias jjsb='jenkins-jobs --conf $JENKINS_INI'
76 function jjsb-test() {
77 if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
78 echo "jenkins-jobs not found! Run jjb-sandbox-env to activate."
82 echo "Usage: $FUNCNAME <jenkins-job-name>"
86 && jenkins-jobs --conf $JENKINS_INI test $WS_ROOT/jjb $@
88 function jjsb-update() {
89 if [ -z "$(which jenkins-jobs 2>&1)" ] ; then
90 echo "jenkins-jobs not found! Run jjb-sandbox-env to activate."
94 echo "Usage: $FUNCNAME <jenkins-job-name>"
98 && jenkins-jobs --conf $JENKINS_INI update $WS_ROOT/jjb $@
100 jenkins-jobs --version
103 # Get the refspec for the specified project branch at HEAD
107 # $2 - project (Optional: defaults to 'vpp')
108 get_gerrit_refspec() {
109 local branch=${1:-"master"}
110 local project=${2:-"vpp"}
111 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)"
113 if [ -z "$query" ] ; then
114 echo "ERROR: Invalid project ($1) or branch ($2)"