Anant Arun
Anant Arun

Reputation: 93

Not able to dynamically access secret variable value of github using yaml into github action workflow by giving the secret key to another variable

I have 2 repositories secrets in Git Hub: Secret_Key1 and Secret_Key2, I also have a YAML file. Ex:

system1:
  port: 3333
  user: user1
  key: Secret_Key1
system2:
  port: 4444
  user: user2
  key: Secret_Key2

Now, in my workflow I am taking choice input(via workflow dispatch) with system1 or system2, using setup-yq, I can get key values from the above YAML file. Let's say I chose system1 and from the setup-yq step I can get Secret_Key1(secret key name) and I plan to fetch its value into the key variable in GitHub steps, but I cannot use the key to utilize its secret.

My Workflow step Example:

on: 
workflow-dispatch:
  inputs:
    Target_System:
      description: 'Target_System'
      required: true
      default: system1
      type: choice
      options:
        - system1
        - system2
jobs:
  setup:
    runs-on: [hosted-runner]
    steps:
    - name: setup yq
      uses: setup-yq@v2
    - name: Extract Value From Config
      run:
       system_name="${{ inputs.system_name }}"
       key=$(yq eval ".${system_name}.key" "config.yaml") #working, it can get secret key name
       #I tried several ways below, but none seems to be working
       #way 1
       echo "key=$key" >> $GITHUB_ENV
       echo "${{ secrets[env.key] }}" #didn't worked
       #way 2
       key=${{ secrets[format('{0}',env.key]) }}
       echo "$key" #didn't worked

Note:

  1. I am not printing secret variables in my actual action workflow here is just a template/example of my actual workflow, not the actual workflow.
  2. I also tried to to give the output of yq into the step's env block but that way, I was not able to utilize it.
  3. How Secret got added: Repository > settings > secrets and variables > actions > New Repository Secrets . I am able to access and use secrets directly, but I want to use it in a way explained above.

Upvotes: -1

Views: 40

Answers (0)

Related Questions