gpt4 book ai didi

azure - 环境变量并不总是在 GitHub Actions 工作流程文件中展开

转载 作者:行者123 更新时间:2023-12-03 04:45:23 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com