GitHub Action中echo表达式执行zip报Nothing to do错误排查
GitHub Action 条件打包变更目录报
zip error: Nothing to do 修复方案 问题根因
错误确实出在zip步骤的args参数配置,两个核心问题导致执行失败:
- 参数未经过shell解析:
montudor/action-zip@v1会把args字段的内容直接作为参数传给zip二进制程序,不会启动shell解释执行,因此配置里写的$()命令替换、&&条件判断语法都不会生效,会被直接当成字面量文件路径,zip找不到对应路径自然抛出无内容可打包的错误。 - 转义字符错误:配置中写的
"、&&是HTML转义实体,在YAML配置文件中不会被自动转义为原字符,会被当做普通字符串传入,进一步导致参数解析异常。
即使三个变更检测输出全为true,上述写法最终传给zip的参数是一串不存在的路径字符串,必然报错。
修复方案
不要把shell逻辑写在zip的args参数里,新增单独的shell步骤提前计算好需要打包的目录列表,再把最终的纯路径参数传给zip命令。
修复后的完整工作流配置如下:
changes: name: Detect changes runs-on: ubuntu-latest outputs: aws: ${{ steps.filter.outputs.aws }} azure: ${{ steps.filter.outputs.azure }} gcp: ${{ steps.filter.outputs.gcp }} steps: - name: Checkout source code uses: actions/checkout@v2 - name: Check for changes uses: dorny/paths-filter@v2 id: filter with: filters: | aws: - added|modified: 'aws/**' azure: - added|modified: 'azure/**' gcp: - added|modified: 'gcp/**' zip_files: runs-on: ubuntu-latest needs: changes steps: - uses: actions/checkout@v2 - name: Build zip target list id: zip_targets run: | PACKAGE_PATHS="" if [ "${{ needs.changes.outputs.aws }}" = "true" ]; then PACKAGE_PATHS="$PACKAGE_PATHS aws" fi if [ "${{ needs.changes.outputs.azure }}" = "true" ]; then PACKAGE_PATHS="$PACKAGE_PATHS azure" fi if [ "${{ needs.changes.outputs.gcp }}" = "true" ]; then PACKAGE_PATHS="$PACKAGE_PATHS gcp" fi echo "paths=$PACKAGE_PATHS" >> $GITHUB_OUTPUT - uses: montudor/action-zip@v1 with: args: zip -qq -r result.zip ${{ steps.zip_targets.outputs.paths }} - uses: actions/upload-artifact@v3 with: name: my-artifact path: ${{ github.workspace }}/result.zip
额外说明
- YAML配置文件中直接书写原生字符即可,不需要写HTML转义实体,仅在HTML标签嵌套代码的场景才需要做转义。
- 所有涉及条件判断、命令替换的shell逻辑,都要放在
run字段对应的shell步骤中执行,不要直接传给不做shell解析的第三方action参数。 - 原配置用的
actions/upload-artifact@v1版本已经停止维护,升级到v3版本可以避免很多兼容性问题。
内容的提问来源于stack exchange,提问作者Alan Chen
相关产品推荐
相关产品推荐

