Re-use previous run

Hi,

I would like to call a function from a sh script on a different run. Both functions are in the same script (my_script.sh). However, I would like to have a different “step name” on my GitHub action.

How can I achieve that?

The code below generates an error: my_function_2: command not found. It seems I need to call again source my_script.sh which I do not want.

- name: Setup My script
  run: |
    source my_script.sh
    my_function_1
- name: Call function2
  run: |
    # my_function_2 is defined in my_script
    my_function_2

thank you for your feedback

Each step gets a fresh Bash instance, so all loaded functions are lost, and you’ll have to source the script file again. Though in my opinion it’d be nicer to handle the function selection with command line options (maybe a subcommand?) instead of sourcing the script and then calling the function.

If sourcing/running the script twice creates undesirable side effects that may be the real problem to address.

Thank you @airtower-luna for your feedback.

I will follow your advice and review the design of my script and Github action.

Oh, one thing I forgot earlier: You can create groups within the output of one step, with the group and endgroup workflow commands. Not sure if that’d fit your needs, but it’d work like this:

    - run: |
        source my_script.sh
        echo "::group::Run function 1"
        my_function_1
        echo "::endgroup::"
        echo "::group::Run function 2"
        my_function_2
        echo "::endgroup::"

In the log view this will create a folding structure inside the step.