Sub topic 13
Automating agent deployment in a pipeline
This chapter assumes you can already retrieve, validate, and deploy an agent by hand with Salesforce CLI. It also assumes you know a Salesforce DX project layout and how permission sets work. Here you will turn those manual steps into a repeatable pipeline.
Why automate
A manual deployment depends on one person remembering every step. They must retrieve the right metadata, validate, deploy, assign permissions, and activate. A pipeline runs the same steps in the same order every time. It also leaves a log you can review when something fails.
Agent deployment has one extra concern. Deploying metadata does not finish the job. The agent user needs permissions, and the agent must be activated. Both steps belong in the pipeline, or the target org ends up with an agent that exists but does not run.
The pipeline stages
A typical pipeline has five stages. Each stage should stop the run if it fails.
- Authenticate. Log in to the target org without a browser.
- Validate. Run a validation-only deployment with
--dry-run. - Deploy. Run the real deployment with the same manifest.
- Configure. Assign the permission set that grants access to the agent's actions.
- Activate and test. Activate the agent, then check that it responds.
Authenticating without a browser
The manual steps used sf org login web, which opens a browser. A pipeline runner has no browser. Use the JWT bearer flow instead. It needs a connected app in the target org, a certificate, and a private key stored as a pipeline secret.
sf org login jwt \
--client-id "$SF_CLIENT_ID" \
--jwt-key-file server.key \
--username "$SF_USERNAME" \
--alias target-org \
--set-defaultNever commit server.key or the client ID to the repository. Store them as encrypted secrets in your CI system and write the key to a temporary file at run time.
A GitHub Actions example
This workflow deploys when changes reach the main branch. It reuses the manifest and the commands from earlier chapters. The permission set and agent names are placeholders.
name: deploy-agent
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to target org
run: |
echo "${{ secrets.SF_JWT_KEY }}" > server.key
sf org login jwt \
--client-id "${{ secrets.SF_CLIENT_ID }}" \
--jwt-key-file server.key \
--username "${{ secrets.SF_USERNAME }}" \
--alias target-org
rm server.key
- name: Validate
run: |
sf project deploy start \
--manifest manifest/package.xml \
--target-org target-org \
--dry-run
- name: Deploy
run: |
sf project deploy start \
--manifest manifest/package.xml \
--target-org target-org
- name: Assign permission set
run: sf org assign permset --name Agent_Action_Access --target-org target-org
- name: Activate agent
run: sf agent activate --api-name My_Service_Agent --target-org target-orgGitHub Actions stops a job at the first failing step, so a failed validation prevents the real deployment.
Design decisions
- Environment-specific values. Keep values that differ per org out of the metadata you commit, or substitute them in a pipeline step. The target org's configuration should come from the pipeline, not from the source sandbox.
- Narrow the manifest. Wildcards such as
*are fine for learning. In a pipeline, list specific members so a stray component in the repository does not reach production. - Repeat permission assignment safely. If the permission set is already assigned, the command may report an error. Decide whether your pipeline should tolerate that result, and check the output before ignoring it.
- Separate environments. Use one job per target, such as a test sandbox and then production, and require manual approval before production.
Testing in the pipeline
The last stage should confirm that the agent works. At a minimum, check that activation succeeded and that the exit code of every command was zero. If your team has agent tests defined, run them against the target org and fail the pipeline when a test fails. Chapter-level manual checks, such as asking the agent a known question, are still useful after the first deployment.
Exercise
Your pipeline deploys the agent successfully, but users in the target org cannot get the agent to run its actions. Which two pipeline steps would you check first, and why?
Show answer
Check the permission set assignment and the activation step. Deployment only moves metadata. The agent user needs the permission set that grants access to the agent's actions, and the agent must be activated before it responds. If either step failed or was skipped, the agent exists but does not work.
Exercise
Why does the pipeline use sf org login jwt instead of sf org login web, and where should the private key be stored?
Show answer
A pipeline runner has no browser, so the web login cannot complete. The JWT flow authenticates with a key. The private key belongs in the CI system's encrypted secrets, not in the repository.