How to view inputs for an action using github-cli while the action is running?

838 Views Asked by At

Is there a way to use the github-cli or api to view the inputs of an action while it is running?


I want to allow Github actions to run concurrently. The resources they will manage are determined by the input stack_name. I want to make sure two pipelines cannot run at the same time with the same stack_name input. If this happens, then I want one of the pipeline actions to fail and stop immediately.

I am also taking the input and turning it into an environmental variable for one of my jobs. After the job finishes, the values are available in the logs and I can grep through the following output to get a pipelines stack_name:

$ gh run view $running_pipeline_id --repo=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY --log
....
env-check   env-check   2022-03-22T17:06:30.2615395Z   STACK_NAME: foo

However, this is not available while a job is running and I instead get this error:

run 1234567890 is still in progress; logs will be available when it is complete

Here is my current attempt at a code block that can achieve this. I could also use suggestions on how to make better gh run list and/or gh run view calls that can avoid using grep and awk. Clean json output I can parse with jq is preferable.

  set +e
  running_pipeline_ids=$(gh run list --workflow=$SLEEVE --repo=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY \
    | grep 'in_progress' \
    | awk '{print $((NF-2))}' \
    | grep -v $GITHUB_RUN_ID)
  set -e
  for running_pipeline_id in $running_pipeline_ids; do
    # get the stack name for all other running pipelines
    running_pipeline_stack_name=$(gh run view $running_pipeline_id --repo=$GITHUB_SERVER_URL/$GITHUB_REPOSITORY --log \
      | grep 'STACK_NAME:' | head -n 1 \
      | awk -F "STACK_NAME:" '{print $2}' | awk '{print $1}')
    # fail if we detect another pipeline running against the same stack
    if [ "$running_pipeline_stack_name" == "$STACK_NAME" ]; then
      echo "ERROR: concurrent pipeline detected. $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$running_pipeline_id"
      echo "Please try again after the running pipeline has completed."
      exit 1
    fi
  done
1

There are 1 best solutions below

0
rethab On

Perhaps you could use the concurrency feature of GitHub Actions?

Now you cannot directly bake this into an action, but if it's possible for you to extract your action into a reusable workflow then you could make use of the concurrency feature.

It would look something like this:

# ./github/workflows/partial.yaml
on:
  workflow_call:
    inputs:
      stack-name:
        description: "name of the stack"
        required: true
        type: string

jobs:
  greet:
    runs-on: ubuntu-latest
    concurrency:
      group: ${{ inputs.stack-name }}
      cancel-in-progress: true
    steps:
      - uses: my/other-action
        with:
          stack_name: ${{ inputs.stack-name }}

And then where you're using it:

jobs:
  test:
    uses: my/app-repo/.github/workflows/partial.yml@main
    with:
      stack-name: 'my-stack'