7

To facilitate deploying packages, it is helpful to quickly identify what the latest package version instead of manually finding the latest package version id.

I cannot find a sfdx command to install the latest package version when given a parameter, such as the package name.

For example, it would be great to do something like this:

sfdx force:package:version:list -p "Package A" -latest

Is there a helpful sfdx command to do this? It's possible to do this via a bash script etc, but would be nicer if there was a sfdx way to achieve this.

4 Answers 4

6

Spent several hours searching and looks like script is the only solution.

Raised enhancement request https://github.com/forcedotcom/sfdx-core/issues/165

For now just using bash to resolve latest version(based on creation date):

latestVersion=$(sfdx force:package:version:list -p '%sf.pkg.name%' -o CreatedDate --concise | tail -1 | awk '{print $3}')

Most likely, everyone has a similar script in their CI/CD setup.

1
  • BTW, you can use -o "CreatedDate DESC" to reverse the order (and use "head" instead of "tail").
    – Phil W
    Commented May 14, 2021 at 16:31
6

You can use the tooling API (sfdx force:data:soql:query) to simply query for package versions:

SELECT Id,Package2Id,SubscriberPackageVersionId,Description,IsDeprecated,IsPasswordProtected,IsReleased FROM Package2Version ORDER BY CreatedDate DESC LIMIT 1

This query will return the latest package version, with SubscriberPackageVersionId beeing the 04t Id that you can use to install the package.

You can build any logic to retrieve only Package Versions to specific packages (use Package2Id to filter for that) or only the latest version of a given MajorVersion. You can also use this to extract / solve dependencies.

Resources:

1

Here is a script using JQ. Advantage being you can pass it write into install command.

#echo "Obtaining Latest Package Version" resampleDevpackageVersion=$(sfdx force:package:version:list -p 0Ho3i00000000FQCAY --json | jq '.result[-1].SubscriberPackageVersionId' --raw-output)

echo "Installing Package Version: " $resampleDevpackageVersion sfdx force:package:install --package $resampleDevpackageVersion -w 15

0

I am using Python to obtain the highest released version, and I am incorporating additional logic to disregard patches since I only require the latest released version.

import subprocess
import json

def sort_packages(packages):
    return sorted(packages, key=lambda x: (int(x['MajorVersion']), int(x['MinorVersion']), int(x['PatchVersion']), int(x['BuildNumber'])))

def execute_command(command):
    return subprocess.check_output(command, shell=True, text=True, cwd='./')

package_id = "your_package_id"
command = f"sfdx package version list -p \"{package_id}\" --released --order-by CreatedDate --concise --json"

result_json = execute_command(command)

released_versions = json.loads(result_json)['result']
released_versions = sort_packages(released_versions)

# Latest Released Version
last_package = released_versions[-1]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .