-1

I am trying to pass variable poetry-config with multiple values:

jobs:
  build:
    runs-on: [ ubuntu-latest ]
    steps:
    - uses: actions/checkout@v3
    - name: Install poetry (with github.com token)
      uses: ./actions/setup-poetry
      with:
        python-version: "3.10"
        poetry-version: "1.4.2"
        poetry-project: .
        poetry-install-arguments: "--only main --no-root"
        poetry-config: |
          "http-basic.abc1 test1 value1"
          "http-basic.abc2 test2 value2"

In action.yml, I need to run the command:

poetry config http-basic.abc1 test1 value1
poetry config http-basic.abc2 test2 value2

My current action.yml is:

name: Setup Poetry
description: Set up python and poetry

runs:
  using: "composite"
  steps:
    - name: poetry config
      shell: bash
      run: poetry config ${{ inputs.poetry-config }}
      working-directory: ${{ inputs.poetry-project }}

How can I split the poetry-config and iterate step on it?

Azeem
  • 11,148
  • 4
  • 27
  • 40

1 Answers1

0

you want to run these two commands:

poetry config http-basic.abc1 test1 value1
poetry config http-basic.abc2 test2 value2

of which only the beginning part is fixed and the rest is configuration data:

poetry config

          http-basic.abc1 test1 value1
          http-basic.abc2 test2 value2

so you have two records with three fields each:

          http-basic.abc1 test1 value1
          http-basic.abc2 test2 value2

as you already learned, you can provide this as input to your action, just don't quote the records:

        poetry-config: |
          http-basic.abc1 test1 value1
          http-basic.abc2 test2 value2

and then when you "run" the input, make it a pipeline¹ with xargs(1)² on the right hand side executing each records fields as arguments of the poetry config command:

      run: echo '${{ inputs.poetry-config }}' | xargs -L1 poetry config

This will then run one invocation of poetry config per line in the input.

If the line is empty, it won't run for that line.

If there is no line or a single empty line, it would run poetry config once (without any other arguments), unless you add the -r switch:

      run: echo '${{ inputs.poetry-config }}' | xargs -rL1 poetry config

this then supports no input and up to n lines.

finally (simplified):

jobs:
  job:
    runs-on: [ ubuntu-latest ]
    steps:
    - uses: ./actions/setup
      with:
        config: |
          http-basic.abc1 test1 value1
          http-basic.abc2 test2 value2

./actions/setup/action.yml

name: setup action

runs:
  using: "composite"
  steps:
    - shell: bash
      run: |
        echo '${{ inputs.config }}' | 
          xargs -rL1 poetry config

¹ 2.9.2 Pipelines https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_02

² xargs(1) https://manpages.debian.org/stretch/findutils/xargs.1.en.html

hakre
  • 193,403
  • 52
  • 435
  • 836