Fromjson throws error when used in jobs.if

I’m running into an issue I’m hoping someone else can help shed some light on. I want to be able to run a github action only for deployment events that have a matching key in the deployment payload (encoded as json – taken from https://docs.github.com/en/rest/reference/repos#create-a-deployment--parameters).

The following works and prints out true when run:

on: 
  - deployment
jobs:
  do_something:
    timeout-minutes: 1
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "The deployment key is: ${{ fromJSON(github.event.deployment.payload).someKey == 'expectedValue' }}"

However, if I try using this in jobs.if it throws an error when run (GitHub Actions has encountered an internal error when running your job.):

on: 
  - deployment
jobs:
  do_something:
    timeout-minutes: 1
    runs-on: ubuntu-latest
    if: fromJSON(github.event.deployment.payload).someKey == 'expectedValue'
    steps:
    - run: |
        echo "The deployment key is: ${{ fromJSON(github.event.deployment.payload).someKey == 'expectedValue' }}"

image

It would seem to me that the above should work. Out of curiosity I did try running using the contains function and it worked as expected (very hacky though).

on: 
  - deployment
jobs:
  do_something:
    timeout-minutes: 1
    runs-on: ubuntu-latest
    if: contains(github.event.deployment.payload,'expectedValue')
    steps:
    - run: |
        echo "The deployment key is: ${{ fromJSON(github.event.deployment.payload).someKey == 'expectedValue' }}"

Any ideas would be greatly appreciated!

Hi @ejhayes,

Glad to see you in Github Community Forum!

The payload value can be got from github context, please use below if expression:

if: github.event.deployment.payload.someKey=='expectedValue'
steps:
....

fromJSON(value) will return a JSON object for value. I tried to echo the payload with fromJson, it reports error. What did you set in the deployment payload?

Thanks.

Hey @weide-zhou – here’s how I created the deployment (using the github client from @actions/github npm package:

github.repos.createDeployment({
    owner: 'someowner',
    repo: 'somerepo',
    ref: 'develop',
    auto_merge: false,
    required_contexts: [],
    task: 'deploy',
    description: 'somedescription',
    environment: `development`,
    headers: {
        'Accept': 'application/vnd.github.ant-man-preview+json, application/vnd.github.flash-preview+json'
    },
    payload: JSON.stringify({
        someKey: 'expectedValue'
    })
})

When I go to look for the deployment, I can see that the JSON payload contains the expected key/value:

$ curl https://api.github.com/repos/someowner/somerepo/deployments -H 'Authorization: token blahblah' | jq
[
  {
    "url": "https://api.github.com/repos/someowner/somerepo/deployments/249078576",
    "id": 249078576,
    "node_id": "****",
    "sha": "****",
    "ref": "develop",
    "task": "deploy",
    "payload": "{\"someKey\":\"expectedValue\"}",
    "original_environment": "development",
    "environment": "development",
    "description": "somedescription",
    "creator": {
      "login": "ejhayes",
      "id": 1234,
      "node_id": "****",
      "avatar_url": "https://avatars0.githubusercontent.com/u/****?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/ejhayes",
      "html_url": "https://github.com/ejhayes",
      "followers_url": "https://api.github.com/users/ejhayes/followers",
      "following_url": "https://api.github.com/users/ejhayes/following{/other_user}",
      "gists_url": "https://api.github.com/users/ejhayes/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/ejhayes/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/ejhayes/subscriptions",
      "organizations_url": "https://api.github.com/users/ejhayes/orgs",
      "repos_url": "https://api.github.com/users/ejhayes/repos",
      "events_url": "https://api.github.com/users/ejhayes/events{/privacy}",
      "received_events_url": "https://api.github.com/users/ejhayes/received_events",
      "type": "User",
      "site_admin": false
    },
    "created_at": "2020-07-16T14:49:54Z",
    "updated_at": "2020-07-16T14:49:54Z",
    "statuses_url": "https://api.github.com/repos/someowner/somerepo/deployments/249078576/statuses",
    "repository_url": "https://api.github.com/repos/someowner/somerepo",
    "transient_environment": false,
    "production_environment": false
  }
]

Hi @ejhayes,

Thanks for your reply!

You cannot omit the ${{ }} in your if expression since payload is in json body, please use below code:

if: ${{ fromJSON(github.event.deployment.payload).someKey == 'expectedValue' }}

It works in my workflow.

Thanks.

Hi @ejhayes,

Just wana confirm if it’s resolved by adding ${{ }} to fromjson? Please let me know if there are still any errors of the if expression.

Thanks