如何在GitHub Workflow步骤名称中嵌入变量值?
在GitHub Workflow步骤名称中使用变量值的解决方法
你遇到的问题是步骤名称里的${{ env.REGISTRY }}变量无法正常渲染,这是YAML解析时的语法问题,以下是两种可行的解决方法:
方法1:用双引号包裹步骤名称
GitHub Actions支持在步骤名称中使用表达式,但需要将整个名称用双引号括起来,确保YAML正确解析表达式内容:
name: Publish on: push: branches: - main env: REGISTRY: ghcr.io jobs: Publish: runs-on: ubuntu-latest steps: - name: "Log into Container registry ${{ env.REGISTRY }}" # 示例:添加docker登录逻辑 uses: docker/login-action@v2 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }}
方法2:通过步骤摘要自定义显示标题
如果方法1不生效,还可以通过run步骤将带变量的标题写入步骤摘要,在Workflow运行结果中展示自定义标题:
name: Publish on: push: branches: - main env: REGISTRY: ghcr.io jobs: Publish: runs-on: ubuntu-latest steps: - name: Log into Container registry run: | # 将带变量的标题写入步骤摘要 echo "### Log into Container registry ${{ env.REGISTRY }}" >> $GITHUB_STEP_SUMMARY uses: docker/login-action@v2 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }}
内容的提问来源于stack exchange,提问作者me-me
相关产品推荐
相关产品推荐

