Sub topic 16
Automating Package Builds in CI
This chapter assumes you can run sf package version create by hand, understand version numbers and promotion, and know basic Git and a CI tool such as GitHub Actions. Here you move those manual steps into a pipeline.
Why automate
Building a package version takes several minutes and runs all Apex tests. Doing it by hand makes releases inconsistent. A pipeline builds the same way every time, and it leaves a record of which commit produced which version.
A common split uses two jobs:
- Pull request job: builds a beta version with code coverage and checks that it succeeds.
- Release job: runs on a tag or on the main branch. It promotes a version that has already been validated.
Authenticating without a browser
CI cannot use sf org login web. Use the JWT bearer flow with a connected app in the Dev Hub. Store the private key and the consumer key as encrypted secrets, never in the repository.
echo "$SERVER_KEY" > server.key
sf org login jwt \
--client-id "$CONSUMER_KEY" \
--jwt-key-file server.key \
--username "$DEVHUB_USERNAME" \
--set-default-dev-hub \
--alias devhub
rm server.keyA pull request workflow
This GitHub Actions workflow installs the CLI, authenticates, and builds a version. The --wait flag makes the step block until the build finishes. A failed build returns a non-zero exit code, which fails the job.
name: package-validate
on:
pull_request:
branches: [main]
jobs:
build-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install --global @salesforce/cli
- name: Authenticate to Dev Hub
env:
SERVER_KEY: ${{ secrets.SERVER_KEY }}
CONSUMER_KEY: ${{ secrets.CONSUMER_KEY }}
DEVHUB_USERNAME: ${{ secrets.DEVHUB_USERNAME }}
run: |
echo "$SERVER_KEY" > server.key
sf org login jwt --client-id "$CONSUMER_KEY" --jwt-key-file server.key --username "$DEVHUB_USERNAME" --set-default-dev-hub --alias devhub
rm server.key
- name: Create package version
run: sf package version create --package AcmeApp --installation-key-bypass --code-coverage --wait 30 --target-dev-hub devhub --jsonCommon errors
- Timeouts: if
--waitis shorter than the build, the CLI stops waiting but the build continues in the Dev Hub. Use a generous value and check the result withsf package version create report. - Version limits: each Dev Hub has a daily limit on package version creation. Building on every push can exhaust it, so build on pull requests and merges only.
- Missing coverage: a version built without
--code-coveragecannot be promoted. Always include the flag in the pipeline. - Leaked secrets: a key file left on a shared runner is a risk. Delete it after login, as shown above.
Promoting from the pipeline
Promotion is hard to reverse, so many teams place it behind a manual approval step on the release job. The job then runs sf package version promote --package [email protected] --no-prompt --target-dev-hub devhub. The --no-prompt flag skips the confirmation question, which a pipeline cannot answer.
Exercise
Why should the pipeline always pass --code-coverage when it builds a version, and why should promotion often sit behind a manual approval?
Show answer
A version built without code coverage data cannot be promoted, so leaving the flag out blocks the release later. Promotion marks the version as released and cannot be undone for that version, so a manual approval gives a person a chance to confirm before it happens.