Bitbucket Pipelines产物已提取却不可见,Kaniko构建报错求助
问题:Bitbucket Pipelines中Kaniko构建时找不到dist/app.jar
流水线配置
pipelines: custom: build: - parallel: - step: name: scan for secrets runs-on: - 'self.hosted' - 'linux' script: - pipe: atlassian/git-secrets-scan:0.5.1 - step: name: build image: name: gradle:8.1.1 caches: - gradle runs-on: - 'self.hosted' - 'linux' script: - gradle build -x test - mkdir -p $BITBUCKET_CLONE_DIR/dist - cp build/libs/app.jar $BITBUCKET_CLONE_DIR/dist/ - ls -la dist after-script: - pipe: atlassian/checkstyle-report:0.3.0 artifacts: paths: - dist/** - step: name: test runs-on: - 'self.hosted' - 'linux' image: name: gradle:8.1.1 caches: - gradle script: - gradle test - step: name: dockerize runs-on: - 'self.hosted' - 'linux' image: name: xxx.com/kaniko:v1.12.1-debug-4 username: $USERNAME password: $PASSWORD size: 2x script: - pwd - ls -la - find . -iname '*.jar' - ./devops/pipelines/scripts/kaniko-login.sh - /kaniko/executor --cache=true --cache-copy-layers=true --cache-repo="$DOCKER_DOMAIN/lalala/cache" --context="$BITBUCKET_CLONE_DIR" --dockerfile="$BITBUCKET_CLONE_DIR/Dockerfile" --destination="$DOCKER_DOMAIN/lalala:latest" --destination="$DOCKER_DOMAIN/lalala:$BITBUCKET_BUILD_NUMBER" --use-new-run --snapshot-mode=redo
报错信息
error building image: error building stage: failed to optimize instructions: failed to get files used from context: failed to get fileinfo for /opt/atlassian/pipelines/agent/build/dist/app.jar: lstat /opt/atlassian/pipelines/agent/build/dist/app.jar: no such file or directory
调试结果
- 在
build步骤执行ls -la dist,确认dist目录及app.jar存在:
+ ls -la dist total 58312 drwxrwxrwx 2 root root 4096 Jul 12 11:07 . drwxrwxrwx 9 root root 4096 Jul 12 11:07 .. -rw-rw-rw- 1 root root 59700452 Jul 12 11:07 app.jar
build步骤artifact上传成功,可在Artifacts标签页下载dockerize步骤显示artifact已下载提取,但执行find . -iname '*.jar'仅找到gradle-wrapper.jar,无dist目录
解决方案
问题核心是artifact的下载位置与Kaniko指定的构建上下文不匹配:
- Bitbucket Pipelines会将artifact下载到当前步骤的工作目录,但你使用的Kaniko镜像默认工作目录并非
$BITBUCKET_CLONE_DIR(代码克隆目录) - Kaniko命令中指定
--context="$BITBUCKET_CLONE_DIR",而该目录下只有原始克隆代码,没有下载的artifact
修复方案一:调整Kaniko的构建上下文
将Kaniko命令中的--context参数改为当前工作目录(.),同时确保Dockerfile中引用app.jar的路径对应dist目录的位置:
/kaniko/executor --cache=true --cache-copy-layers=true --cache-repo="$DOCKER_DOMAIN/lalala/cache" --context="." --dockerfile="$BITBUCKET_CLONE_DIR/Dockerfile" --destination="$DOCKER_DOMAIN/lalala:latest" --destination="$DOCKER_DOMAIN/lalala:$BITBUCKET_BUILD_NUMBER" --use-new-run --snapshot-mode=redo
修复方案二:指定artifact下载到克隆目录
在dockerize步骤中添加artifact下载配置,强制将dist目录下载到$BITBUCKET_CLONE_DIR下:
step: name: dockerize runs-on: - 'self.hosted' - 'linux' image: name: xxx.com/kaniko:v1.12.1-debug-4 username: $USERNAME password: $PASSWORD size: 2x artifacts: download: - path: dist/** destination: $BITBUCKET_CLONE_DIR script: - pwd - ls -la $BITBUCKET_CLONE_DIR/dist - ./devops/pipelines/scripts/kaniko-login.sh - /kaniko/executor --cache=true --cache-copy-layers=true --cache-repo="$DOCKER_DOMAIN/lalala/cache" --context="$BITBUCKET_CLONE_DIR" --dockerfile="$BITBUCKET_CLONE_DIR/Dockerfile" --destination="$DOCKER_DOMAIN/lalala:latest" --destination="$DOCKER_DOMAIN/lalala:$BITBUCKET_BUILD_NUMBER" --use-new-run --snapshot-mode=redo
额外验证步骤
在dockerize步骤的script开头添加以下命令,确认目录结构,排查问题:
- echo "BITBUCKET_CLONE_DIR: $BITBUCKET_CLONE_DIR" - echo "当前工作目录: $(pwd)" - ls -la /opt/atlassian/pipelines/agent/build # 托管runner默认路径,self-hosted可能不同
内容的提问来源于stack exchange,提问作者Mateusz
相关产品推荐
相关产品推荐

