Merge "Nexus retirement CI job clean up"
[ci-management.git] / jjb / scripts / packagecloud_push.sh
1 #!/bin/bash
2
3 # Copyright (c) 2021 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 echo "---> jjb/scripts/packagecloud_push.sh"
17
18 set -euxo pipefail
19
20 line="*************************************************************************"
21
22 # Nothing was built if this is a merge job being run when
23 # the git HEAD id is not the same as the Gerrit New Revision ID
24 if [[ ${JOB_NAME} == *merge* ]] && [ -n "${GERRIT_NEWREV:-}" ] &&
25        [ "$GERRIT_NEWREV" != "$GIT_COMMIT" ] ; then
26     echo -e "\n$line\nSkipping package push. A newer patch has been merged.\n$line\n"
27     exit 0
28 fi
29
30 DRYRUN="${DRYRUN:-}"
31 if [ "${DRYRUN,,}" = "true" ] ; then
32     echo -e "\n$line\nSkipping package push because DRYRUN is '${DRYRUN,,}'.\n$line\n"
33     exit 0
34 fi
35
36 echo "STARTING PACKAGECLOUD PUSH"
37
38 sleep 10
39
40 FACTER_OS=$(/usr/bin/facter operatingsystem)
41 push_cmd=""
42 push_ext_deps_cmd=""
43 ext_deps_pkg=""
44 downloads_dir="/root/Downloads"
45
46 create_deb_push_cmds()
47 {
48     local distro="$1"
49
50     if [ "$distro" = "debian" ] || [ "$distro" = "ubuntu" ] ; then
51         FACTER_LSBNAME=$(/usr/bin/facter lsbdistcodename)
52         DEBS=$(find . -type f -iname '*.deb' | grep -v vpp-ext-deps | xargs || true)
53         push_cmd="package_cloud push ${PCIO_CO}/${STREAM}/${distro}/${FACTER_LSBNAME}/main/ ${DEBS}"
54         ext_deps_ver="$(dpkg -l vpp-ext-deps | mawk '/vpp-ext-deps/{print $3}' || true)"
55         ext_deps_pkg="$(find . -type f -iname 'vpp-ext-deps*.deb' | grep $ext_deps_ver || find $downloads_dir -type f -iname 'vpp-ext-deps*.deb' | grep $ext_deps_ver || true)"
56         if [ -n "$ext_deps_pkg}" ] ; then
57             push_ext_deps_cmd="package_cloud push ${PCIO_CO}/${STREAM}/${distro}/${FACTER_LSBNAME}/main/ ${ext_deps_pkg}"
58         fi
59     else
60         echo "ERROR: Unknown distro: '$distro'"
61         return 1
62     fi
63 }
64
65 create_rpm_push_cmds()
66 {
67     FACTER_OSMAJREL=$(/usr/bin/facter operatingsystemmajrelease)
68     FACTER_ARCH=$(/usr/bin/facter architecture)
69     RPMS=$(find . -type f -iregex '.*/.*\.\(s\)?rpm' | grep -v vpp-ext-deps | xargs || true)
70     push_cmd="package_cloud push ${PCIO_CO}/${STREAM}/el/${FACTER_OSMAJREL}/os/${FACTER_ARCH}/ ${RPMS}"
71     ext_deps_ver="$(dnf list vpp-ext-deps | mawk '/vpp-ext-deps/{print $2}' || true)"
72     ext_deps_pkg="$(find . -type f -iname 'vpp-ext-deps*.rpm' | grep $ext_deps_ver || find $downloads_dir -type f -iname 'vpp-ext-deps*.rpm' | grep $ext_deps_ver || true)"
73     if [ -n "$ext_deps_pkg" ] ; then
74         push_ext_deps_cmd="package_cloud push ${PCIO_CO}/${STREAM}/el/${FACTER_OSMAJREL}/os/${FACTER_ARCH}/ ${ext_deps_pkg}"
75     fi
76 }
77
78 # PCIO_CO and SILO are Jenkins Global Environment variables defined in
79 # .../ci-management/jenkins-config/global-vars-*.sh
80 if [ -f ~/.packagecloud ]; then
81     case "$FACTER_OS" in
82         Debian)
83             create_deb_push_cmds debian
84             ;;
85         Ubuntu)
86             create_deb_push_cmds ubuntu
87             ;;
88         CentOS)
89             create_rpm_push_cmds
90             ;;
91         *)
92             echo -e "\n$line\n* ERROR: Unsupported OS '$FACTER_OS'\n* PACKAGECLOUD PUSH FAILED!\n$line\n"
93             exit 1
94             ;;
95     esac
96     if [ "${SILO,,}" = "sandbox" ] ; then
97         echo "SANDBOX: skipping '$push_cmd'"
98         if [ -n "$push_ext_deps_cmd" ] ; then
99             echo "SANDBOX: skipping '$push_ext_deps_cmd'"
100         fi
101     else
102         $push_cmd
103         if [ -n "$push_ext_deps_cmd" ] ; then
104             $push_ext_deps_cmd || true
105         fi
106     fi
107 else
108     echo "ERROR: Missing '~/.packagecloud' for user '$(id)'"
109     echo "PACKAGECLOUD PUSH FAILED!"
110     exit 1
111 fi
112
113 echo -e "\n$line\n* PACKAGECLOUD PUSH COMPLETE\n$line\n"