xci: Fix extracting scenarios for generic scenarios
[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 # ensure GERRIT_TOPIC is set
21 GERRIT_TOPIC="${GERRIT_TOPIC:-''}"
22 # skip the healthcheck if the patch doesn't impact the deployment
23 if [[ "$GERRIT_TOPIC" =~ skip-verify|skip-deployment ]]; then
24     echo "Skipping the healthcheck!"
25     exit 0
26 fi
27
28 WORK_DIRECTORY=/tmp/$GERRIT_CHANGE_NUMBER/$DISTRO
29 /bin/rm -rf $WORK_DIRECTORY && mkdir -p $WORK_DIRECTORY
30
31 # if change is coming to releng-xci, continue as usual until that part is fixed as well
32 if [[ $GERRIT_PROJECT == "releng-xci" ]]; then
33     # save the scenario name into java properties file to be injected to downstream jobs via envInject
34     echo "Recording scenario name for downstream jobs"
35     echo "DEPLOY_SCENARIO=os-nosdn-nofeature" > $WORK_DIRECTORY/scenario.properties
36     exit 0
37 fi
38
39 # projects develop different scenarios and jobs need to know which scenario got the
40 # change under test so the jobs can deploy and test the right scenario.
41 # we need to fetch the change and look at the changeset to find out the scenario instead
42 # of hardcoding scenario per project.
43
44 git clone https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $WORK_DIRECTORY/$GERRIT_PROJECT
45 cd $WORK_DIRECTORY/$GERRIT_PROJECT
46 git fetch https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $GERRIT_REFSPEC && git checkout FETCH_HEAD
47 DEPLOY_SCENARIO=$(git diff HEAD^..HEAD --name-only | grep scenarios | sed -r 's/scenarios\/(.*?)\/role.*/\1/' | uniq)
48
49 # ensure single scenario is impacted
50 if [[ $(echo $DEPLOY_SCENARIO | wc -w) != 1 ]]; then
51     echo "Change impacts multiple scenarios!"
52     echo "XCI doesn't support testing of changes that impact multiple scenarios currently."
53     echo "Please split your change into multiple different/dependent changes, each modifying single scenario."
54     exit 1
55 fi
56
57 # save the scenario name into java properties file to be injected to downstream jobs via envInject
58 echo "Recording scenario name for downstream jobs"
59 echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" > $WORK_DIRECTORY/scenario.properties
60
61 # skip the deployment if the scenario is not supported on this distro
62 OPNFV_SCENARIO_REQUIREMENTS=$WORKSPACE/xci/opnfv-scenario-requirements.yml
63 if ! sed -n "/^- scenario: $DEPLOY_SCENARIO$/,/^$/p" $OPNFV_SCENARIO_REQUIREMENTS | grep -q $DISTRO; then
64     echo "# SKIPPED: Scenario $DEPLOY_SCENARIO is NOT supported on $DISTRO"
65     exit 0
66 fi