764857ba47ad058cb892a05e5605f04d30293297
[releng.git] / jjb / xci / xci-set-scenario.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018 SUSE and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 set -o errexit
11 set -o nounset
12 set -o pipefail
13
14 #----------------------------------------------------------------------
15 # This script is used by CI and executed by Jenkins jobs.
16 # You are not supposed to use this script manually if you don't know
17 # what you are doing.
18 #----------------------------------------------------------------------
19
20 WORK_DIRECTORY=/tmp/$GERRIT_CHANGE_NUMBER/$DISTRO
21 /bin/rm -rf $WORK_DIRECTORY && mkdir -p $WORK_DIRECTORY
22
23 # ensure GERRIT_TOPIC is set
24 GERRIT_TOPIC="${GERRIT_TOPIC:-''}"
25
26 # skip the healthcheck if the patch doesn't impact the deployment
27 if [[ "$GERRIT_TOPIC" =~ skip-verify|skip-deployment ]]; then
28     echo "Skipping verify!"
29     echo "DEPLOY_SCENARIO=os-nosdn-nofeature" > $WORK_DIRECTORY/scenario.properties
30     exit 0
31 fi
32
33 # if the scenario is external, we need to wipe WORKSPACE to place releng-xci there since
34 # the project where the scenario is coming from is cloned and the patch checked out to the
35 # xci/scenarios/$DEPLOY_SCENARIO to be synched on clean VM
36 # apart from that, we need releng-xci stuff in WORKSPACE for things to function correctly on Jenkins.
37 # if the change is coming to releng-xci, we don't need to do anything since the patch is checked
38 # out to the WORKSPACE anyways
39 if [[ $GERRIT_PROJECT != "releng-xci" ]]; then
40     cd $HOME && /bin/rm -rf $WORKSPACE
41     git clone https://gerrit.opnfv.org/gerrit/releng-xci $WORKSPACE && cd $WORKSPACE
42     chmod -R go-rwx $WORKSPACE/xci/scripts/vm
43 fi
44
45 # if change is coming to releng-xci, continue as usual until that part is fixed as well
46 if [[ $GERRIT_PROJECT == "releng-xci" ]]; then
47     # save the scenario name into java properties file to be injected to downstream jobs via envInject
48     echo "Recording scenario name for downstream jobs"
49     echo "DEPLOY_SCENARIO=os-nosdn-nofeature" > $WORK_DIRECTORY/scenario.properties
50     exit 0
51 fi
52
53 # projects develop different scenarios and jobs need to know which scenario got the
54 # change under test so the jobs can deploy and test the right scenario.
55 # we need to fetch the change and look at the changeset to find out the scenario instead
56 # of hardcoding scenario per project.
57
58 git clone https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $WORK_DIRECTORY/$GERRIT_PROJECT
59 cd $WORK_DIRECTORY/$GERRIT_PROJECT
60 git fetch https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $GERRIT_REFSPEC && git checkout FETCH_HEAD
61 DEPLOY_SCENARIO=$(git diff HEAD^..HEAD --name-only | grep scenarios | awk -F '[/|/]' '{print $2}' | uniq)
62
63 # ensure single scenario is impacted
64 if [[ $(echo $DEPLOY_SCENARIO | wc -w) != 1 ]]; then
65     echo "Change impacts multiple scenarios!"
66     echo "XCI doesn't support testing of changes that impact multiple scenarios currently."
67     echo "Please split your change into multiple different/dependent changes, each modifying single scenario."
68     exit 1
69 fi
70
71 # save the scenario name into java properties file to be injected to downstream jobs via envInject
72 echo "Recording scenario name '$DEPLOY_SCENARIO' for downstream jobs"
73 echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" > $WORK_DIRECTORY/scenario.properties
74
75 # skip the deployment if the scenario is not supported on this distro
76 OPNFV_SCENARIO_REQUIREMENTS=$WORKSPACE/xci/opnfv-scenario-requirements.yml
77 if ! sed -n "/^- scenario: $DEPLOY_SCENARIO$/,/^$/p" $OPNFV_SCENARIO_REQUIREMENTS | grep -q $DISTRO; then
78     echo "# SKIPPED: Scenario $DEPLOY_SCENARIO is NOT supported on $DISTRO"
79     exit 0
80 fi