Newer Version Available

This content describes an older version of this product. View Latest

Sample Script for Installing Unlocked Packages with Dependencies

Use this sample script as a basis to create your own script to install packages with dependencies. This script contains a query that finds dependent packages and installs them in the correct dependency order.

Sample Script

Be sure to replace the package version ID and scratch org user name with your own specific details.

Note

1#!/bin/bash
2
3
4# The execution of this script stops if a command or pipeline has an error.
5
6# For example, failure to install a dependent package will cause the script
7
8# to stop execution.
9
10set -e
11
12
13# Specify a package version id (starts with 04t)
14
15# If you know the package alias but not the id, use sf package version list to find it.
16
17PACKAGE=04tB0000000NmnHIAS
18
19
20# Specify the user name of the subscriber org.
21
22USER_NAME=test-bvdfz3m9tqdf@example.com
23
24
25# Specify the timeout in minutes for package installation.
26
27WAIT_TIME=15
28
29
30echo "Retrieving dependencies for package Id: "$PACKAGE
31
32
33# Execute soql query to retrieve package dependencies in json format.
34
35RESULT_JSON=`sf data query -u $USER_NAME -t -q "SELECT Dependencies FROM SubscriberPackageVersion WHERE Id='$PACKAGE'" --json`
36
37
38# Parse the json string using python to test whether the result json contains a list of ids or not.
39
40DEPENDENCIES=`echo $RESULT_JSON | python -c 'import sys, json; print json.load(sys.stdin)["result"]["records"][0]["Dependencies"]'`
41
42
43# If the parsed dependencies is None, the package has no dependencies. Otherwise, parse the result into a list of ids.
44
45# Then loop through the ids to install each of the dependent packages.
46
47if [[ "$DEPENDENCIES" != 'None' ]]; then
48
49
50    DEPENDENCIES=`echo $RESULT_JSON | python -c '
51
52import sys, json
53
54ids = json.load(sys.stdin)["result"]["records"][0]["Dependencies"]["ids"]
55
56dependencies = []
57
58for id in ids:
59
60    dependencies.append(id["subscriberPackageVersionId"])
61
62print " ".join(dependencies)
63
64'` 
65
66
67    echo "The package you are installing depends on these packages (in correct dependency order): "$DEPENDENCIES
68
69    for id in $DEPENDENCIES
70
71    do
72
73        echo "Installing dependent package: "$id
74
75        sf package install --package $id -u $USER_NAME -w $WAIT_TIME --publish-wait 10
76
77    done
78
79
80else
81
82    echo "The package has no dependencies"
83
84
85fi
86
87
88# After processing the dependencies, proceed to install the specified package.
89
90echo "Installing package: "$PACKAGE
91
92sf package install --package $PACKAGE -u $USER_NAME -w $WAIT_TIME --publish-wait 10
93
94
95exit 0;