Skip to content

Is there any possibility to use file as a script? #38

Closed

Description

@extempl

Maybe using cat file or something?
If no, is it possible to add?

Because it is not that easy to write normal js code inside yaml.

Activity

  1. extempl commented on Apr 17, 2020

    @extempl
    Author

    End up with following solution:

          - run: echo "::set-output name=js::$(tr '\n' ' ' < ./.github/diffWithMaster.js)"
            id: diffJS
            name: service run
    
          - name: Getting diff with master
            id: diff
            uses: actions/[email protected]
            with:
              any_variable: ${{github.event.ref || github.event.pull_request.head.ref}} 
              github-token: ${{secrets.GITHUB_TOKEN}}
              result-encoding: string
              script: ${{ steps.diffJS.outputs.js }}
    

    tr '\n' ' ' for merging file into one line, so make sure you have ; in the js everywhere and don't use // comments, use block one instead.
    Also, so far this is the only solution to nicely merge output (echo won't work on github actions for some reason).

    Also note that in this case need to throw external variables inside script through with, and read in the script with core.getInput('any_variable').

    Js script may be safely wrapped into async function to get correct syntax and avoid errors:

    /* global github, context, core */
    (async () => {
    /*....code (remember about semicolons) */
    })();
    

    But in this case you won't be able to retrieve any returns from the script. This probably could be solved on the github-script side.

  2. DannyvanderJagt commented on Apr 21, 2020

    @DannyvanderJagt

    Using files instead of strings would be nice. Will save a lot of code in my .yml files and allows me to re-use scripts I already use for other stuff.

    I made a local PR to add file support. I'm testing it now and will create a PR here tomorrow.

  3. lukka commented on May 15, 2020

    @lukka

    Having arbitrary code to run inside to the yaml file, it is not a good idea, for many reasons:

    • not testable with unit test;
    • not manually testable;
    • maintenance headache;

    IMO yaml is good to contain declarative statements only and it should be as concise as possible.

    Having file as an input should be coming from the design review, not from customer feedback.

  4. extempl commented on May 15, 2020

    @extempl
    Author

    Having file as an input should be coming from the design review, not from customer feedback.

    In fairness, it is true when you selling your product, and there are customers. Otherwise, when you building some tool for yourself and publishing to help others - it is not really fair to say like that :)

  5. lukka commented on May 15, 2020

    @lukka

    My fault, I completely missed this action is experimental indeed as stated in the README.md file. I take it back :)

  6. added a commit that references this issue on May 15, 2020
  7. jclem commented on May 18, 2020

    @jclem
    Contributor

    @lukka We're not going to remove the ability to inline the script—we've seen that there's a huge demand for it in development lifecycle flows where a user just wants to write one file that accomplishes the task of something like commenting on an issue. Also, I use it myself a huge amount in quick one-off workflows that I don't want to put much effort into 😆

    That said, I also think there's value in being able to run a file, and I plan on adding this soon.

  8. jclem commented on May 18, 2020

    @jclem
    Contributor

    The more I think about it, the less I'm convinced that adding this is necessary. If you want to run a file instead of a plain script, it can be done this way:

    uses: actions/[email protected]
      with:
        script: |
          const path = require('path')
          return require(path.resolve('my-script.js'))({context})

    And then, in your file:

    module.exports = ({context}) => {
      return context.payload.client_payload.value
    }

    It's also safe to use a module exporting an async function in this way as long as you await it in the inline script.

  9. extempl commented on May 18, 2020

    @extempl
    Author

    @jclem I think any solution would work, as long as it is documented. So, yours actually looks great, but I'd add it to the readme.

  10. jclem commented on May 18, 2020

    @jclem
    Contributor
  11. jclem commented on May 18, 2020

    @jclem
    Contributor

    In the meantime, I'm going to go ahead and close this issue.

    While it's not quite as simple on the user-side as adding a file as an input to the Action, it has the benefit of:

    • Letting the external file be a testable module
    • Keeping this action focused on its goal of enabling quick one-off actions contained entirely in the workflow file itself
  12. extempl commented on May 18, 2020

    @extempl
    Author

    Yes, the only thing is missing I think, is the note about mandatory checkout

  13. jclem commented on May 18, 2020

    @jclem
    Contributor

    the note about mandatory checkout

    Good call, I will add that.

  14. sr-apotapov commented on Apr 5, 2021

    @sr-apotapov

    While it's not quite as simple on the user-side as adding a file as an input to the Action, it has the benefit of:

    • Letting the external file be a testable module
    • Keeping this action focused on its goal of enabling quick one-off actions contained entirely in the workflow file itself

    @jclem Can you please explain how exactly can people use it?
    For example I'm trying to use {{secret.NAME}} and {{steps.NAME.outputs.stdout}} context. What should I pass exactly? in yaml and how should I change my JS code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions