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

Bitbucket Pipeline全局函数调用失败:common_function未找到求助

问题原因

你遇到的common_function: command not found错误,是因为用YAML锚点定义的common_function只是一段shell代码文本,并非可直接调用的bash函数,所以把它当成命令执行时会被系统判定为找不到。

解决方案

以下两种方法都能解决这个问题,根据你的使用场景选择即可:

方案1:将锚点改为函数定义,先加载再调用

把锚点内容改成bash函数的定义格式,在步骤脚本中先加载函数,之后就能正常调用:

definitions:
  steps:
    # 锚点改为定义bash函数
    common_function_def: &common_function_def |
      common_function() {
        if [ "$BITBUCKET_BRANCH" == "master" ]; then
          echo "steps"
        fi
      }

pipelines:
  branches:
    master:
      - step:
          name: Step1
          size: 2x 
          runs-on: 
            - 'scanner.scanner'
          script:
            # 先执行函数定义,将函数加载到当前shell环境
            *common_function_def
            # 现在可以正常调用函数
            if some condition; then
              common_function
            fi

方案2:直接用锚点插入代码块(无需函数)

如果只是需要在特定位置复用这段代码,不需要定义函数,可以直接把锚点的代码插入到脚本对应位置,不用当成命令调用:

definitions:
  steps:
    common_function: &common_function |
      if [ "$BITBUCKET_BRANCH" == "master" ]; then
        echo "steps"
      fi

pipelines:
  branches:
    master:
      - step:
          name: Step1
          size: 2x 
          runs-on: 
            - 'scanner.scanner'
          script:
            if some condition; then
              # 直接引用锚点,把代码插入到当前位置
              *common_function
            fi

YAML解析时会自动把锚点里的代码替换到*common_function的位置,相当于你直接把那段if语句写在这里,自然能正常执行。

内容的提问来源于stack exchange,提问作者Justine mathew

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 23:21:05