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

Bitbucket Pipeline无法正确加载Ubuntu镜像,执行命令报错

问题排查与解决

错误原因分析

出现bash: dircolors: command not found和bash: apt-get: command not found,核心原因是实际运行构建的容器并非你指定的ubuntu:latest镜像,大概率是以下两种情况:

  • Bitbucket仓库的Pipelines全局设置中指定了默认镜像,覆盖了yml文件里的配置
  • bitbucket-pipelines.yml存在语法问题,导致镜像配置未被正确加载,系统自动使用了Bitbucket Pipelines的默认镜像(通常是基于Alpine Linux的atlassian/default-image,这类镜像用apk而非apt-get,且没有dircolors工具)

排查步骤

  1. 验证当前运行的系统:在script最开头添加以下命令,确认实际使用的镜像系统:
cat /etc/os-release
which apt-get || which apk
  1. 检查仓库Pipelines设置:登录Bitbucket仓库,进入「Settings > Pipelines > Settings」,查看「Default Docker image」是否被修改为非Ubuntu镜像,若有则要么改回默认,要么在yml里明确指定镜像覆盖全局设置。
  2. 检查yml语法:确认image: ubuntu:latest是顶格写(和pipelines同层级),无拼写错误(比如写成images),可使用Bitbucket的「Pipelines > Validate」功能验证yml语法正确性。

修正后的配置示例

方案1:确保使用Ubuntu镜像

如果坚持用Ubuntu镜像,需保证配置正确,同时提前安装dircolors所属的coreutils包:

image: ubuntu:latest

pipelines:
  branches:
    master:
      - step:
          name: Upload
          script:
            # 先安装必要工具,包含dircolors所属的coreutils
            - apt-get update && apt-get install -y openssh-client jq curl coreutils
            - cat /opt/atlassian/pipelines/agent/ssh/id_rsa >> ec2_private_key.pem
            - chmod 600 ec2_private_key.pem
            - echo "Fetching modified files from Bitbucket API using diff endpoint"
            - |
              curl -s -H "Authorization: Bearer $API_TOKEN_BEARER" \
              "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_FULL_NAME/commits/master" \
              | jq -r '.values[0].hash' > latest_commit.txt
            - COMMIT_HASH=$(cat latest_commit.txt)
            - |
              curl -s -H "Authorization: Bearer $API_TOKEN_BEARER" \
              "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_FULL_NAME/diff/$COMMIT_HASH" \
              | grep '^+++ b' | awk '{print $2}' | sed 's/^b\///' > modified_files.txt
            - echo "Modified files:"
            - cat modified_files.txt
            - while IFS= read -r file; do
                scp -i ec2_private_key.pem -o StrictHostKeyChecking=no -r "$file" ubuntu@$EC2_HOST:$PATH"$file";
              done < modified_files.txt
            - ssh -i ec2_private_key.pem -o StrictHostKeyChecking=no ubuntu@$EC2_HOST "cd $PATH && sudo docker-compose up --build -d"

方案2:改用Bitbucket默认Alpine镜像

如果打算使用Bitbucket的默认镜像,需将apt-get命令替换为apk:

image: atlassian/default-image:3

pipelines:
  branches:
    master:
      - step:
          name: Upload
          script:
            - apk update && apk add openssh-client jq curl coreutils
            - cat /opt/atlassian/pipelines/agent/ssh/id_rsa >> ec2_private_key.pem
            - chmod 600 ec2_private_key.pem
            # 后续脚本内容与原配置一致

内容的提问来源于stack exchange,提问作者Some d

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 00:01:18