You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中用system模块判断退出码0?开发SalesforceDX+Bamboo交互脚本

Checking Exit Codes for SalesforceDX CLI Commands in Python (for Bamboo)

Great question! Proper exit code checking is super important for keeping your Bamboo + SalesforceDX workflow reliable—you don't want failed auth steps to silently break downstream jobs. Let's fix up your script and add the conditional logic you need.

First: Fix the Exit Code Capture Issue

Right now, your auth command ends with ; echo $?. The problem here is that os.system() will return the exit code of the last command in the chain—which is always echo, almost always 0—instead of the actual exit code from the sfdx force:auth:jwt:grant command. That means you can't tell if the auth actually succeeded!

Let's fix that, and also switch to using subprocess instead of os.system()—it's the modern, more flexible way to run external commands in Python, and it makes capturing exit codes and output way easier.

Updated Script with Exit Code Checks

Here's a refactored version of your script that properly checks the exit code and adds the if logic for success (exit code 0):

import os
import subprocess

# Define your working directory and SFDX command
build_dir = "/var/Atlassian/bamboo/bamboo-home/xml-data/build-dir/SAL-SC-JOB1"
auth_command = [
    "sfdx", "force:auth:jwt:grant",
    "--clientid", "clientidexample",
    "--jwtkeyfile", "/root/server.key",
    "--username", "username@example.org",
    "--setalias", "Alias",
    "--setdefaultdevhubusername"
]

# Move to the Bamboo build directory
os.chdir(build_dir)

# Run the SFDX command and capture results
result = subprocess.run(
    auth_command,
    capture_output=True,  # Capture stdout/stderr for debugging
    text=True  # Return output as strings instead of bytes
)

# Check the exit code and run conditional logic
if result.returncode == 0:
    print("✅ Salesforce DX authentication succeeded!")
    # Add your post-success logic here—like running deploy commands, tests, etc.
else:
    print("❌ Salesforce DX authentication failed!")
    print(f"Error details:\n{result.stderr}")
    # In Bamboo, you might want to exit with a non-zero code to mark the job as failed
    exit(result.returncode)

Why This Works:

  • Using a list for the command (auth_command) avoids shell injection risks and handles spaces/special characters in arguments correctly.
  • subprocess.run() returns a CompletedProcess object where returncode is the actual exit code from the SFDX command (0 = success, non-zero = failure).
  • capture_output=True lets you access result.stdout and result.stderr—super useful for debugging failed Bamboo jobs.

If you need to stick with os.system() for some reason, you can still get the real exit code, but you have to adjust for how os.system() returns values (it packs the exit code into the high 8 bits of an integer):

import os

build_dir = "/var/Atlassian/bamboo/bamboo-home/xml-data/build-dir/SAL-SC-JOB1"
auth_command = "sfdx force:auth:jwt:grant --clientid clientidexample --jwtkeyfile /root/server.key --username username@example.org --setalias Alias --setdefaultdevhubusername"

os.chdir(build_dir)
exit_code = os.system(auth_command)

# Extract the real exit code (shift right by 8 bits)
real_exit_code = exit_code >> 8

if real_exit_code == 0:
    print("Authentication successful!")
else:
    print(f"Authentication failed with exit code: {real_exit_code}")

Note: This method doesn't let you capture stdout/stderr easily, which makes debugging harder in a CI/CD environment like Bamboo. subprocess is definitely the better choice.

Key Takeaways for Bamboo Workflows

  • Always check exit codes for every CLI command—this ensures your Bamboo job stops early if a critical step (like auth) fails.
  • Capture error output so you can quickly diagnose failures in Bamboo's job logs.
  • Use subprocess.run() for more control and reliability over external commands.

内容的提问来源于stack exchange,提问作者Matthew Morcaldi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:07:27