Labeled action on pull_request

For the life of me I cannot figure this out. What exactly is value of github.event.pull_request.labels? Based on the developer guide, it should be an array of objects, but here, it prints Array, and no matter how I try to loop it in Bash, it prints Array. Is it literally the string “Array”? So how do I find out specific labels that have been applied to a specific pull request? 

name: test

on:
  pull_request:
    branches:
      - master
    types: [labeled]

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
    - name: current branch
      run: |
        array = ${{ github.event.pull_request.labels }}
        echo "${array[*]}"

If you use this step to output the entire github context you can see what the JSON looks like.

- name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"

I ran it for a PR with two labels and the context output looks like this.

"labels": [
      {
        "color": "ededed",
        "default": false,
        "id": 1579077344,
        "name": "automated pr",
        "node_id": "MDU6TGFiZWwxNTc5MDc3MzQ0",
        "url": "https://api.github.com/repos/peter-evans/actions-playground/labels/automated%20pr"
      },
      {
        "color": "ededed",
        "default": false,
        "id": 1441562142,
        "name": "report",
        "node_id": "MDU6TGFiZWwxNDQxNTYyMTQy",
        "url": "https://api.github.com/repos/peter-evans/actions-playground/labels/report"
      }
    ],
2 Likes

Thanks… I’ve been trying so hard to loop it as is but to no avail. Do I have to run toJSON() to be able to interact with it? Even the normal bash syntax to access the values such as array[0] prints Array[0]

This question/answer might help you.

https://stackoverflow.com/questions/59588605/how-to-check-for-a-label-in-a-github-action-condition

Hello.

I’ve tried the solution given in slackoverflow above, but couldn’t get the label name.
Could you help me?

my-actions.yaml

name: label
on:
  pull_request:
    branches:
      - master
jobs:
  label:
    name: Run only when the labels do not include "dependencies"
    if: ${{ !contains(github.event.pull_request.labels.*.names, 'dependencies') }}
    runs-on: ubuntu-latest
    steps:
      - name: debug
        run: |
          echo "${{ toJson(github.event.pull_request.labels) }}"
          echo "${{ toJson(github.event.pull_request.labels.*.names) }}"

and gets

[
  {
    color: 0366d6,
    default: false,
    description: Pull requests that update a dependency file,
    id: 2100415986,
    name: dependencies,
    node_id: MDU6TGFiZWwyMTAwNDE1OTg2,
    url: https://api.github.com/repos/epaew/eslint-plugin-filenames-simple/labels/dependencies
  }
]
[]

Sorry, I solved it myself…

-          echo "${{ toJson(github.event.pull_request.labels.*.names) }}"
+          echo "${{ toJson(github.event.pull_request.labels.*.name) }}"