作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 GitHub Actions 工作流程文件,其中环境变量并不总是被扩展。
根据注释,环境变量的使用可以正常工作,直到最后一次在 name:deploy
部分中使用它时,它不会扩展并实际上变成字符串 rg- blue-$***GITHUB_REF#refs/heads/***
而不是前面部分中正确扩展的字符串:rg-blue-my-branch-name
。
这会导致 Azure ARM 错误:错误:找不到资源组 rg-blue-$***GITHUB_REF#refs/heads/***。
为什么除了最后一步之外,变量扩展在所有地方都能正确工作?我该如何修复它?
on: [push]
name: Azure ARM
jobs:
build-and-deploy:
runs-on: ubuntu-latest
env:
resourceGroupName: rg-blue-${GITHUB_REF#refs/heads/}
resourceGroupLocation: 'uksouth'
- name: Use the custom ENV variable
run: |
echo "${{ env.resourceGroupName}}"
echo ${{ env.resourceGroupName}}
// these two work perfectly fine and prints "rg-blue-my-branch-name" etc
- uses: actions/checkout@master
- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- uses: azure/CLI@v1
with:
inlineScript: |
#!/bin/bash
// works perfectly fine here too
if $(az group exists --name ${{ env.resourceGroupName }}) ; then
echo "Azure resource group ${{ env.resourceGroupName }} already exists, skipping creation..."
else
az group create --name ${{ env.resourceGroupName }} --location ${{ env.resourceGroupLocation }}
echo "Azure resource group ${{ env.resourceGroupName }} created"
fi
# Deploy Bicep file
- name: deploy
uses: azure/arm-deploy@v1
with:
subscriptionId: ${{ secrets.AZURE_SUBSCRIPTION }} <- this one works fine!
resourceGroupName: "${{ env.resourceGroupName }}" <- Error: Resource Group rg-blue-$***GITHUB_REF#refs/heads/*** could not be found.
template: infrastructure/blue.bicep
parameters: storagePrefix=mystore
failOnStdErr: false
最佳答案
当您在 YAML 中分配值时,不会发生 Shell 参数扩展。换句话说,在此之后
env:
resourceGroupName: rg-blue-${GITHUB_REF#refs/heads/}
resourceGroupName
的值是文字字符串 rg-blue-${GITHUB_REF#refs/heads/}
。它似乎有效,因为当您使用
echo "${{ env.resourceGroupName}}"
这被替换为
echo "rg-blue-${GITHUB_REF#refs/heads/}"
然后shell进行扩展。您可以使用以下方法进行测试
echo '${{ env.resourceGroupName}}'
而不是抑制 shell 参数扩展。
要修复此问题,您可以使用单独的步骤来正确设置环境变量:
- name: Set env variable
run: |
echo "resourceGroupName=${GITHUB_REF#refs/heads/}" >> "$GITHUB_ENV"
并且没有事先在 env
中设置它。
关于azure - 环境变量并不总是在 GitHub Actions 工作流程文件中展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68808854/
我是一名优秀的程序员,十分优秀!