Hello, I have a job that looks something like thi...
# github-actions
s
Hello, I have a job that looks something like this:
Copy code
jobs:
  pop-values-from-list:
    runs-on: my-runner
    env:
      MY_LIST: '[1,2,3,4,5,6,7,8,9,0]' # list has 10 items
    steps:
      - name: Pop 10% of list
        run: |
          pop_my_list() {
            percentage=$1
            list=$2
            MY_LIST=$(process_and_pop $percentage $list) >>"$GITHUB_OUTPUT"
          }
          pop_my_list 10% $MY_LIST # MY_LIST now has 9 items
      - run: WaitForAsyncProcess
      - name: Pop 20% of list
        run: |
          pop_my_list() {
            percentage=$1
            list=$2
            MY_LIST=$(process_and_pop $percentage $list) >>"$GITHUB_OUTPUT"
          }
          pop_my_list 20% $MY_LIST # let's assume that MY_LIST now has 7 items
      - run: WaitForAsyncProcess
...
      - name: Pop everything that remains in list
        ...
This is of course a simplified version of what I am trying to achieve. The problem here is that I have to keep repeating the function definition for
pop_my_list()
Is there a way I can have the function defined once and invoke it across steps without repetition?
Figured it out. What I did is that I copied the function definition to a file (escaping the
$
symbols) in the first step, then sourced the file in subsequent steps before calling the function
👍 1
i
@Saichovsky, nice workaround.
🤝 1
m
You could also consider putting the function in an action (either Dockerfile or Javascript based) and then call it as a true action in your workflow. then could also be used in other repos.