Azure DevOps CI中执行下载的Pipeline Artifact时出现权限问题
Azure DevOps Pipeline多阶段共享二进制文件时权限拒绝问题解决
我在Azure DevOps Pipeline中使用多阶段流程,Build阶段编译C文件生成二进制可执行文件,通过Pipeline Artifacts将文件共享到Test阶段执行测试。编写的C文件是简单的打印hello程序,Pipeline配置如下:
trigger: - main pool: vmImage: ubuntu-latest stages: - stage: Build jobs: - job: BuildJob steps: - script: | echo compiling the c file gcc hello.c -o hello echo finished compiling echo "publishing the contents" - publish: $(System.DefaultWorkingDirectory) artifact: myobj - stage: Test jobs: - job: Testing steps: - download: current artifact: myobj - script: | cd $(Pipeline.Workspace)/myobj ./hello
执行Test阶段时出现权限拒绝错误:
/home/vsts/work/_temp/3ba036fd-22vm0-4e54-b5a4-3ce3ce00f7e6.sh: line 4: ./hello: Permission denied ##[error]Bash exited with code '126'.
问题原因
Pipeline Artifacts在存储和传输文件时,不会保留Linux系统的文件权限位(比如可执行权限+x)。Build阶段编译生成的hello文件本身有执行权限,但发布为Artifact后,权限信息丢失;Test阶段下载后,文件没有执行权限,因此运行时触发Permission denied错误。
解决方案
方案1:Test阶段临时添加执行权限
在Test阶段的脚本中,先给hello文件添加可执行权限,再运行:
- stage: Test jobs: - job: Testing steps: - download: current artifact: myobj - script: | cd $(Pipeline.Workspace)/myobj chmod +x hello ./hello
方案2:打包保留权限后发布
通过tar打包二进制文件,保留权限信息,发布tar包作为Artifact,Test阶段解压后直接执行:
修改Build阶段配置
- stage: Build jobs: - job: BuildJob steps: - script: | echo compiling the c file gcc hello.c -o hello echo finished compiling echo "packing with permissions" tar -czf hello.tar.gz hello echo "publishing the package" - publish: $(System.DefaultWorkingDirectory)/hello.tar.gz artifact: myobj
修改Test阶段配置
- stage: Test jobs: - job: Testing steps: - download: current artifact: myobj - script: | cd $(Pipeline.Workspace)/myobj tar -xzf hello.tar.gz ./hello
内容的提问来源于stack exchange,提问作者Rakshan Premsagar Kapikad
相关产品推荐
相关产品推荐

