Sub topic 13
Automating agent deployment in a pipeline
You have run each step of an agent deployment by hand: retrieve, build a manifest, deploy, assign permissions, activate, verify. This chapter turns those steps into a repeatable pipeline. It assumes you can read a GitHub Actions workflow (the same ideas apply to other CI systems), have a Salesforce DX project under version control, and have completed the earlier chapters on manifests, the agent user, and activation.
Why automate this
A manual deployment of an agent depends on memory. Someone has to remember to deploy the Apex classes before the actions that call them, assign the permission set, and activate the agent. A pipeline records those steps once and runs them the same way every time.
A pipeline also gives you three things that manual work does not:
- A validation run on every pull request, so broken dependencies show up before merge.
- An approval gate before the production org changes.
- A test report that is saved with each run, which helps when you compare behavior between orgs.
The shape of the pipeline
The pipeline has the same stages as the manual process. Each stage fails fast and stops the later ones.
The table below maps each stage to a command and says what a failure usually means.
| Stage | Command | A failure usually means |
|---|---|---|
| Authenticate | `sf org login jwt` | Wrong client ID, key, username, or instance URL |
| Validate | `sf project deploy start --dry-run` | A missing dependency or a feature not enabled in the target org |
| Deploy | `sf project deploy start` | The same causes as validation, or the org changed since validation |
| Permissions | `sf org assign permset` | The permission set was not part of the deployment |
| Activate | `sf agent activate` | The agent has a configuration error, or the agent user is invalid |
| Test | `sf agent test run` | The agent behaves differently in the target org |
Authenticating without a browser
The sf org login web command used earlier opens a browser, so it cannot run in a pipeline. Use the JWT bearer flow instead. It needs a connected app in the target org that is configured with a certificate. You keep the matching private key in your CI system's secret store.
Store four values as secrets: the connected app's client ID, the private key, the pipeline user's username, and the instance URL. Never commit the key to the repository.
printf '%s' "$SF_JWT_KEY" > "$RUNNER_TEMP/server.key"
sf org login jwt \
--client-id "$SF_CLIENT_ID" \
--jwt-key-file "$RUNNER_TEMP/server.key" \
--username "$SF_USERNAME" \
--instance-url "$SF_INSTANCE_URL" \
--alias target-org
rm -f "$RUNNER_TEMP/server.key"For a sandbox, the instance URL is https://test.salesforce.com or your sandbox's My Domain URL. For production it is https://login.salesforce.com or your My Domain URL.
The pipeline user needs permission to deploy metadata and to manage agents in the target org. Give it a dedicated permission set rather than reusing an administrator account.
Validate on pull requests, deploy on merge
Run the validation-only deployment on every pull request. It performs the same checks as a real deployment but saves nothing in the org. Run the real deployment only after merge, and put an approval gate in front of it.
sf project deploy start \
--manifest manifest/package.xml \
--target-org target-org \
--dry-run \
--wait 30The --wait flag sets how many minutes the CLI waits for the deployment to finish. Without it, a long deployment can exceed the default wait and the command returns while the job is still running, which makes the pipeline report the wrong result.
For production, add --test-level RunLocalTests. Production deployments that include Apex classes require Apex tests to run, and your agent's actions often include Apex classes.
A complete workflow
The workflow below uses GitHub Actions. It has two jobs. The validate job runs on pull requests. The deploy job runs after a merge to main and uses a GitHub environment named uat. You can set required reviewers on that environment, which creates the approval gate.
name: Agent deployment
on:
pull_request:
branches: [main]
push:
branches: [main]
env:
AGENT_API_NAME: My_Service_Agent
AGENT_TEST_NAME: My_Service_Agent_Tests
jobs:
validate:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate
env:
SF_JWT_KEY: ${{ secrets.SF_JWT_KEY }}
run: |
printf '%s' "$SF_JWT_KEY" > "$RUNNER_TEMP/server.key"
sf org login jwt \
--client-id "${{ secrets.SF_CLIENT_ID }}" \
--jwt-key-file "$RUNNER_TEMP/server.key" \
--username "${{ secrets.SF_USERNAME }}" \
--instance-url "${{ secrets.SF_INSTANCE_URL }}" \
--alias target-org
rm -f "$RUNNER_TEMP/server.key"
- name: Validate deployment
run: |
sf project deploy start \
--manifest manifest/package.xml \
--target-org target-org \
--dry-run \
--wait 30
deploy:
if: github.event_name == 'push'
runs-on: ubuntu-latest
environment: uat
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate
env:
SF_JWT_KEY: ${{ secrets.SF_JWT_KEY }}
run: |
printf '%s' "$SF_JWT_KEY" > "$RUNNER_TEMP/server.key"
sf org login jwt \
--client-id "${{ secrets.SF_CLIENT_ID }}" \
--jwt-key-file "$RUNNER_TEMP/server.key" \
--username "${{ secrets.SF_USERNAME }}" \
--instance-url "${{ secrets.SF_INSTANCE_URL }}" \
--alias target-org
rm -f "$RUNNER_TEMP/server.key"
- name: Deploy
run: |
sf project deploy start \
--manifest manifest/package.xml \
--target-org target-org \
--wait 30
- 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 "$AGENT_API_NAME" --target-org target-org
- name: Run agent tests
run: |
sf agent test run \
--api-name "$AGENT_TEST_NAME" \
--target-org target-org \
--wait 10 \
--result-format junit \
--output-dir test-results
- name: Upload test results
if: always()
uses: actions/upload-artifact@v4
with:
name: agent-test-results
path: test-resultsThe test step assumes your project contains an agent test definition named My_Service_Agent_Tests and that it was part of the deployment. Add it to the manifest, as you did for the other agent components. If you have not written agent tests yet, remove the last two steps for now and add them back later.
Making the steps safe to repeat
A pipeline runs the same steps many times, so each step has to tolerate repeated runs.
- Permission sets. Assigning a permission set the user already has produces an error in some CLI versions. In a pipeline, treat that specific message as success rather than failing the whole run. Check what your CLI version prints before you filter on it.
- Activation. Activating an agent that is already active may fail or do nothing, depending on the version. Look at the exit code and message in a test run, and decide whether you want the pipeline to continue.
- New agent versions. When a deployment contains a new version of an agent that is currently active, the version that serves users does not change until you activate the new one. Your activation step is what makes the change visible to users.
Environment-specific values
The pipeline should deploy the same metadata to every environment. Settings that differ between orgs, such as the agent user's username, should not be edited by hand before each run. Two common approaches work:
- Keep one value per environment in the pipeline's configuration and apply it with a small script before deployment. For example, replace a placeholder username in the agent metadata with
sed. - Keep a separate source folder per environment and deploy it after the shared folder, with the environment-specific files overriding the shared ones.
Whichever you choose, keep the substitution in the pipeline and not in a person's local checkout. Otherwise the repository will hold values that belong to a single org.
What belongs in version control
The pipeline deploys from the repository, so the repository has to contain everything the agent needs:
- The agent metadata you retrieved:
Bot,BotVersion,GenAiPlanner,GenAiPlugin, andGenAiFunction. - The Apex classes, flows, and permission sets that the actions call.
- The manifest in
manifest/package.xml. - The agent test definitions.
If a component exists only in the source org, the pipeline will pass on the source org and fail on a clean target. Retrieve again after each change in the source org and commit the result, so the repository does not drift from what you tested.
Common pipeline failures
- The login fails with a certificate error. The certificate on the connected app does not match the private key in your secret. Upload the certificate again, or replace the secret.
- The key is empty. The secret has no value in this environment, or the environment name in the workflow does not match the name in the repository settings.
- Validation passes but the deployment fails. The target org changed between runs. Run the two steps close together, and check the deployment details with
sf project deploy report. - Activation fails after a successful deployment. The agent user is missing, inactive, or lacks the permission set. Check the agent user setup before looking at the metadata.
- The test step times out. Increase
--wait, or split the tests into smaller definitions.
Exercise
Your pull request validation passes, but the deployment job fails after merge with an error that a flow referenced by an agent action does not exist. Name two likely causes and one change to the pipeline that would catch the problem earlier.
Show answer
Likely causes: the flow was never added to the manifest or committed to the repository (it existed only in the source org), or someone deleted or renamed the flow in the target org between validation and deployment. Catching it earlier: make sure the validation job uses the same manifest as the deployment job, retrieve and commit the flow, and shorten the gap between validation and deployment, for example by validating again immediately before the deploy step.
Exercise
Why does the workflow assign the permission set and activate the agent in the deploy job but not in the validate job?
Show answer
Validation uses --dry-run, so nothing is saved in the org. There is no deployed agent to activate and no deployed permission set to assign. Those steps change the org's state, so they run only in the real deployment.