Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / package-listing.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11
12 echo "---> package-listing.sh"
13
14 # Ensure we fail the job if any steps fail
15 set -eu -o pipefail
16
17 set -x # Trace commands for this script to make debugging easier
18
19 OS_FAMILY=$(facter osfamily | tr '[:upper:]' '[:lower:]')
20
21 # Capture the CI WORKSPACE safely in the case that it doesn't exist
22 workspace="${WORKSPACE:-}"
23
24 START_PACKAGES=/tmp/packages_start.txt
25 END_PACKAGES=/tmp/packages_end.txt
26 DIFF_PACKAGES=/tmp/packages_diff.txt
27
28 # Swap to creating END_PACKAGES if we are running in a CI job (determined by if
29 # we have a workspace env) or if the starting packages listing already exists.
30 PACKAGES="${START_PACKAGES}"
31 if [ "${workspace}" ] || [ -f "${START_PACKAGES}" ]; then
32     PACKAGES="${END_PACKAGES}"
33 fi
34
35 case "${OS_FAMILY}" in
36     redhat|suse)
37         # RedHat and Suse flavors all use rpm at the package level
38         rpm -qa | sort > "${PACKAGES}"
39     ;;
40     debian)
41         # Debian derived flavors all use dpkg at the package level
42         dpkg -l | grep '^ii' > "${PACKAGES}"
43     ;;
44     *)
45         # nothing to do
46     ;;
47 esac
48
49 if [ -f "${START_PACKAGES}" ] && [ -f "${END_PACKAGES}" ]; then
50     # ` || true` Ignore exit code because diff exits 1 when there is a diff
51     diff "${START_PACKAGES}" "${END_PACKAGES}" > "${DIFF_PACKAGES}" || true
52 fi
53
54 # If running in a Jenkins job, then copy the created files to the archives
55 # location
56 if [ "${workspace}" ]; then
57     mkdir -p "${workspace}/archives/"
58     cp -f /tmp/packages_*.txt "${workspace}/archives/"
59 fi