Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use ${{ toJson(github) }} when a ' appears in the commit message. #1656

Closed
petersutter5 opened this issue Feb 3, 2022 · 6 comments
Closed
Assignees

Comments

@petersutter5
Copy link

Describe the bug
When trying to use ${{ toJson(github) }} when there is a ' in the commit message, you get an error. The error looks like,
unexpected EOF while looking for matching `"'

To Reproduce
Steps to reproduce the behavior:

  1. Have a workflow bash script that tries to run ${{ toJson(github) }} that runs on a PR
  2. Leave a commit message including a ' character
  3. Trigger the workflow by opening a PR
  4. You should see the toJson step fail with the error message above.

Expected behavior
Expected to see the full pretty printed github context

Runner Version and Platform

Current runner version: '2.287.1'

OS of the machine running the runner? OSX/Windows/Linux/...
Ubuntu
20.04.3
LTS

What's not working?

toJson expression

Job Log Output

unexpected EOF while looking for matching `"'

@petersutter5 petersutter5 added the bug Something isn't working label Feb 3, 2022
@nikola-jokic nikola-jokic self-assigned this Feb 4, 2022
@nikola-jokic
Copy link
Contributor

Hi @petersutter5, thank you for submitting this issue!

We are reproducing this issue now and we will get back to you as soon as we can.

@nikola-jokic
Copy link
Contributor

Hi @petersutter5,

I reproduced the issue. toJSON will create a JSON object, and since ' is a valid thing to be inside the name (strings are enclosed using "), it will not be escaped. Now, if you are using an expression like echo '${{ toJSON(github) }}', it will trigger a parsing error when it gets to the ' inside the "title".

One suggestion can be to pass in the context as env variable, and then printing or using it would not cause an issue:

jobs: 
  build:
    runs-on: [ ubuntu-latest ]
    steps:
      - name: Dump GitHub context
        id: github_context_step
        run: echo $JSON
        env:
          JSON: ${{ toJSON(github) }}

This is a bash error, and you can take advantage of bash's built-in escaping of env variables to solve the problem like in the workflow above.

@ethomson ethomson removed the bug Something isn't working label Feb 4, 2022
@petersutter5
Copy link
Author

This works. Thank you very much for the answer!

@dkrahmer
Copy link

I encountered this same issue. I am outputting the github object but it can be quite large and I am concerned with assigning the JSON text to an environment variable because of the risk of exceeding the max size. It would be nice if there were a "replace" function that could be used like this:
${{ replace(toJSON(github), "'", "\\'") }} (sample, not real code)

@atsu85
Copy link

atsu85 commented Sep 8, 2022

@nikola-jokic , thanks for the workaround, but
note that official documentation
also contains example that triggers this issue, without warning about escaping issues
image
It would be nice to add second warning there about this issue and potential workaround

Guy7B added a commit to jitsecurity-controls/jit-github-action that referenced this issue Mar 5, 2023
with this special character in their description "`"

Solution found here:
actions/runner#1656 (comment)
Guy7B added a commit to jitsecurity-controls/jit-github-action that referenced this issue Mar 5, 2023
with this special character in their description "`" and "'"

Solution found here:
actions/runner#1656 (comment)
jsduenass added a commit to jsduenass/ros2_documentation that referenced this issue Apr 13, 2023
@jc-clark
Copy link

jc-clark commented Jun 6, 2023

📚👋 Hey @atsu85, just a quick follow up from GitHub's documentation team.

note that official documentation
also contains example that triggers this issue, without warning about escaping issues

We just merged some changes to this example which should fix the issue in question. Setting the contexts as an environment variable helps mitigate the ' issue. Let us know if you see other issues like this in the GitHub Actions workflow examples.

We always welcome any contributions, check out our contribution guidelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants