Merge "Move dlux Sonar scan to Sonarcloud"
[releng/builder.git] / jjb / packaging / build-rpm.sh
1 #!/bin/bash
2
3 # Options:
4 #   -x: Echo commands
5 #   -e: Fail on errors
6 #   -o pipefail: Fail on errors in scripts this calls, give stacktrace
7 set -ex -o pipefail
8
9 # Install required packages
10 virtualenv rpm_build
11 # shellcheck disable=SC1091
12 source rpm_build/bin/activate
13 PYTHON="rpm_build/bin/python"
14 $PYTHON -m pip install --upgrade pip
15 $PYTHON -m pip install -r "$WORKSPACE/packaging/packages/requirements.txt"
16
17 # Verify artifact at DOWNLOAD_URL exists
18 # shellcheck disable=SC2154
19 url_status=$(curl --silent --head --location --output /dev/null --write-out \
20   '%{http_code}' "$DOWNLOAD_URL")
21 if [[ $url_status = 2* ]]; then
22   echo "Artifact at DOWNLOAD_URL exists"
23 else
24   echo "Artifact at DOWNLOAD_URL does not exist"
25   exit 1
26 fi
27
28
29 # Packaging logic needs a tarball, but can repackage a zip into tar.gz
30 # if needed. All builds except multipatch-test publish both a tar.gz and zip.
31 # Autorelease passes DOWNLOAD_URL to zip, others typically use tar.gz.
32 # If URL is to zip, check if there's a tar.gz available to avoid repackaging.
33 if [[ $DOWNLOAD_URL = *.zip ]]; then
34   # shellcheck disable=SC2154
35   candidate_tarball_url="${DOWNLOAD_URL//zip/tar.gz}"
36   # shellcheck disable=SC2154
37   url_status=$(curl --silent --head --location --output /dev/null --write-out \
38     '%{http_code}' "$candidate_tarball_url")
39   if [[ $url_status = 2* ]]; then
40     DOWNLOAD_URL="$candidate_tarball_url"
41   fi
42 fi
43
44 # Build release specified by build params
45 "$WORKSPACE/packaging/packages/build.py" --rpm \
46                                          --changelog_name "$CHANGELOG_NAME" \
47                                          --changelog_email "$CHANGELOG_EMAIL" \
48                                          direct \
49                                          --download_url "$DOWNLOAD_URL" \
50                                          --pkg_version "$PKG_VERSION_OVERRIDE"
51
52 # Always allow push to scratch repos, only push to CD repos in RelEng Jenkins
53 if [ "$DEPLOY_TO_REPO" == "opendaylight-epel-7-x86_64-devel" ]; then
54   # Move RPMs (SRPM and noarch) to dir of files that will be uploaded to Nexus
55   UPLOAD_FILES_PATH="$WORKSPACE/upload_files"
56   mkdir -p "$UPLOAD_FILES_PATH"
57   cp "$HOME/rpmbuild/RPMS/noarch/"*.rpm "$_"
58   cp "$HOME/rpmbuild/SRPMS/"*.rpm "$_"
59 else
60   # Publish RPMs to CD repos if in production Jenkins, not in sandbox Jenkins
61   if [ "$SILO" == "sandbox" ]; then
62     echo "Not uploading RPMs to Nexus because running in sandbox"
63   elif  [ "$SILO" == "releng" ]; then
64     # Move RPMs (SRPM+noarch) to dir of files that will be uploaded to Nexus
65     UPLOAD_FILES_PATH="$WORKSPACE/upload_files"
66     mkdir -p "$UPLOAD_FILES_PATH"
67     cp "$HOME/rpmbuild/RPMS/noarch/"*.rpm "$_"
68     cp "$HOME/rpmbuild/SRPMS/"*.rpm "$_"
69   else
70     echo "Unknown Jenkins silo: $SILO"
71     exit 1
72   fi
73 fi