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

如何将Azure Pipeline的iOS构建状态同步到Bitbucket?脚本环境变量问题排查

How to Display Azure Pipeline Build Status in Bitbucket

The issue you're facing is that the original script uses Jenkins-specific environment variables, which don't exist in Azure Pipeline. Azure has its own set of predefined variables, so we need to replace those references, plus optimize authentication and status handling to make this work reliably.

Step 1: Use Azure Pipeline's Predefined Environment Variables

Here's how to map Jenkins variables to their Azure equivalents (note: Azure converts variable names to uppercase with underscores for shell/script access):

  • Jenkins BUILD_ID → Azure BUILD_BUILDID (unique ID of the current build)
  • Jenkins JOB_NAME → Azure BUILD_DEFINITIONNAME (name of your pipeline)
  • Jenkins BUILD_URL → Need to construct it using Azure's collection URI and project info
  • Jenkins GIT_COMMIT → Azure BUILD_SOURCEVERSION (the commit hash being built)

Step 2: Updated Python Script

Replace your original script with this version, which uses Azure variables, dynamic status mapping, and secure authentication:

#!/usr/bin/env python
import os
import requests

# Get Azure Pipeline environment variables
build_id = os.getenv('BUILD_BUILDID')
definition_name = os.getenv('BUILD_DEFINITIONNAME')
commit_hash = os.getenv('BUILD_SOURCEVERSION')
collection_uri = os.getenv('SYSTEM_TEAMFOUNDATIONCOLLECTIONURI')
team_project = os.getenv('SYSTEM_TEAMPROJECT')
job_status = os.getenv('AGENT_JOBSTATUS')

# Map Azure job status to Bitbucket's allowed status values
status_map = {
    'Succeeded': 'SUCCESSFUL',
    'Failed': 'FAILED',
    'Canceled': 'STOPPED'
}
state = status_map.get(job_status, 'INPROGRESS')
description = f"Build {build_id} {job_status.lower()}."

# Construct build URL for Bitbucket link-back
build_url = f"{collection_uri}{team_project}/_build/results?buildId={build_id}"

# Prepare payload for Bitbucket API
data = {
    'key': build_id,
    'state': state,
    'name': definition_name,
    'url': build_url,
    'description': description
}

# Bitbucket repo details (replace with your owner and repo slug)
owner = 'emmap1'
repo_slug = 'MyRepo'
api_url = f"https://api.bitbucket.org/2.0/repositories/{owner}/{repo_slug}/commit/{commit_hash}/statuses/build"

# Get Bitbucket credentials from Azure Pipeline secret variables
bitbucket_user = os.getenv('BITBUCKET_USER')
bitbucket_app_password = os.getenv('BITBUCKET_APP_PASSWORD')

try:
    # Post status to Bitbucket (use app password instead of regular password for security)
    response = requests.post(
        api_url,
        auth=(bitbucket_user, bitbucket_app_password),
        json=data
    )
    response.raise_for_status()
    print(f"Successfully posted build status to Bitbucket. Response: {response.status_code}")
except requests.exceptions.RequestException as e:
    print(f"Failed to post build status: {str(e)}")

Step 3: Configure Azure Pipeline for Secure Authentication

  1. In your Azure Pipeline project, go to Library > Variable Groups (or directly in your pipeline variables)
  2. Add two secret variables:
    • BITBUCKET_USER: Your Bitbucket username
    • BITBUCKET_APP_PASSWORD: A Bitbucket app password with Repository > Write permissions (create this in your Bitbucket account settings under App passwords)
  3. When adding the Python task to your pipeline, ensure the Allow scripts to access OAuth token option is enabled (or if using variable groups, link them to your pipeline)

Step 4: Test the Script

Add a Python script task to your pipeline (after build/test steps) and point it to this updated script. You can add debug prints (like print(f"Build ID: {build_id}")) to verify variables are being loaded correctly.

内容的提问来源于stack exchange,提问作者John Doe

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:35:50