英文:
How to dynamically use github secrets in a github action, according to list of secret names provided?
问题
以下是您要翻译的内容:
"What i'm trying to achieve, is eventually to dispatch an action, with list of comma separated secret names.
What i want to do is to loop over the names and use the secrets, from this example - just print them.
I have 2 secrets SEC_1 and SEC_2 in my repo.
jobs:
dev:
name: dev
runs-on: ubuntu-22.04
env:
ENVIRONMENT: DEV
steps:
- run: |
# Define the secrets
secrets="SEC_1,SEC_2"
# Split the secrets string into an array
IFS=',' read -ra secret_array <<< "$secrets"
# Loop over the secret names and echo their values
for secret_name in "${secret_array[@]}"; do
echo ${{ format('{0}', '$secret_name') }}
# This prints nothing!
echo ${{ secrets[format('{0}', '$secret_name')] }} | sed 's/./& /g'
done
# This works perfectly
echo ${{ secrets[format('{0}', 'SEC_1')] }} | sed 's/./& /g'
echo ${{ secrets[format('{0}', 'SEC_2')] }} | sed 's/./& /g'
i've tried some solutions from here - but nothing works as i don't have fixed number of variables i can define as env vars
What am i missing?
As per the comment, managed to do it, thanks @Azeem!
on:
workflow_dispatch:
inputs:
my_secrets:
description: "list of secrets to fetch"
required: true
permissions:
contents: read
jobs:
dev:
name: dev
runs-on: ubuntu-22.04
env:
SECRETS: '${{ toJson(secrets) }}'
steps:
- run: |
IFS=',' read -ra string_array <<< ${{ github.event.inputs.my_secrets }}
for secret in "${string_array[@]}"; do
value="$(jq -r --arg key "$secret" '.[$key]' <<< "$SECRETS" | sed 's|.|& |g')"
echo "$secret: $value"
done"
希望这能帮助到您。
英文:
What i'm trying to achieve, is eventually to dispatch an action, with list of comma separated secret names.
What i want to do is to loop over the names and use the secrets, from this example - just print them.
I have 2 secrets SEC_1 and SEC_2 in my repo.
jobs:
dev:
name: dev
runs-on: ubuntu-22.04
env:
ENVIRONMENT: DEV
steps:
- run: |
# Define the secrets
secrets="SEC_1,SEC_2"
# Split the secrets string into an array
IFS=',' read -ra secret_array <<< "$secrets"
# Loop over the secret names and echo their values
for secret_name in "${secret_array[@]}"; do
echo ${{ format('{0}', '$secret_name') }}
# This prints nothing!
echo ${{ secrets[format('{0}', '$secret_name')] }} | sed 's/./& /g'
done
# This works perfectly
echo ${{ secrets[format('{0}', 'SEC_1')] }} | sed 's/./& /g'
echo ${{ secrets[format('{0}', 'SEC_2')] }} | sed 's/./& /g'
i've tried some solutions from here - but nothing works as i don't have fixed number of variables i can define as env vars
What am i missing?
As per the comment, managed to do it, thanks @Azeem!
on:
workflow_dispatch:
inputs:
my_secrets:
description: "list of secrets to fetch"
required: true
permissions:
contents: read
jobs:
dev:
name: dev
runs-on: ubuntu-22.04
env:
SECRETS: '${{ toJson(secrets) }}'
steps:
- run: |
IFS=',' read -ra string_array <<< ${{ github.event.inputs.my_secrets }}
for secret in "${string_array[@]}"; do
value="$(jq -r --arg key "$secret" '.[$key]' <<< "$SECRETS" | sed 's|.|& |g')"
echo "$secret: $value"
done
答案1
得分: 1
你可以使用 toJSON
函数将 secrets
上下文转换为 JSON 格式,然后使用 jq
查询你的动态密钥。
以下是一个示例:
name: dynamic_secrets
on: workflow_dispatch
jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: Check
env:
SECRETS: '${{ toJson(secrets) }}'
run: |
for secret in SECRET1 SECRET2; do
value="$(jq -r --arg key "$secret" '.[$key]' <<< "$SECRETS" | sed 's|.|& |g')"
echo "$secret: $value"
done
输出:
参考链接:
英文:
You can convert the secrets
context to JSON with toJSON
function and then use jq
to query your dynamic secrets.
Here's an example:
name: dynamic_secrets
on: workflow_dispatch
jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: Check
env:
SECRETS: '${{ toJson(secrets) }}'
run: |
for secret in SECRET1 SECRET2; do
value="$(jq -r --arg key "$secret" '.[$key]' <<< "$SECRETS" | sed 's|.|& |g')"
echo "$secret: $value"
done
Output:
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论