1bd6fd0c565d3870ad37975798e346a3e9fa92d7
[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 # if the scenario is external, we need to wipe WORKSPACE to place releng-xci there since
29 # the project where the scenario is coming from is cloned and the patch checked out to the
30 # xci/scenarios/$DEPLOY_SCENARIO to be synched on clean VM
31 # apart from that, we need releng-xci stuff in WORKSPACE for things to function correctly on Jenkins.
32 # if the change is coming to releng-xci, we don't need to do anything since the patch is checked
33 # out to the WORKSPACE anyways
34 if [[ $GERRIT_PROJECT != "releng-xci" ]]; then
35     cd $HOME && /bin/rm -rf $WORKSPACE
36     git clone https://gerrit.opnfv.org/gerrit/releng-xci $WORKSPACE && cd $WORKSPACE
37 fi
38
39 WORK_DIRECTORY=/tmp/$GERRIT_CHANGE_NUMBER/$DISTRO
40 /bin/rm -rf $WORK_DIRECTORY && mkdir -p $WORK_DIRECTORY
41
42 # if change is coming to releng-xci, continue as usual until that part is fixed as well
43 if [[ $GERRIT_PROJECT == "releng-xci" ]]; then
44     # save the scenario name into java properties file to be injected to downstream jobs via envInject
45     echo "Recording scenario name for downstream jobs"
46     echo "DEPLOY_SCENARIO=os-nosdn-nofeature" > $WORK_DIRECTORY/scenario.properties
47     exit 0
48 fi
49
50 # projects develop different scenarios and jobs need to know which scenario got the
51 # change under test so the jobs can deploy and test the right scenario.
52 # we need to fetch the change and look at the changeset to find out the scenario instead
53 # of hardcoding scenario per project.
54
55 git clone https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $WORK_DIRECTORY/$GERRIT_PROJECT
56 cd $WORK_DIRECTORY/$GERRIT_PROJECT
57 git fetch https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $GERRIT_REFSPEC && git checkout FETCH_HEAD
58 DEPLOY_SCENARIO=$(git diff HEAD^..HEAD --name-only | grep scenarios | sed -r 's/scenarios\/(.*?)\/.*/\1/' | uniq)
59
60 # ensure single scenario is impacted
61 if [[ $(echo $DEPLOY_SCENARIO | wc -w) != 1 ]]; then
62     echo "Change impacts multiple scenarios!"
63     echo "XCI doesn't support testing of changes that impact multiple scenarios currently."
64     echo "Please split your change into multiple different/dependent changes, each modifying single scenario."
65     exit 1
66 fi
67
68 # save the scenario name into java properties file to be injected to downstream jobs via envInject
69 echo "Recording scenario name '$DEPLOY_SCENARIO' for downstream jobs"
70 echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" > $WORK_DIRECTORY/scenario.properties
71
72 # skip the deployment if the scenario is not supported on this distro
73 OPNFV_SCENARIO_REQUIREMENTS=$WORKSPACE/xci/opnfv-scenario-requirements.yml
74 if ! sed -n "/^- scenario: $DEPLOY_SCENARIO$/,/^$/p" $OPNFV_SCENARIO_REQUIREMENTS | grep -q $DISTRO; then
75     echo "# SKIPPED: Scenario $DEPLOY_SCENARIO is NOT supported on $DISTRO"
76     exit 0
77 fi