GitHub Actions:如何在step步骤中使用matrix矩阵配置值
问题原因
- GitHub Actions中,Action的
with输入参数在工作流预解析阶段处理,不支持读取env字段定义的shell环境变量,你使用的${platform}是shell环境变量读取语法,仅在run字段的shell脚本执行时生效,这是变量识别失败的核心原因,和docker/build-push-action的版本无关。 - 你此前的同类配置能正常运行,大概率是因为之前的变量应用场景是
run字段下的shell命令,而非Action的with输入参数。
解决方案
不需要将matrix值中转到环境变量,直接在with字段中通过GitHub Actions内置表达式引用matrix变量即可,${{ }}包裹的变量可以在with字段中正常解析:
name: Build and Publish Docker image(s) jobs: publish: runs-on: ubuntu-latest steps: - name: Check out repository uses: actions/checkout@v2 - name: Log in to Docker Hub uses: docker/login-action@v1 with: password: ${{ secrets.DOCKERHUB_TOKEN }} registry: docker.io username: ${{ secrets.DOCKERHUB_USERNAME }} - name: Build and push uses: docker/build-push-action@v2 with: context: ./${{ matrix.platforms }}/ push: true tags: oscarotero/lume:1.3.0-${{ matrix.platforms }} # TODO: Parameterize version once this is finally working strategy: matrix: platforms: - alpine - debian on: push: branches: - main
如果你确实需要先将matrix值存入环境变量再使用,需要用${{ env.platform }}的表达式语法读取环境变量,而非shell的${platform}写法。
内容的提问来源于stack exchange,提问作者user4695271
相关产品推荐
相关产品推荐

