0

I have a js file like this, which should return me a string to use later in the action:

action.js

async function getData(){
 const url = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/Meet_Truffle%21.jpg/440px-Meet_Truffle%21.jpg";
 return url;
}

getData().then((url) => {
  return url;
});

I did a similar thing but it doesn't seem to work, can you tell me how I can do it?

Pull.yml

- name: Url
  run: node ./action.js >> $URL

- uses: suisei-cn/actions-download-file@v1
  id: downloadfile
  name: Download the file
  with:
       url: $URL
       target: assets/
Paul
  • 3,644
  • 9
  • 47
  • 113
  • Actually, you should first set the variable by using: `run: URL=$(node ./action.js)`, and then execute `run: echo "URL=$URL" >> $GITHUB_ENV` to set URL as environment variable in the workflow, before being able to use `url: ${{ env.URL }}` as action input. – GuiFalourd Mar 02 '22 at 19:01
  • @GuiFalourd: It doesn't seem to work. https://pastebin.com/MQMBZW7K – Paul Mar 02 '22 at 20:11
  • You need to use `run: |` (with a pipe) to run multiline commands (cf [this SO thread](https://stackoverflow.com/questions/56726429/how-to-run-multiple-commands-in-one-github-actions-docker)). That might be the issue in the link you shared. I added the full answer with the updated workflow below :) – GuiFalourd Mar 02 '22 at 20:18

1 Answers1

1

An option is to attribute the string to a variable before adding it as a ENV variable in the workflow Github Context.

However, to make it work, you can't use directly return url; in the .js file. You will also need console.log(url); to print the url value to the console.

Your workflow would look like this:

- name: Url
  run: |
     URL=$(node ./action.js)
     echo "URL=$URL" >> $GITHUB_ENV

- uses: suisei-cn/actions-download-file@v1
  id: downloadfile
  name: Download the file
  with:
       url: ${{ env.URL }}
       target: assets/

And the action.js file might look like this (I'm not familiar with node):

async function getData(){
 const url = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/Meet_Truffle%21.jpg/440px-Meet_Truffle%21.jpg";
 return url;
}

getData().then((url) => {
  console.log(url);
  return url;
});

You can find an example in this workflow run with python and node. The workflow implementation can be found here.

GuiFalourd
  • 15,523
  • 8
  • 44
  • 71
  • I tried this: https://pastebin.com/0k0scsLp Not working – Paul Mar 02 '22 at 20:23
  • Could you try adding a `echo $URL` command before adding the variable to the $GITHUB_ENV to check if it has been set as expected? – GuiFalourd Mar 02 '22 at 20:26
  • https://i.stack.imgur.com/kV50F.png https://i.stack.imgur.com/Us8ys.png – Paul Mar 02 '22 at 20:30
  • In that case, I believe the problem might be related to the way the `.js` file returns the url. Could you try **printing** it instead of returning it in the `action.js` file? – GuiFalourd Mar 02 '22 at 20:31
  • What do you intend to print it? – Paul Mar 02 '22 at 20:32
  • You can see above what the js file is like. – Paul Mar 02 '22 at 20:33
  • I've tested it using python, you can find the workflow run [here](https://github.com/GuillaumeFalourd/poc-github-actions/runs/5397973830?check_suite_focus=true) and the workflow file [here](https://github.com/GuillaumeFalourd/poc-github-actions/blob/main/.github/workflows/workflow-tester24.yml) and the python script [here](https://github.com/GuillaumeFalourd/poc-github-actions/blob/main/test_workflow24.py). However, to make it work, I add to use `print` in python, it doesn't work with return. Will make a test with node. – GuiFalourd Mar 02 '22 at 20:36
  • I made it work using `console.log("url");` instead of return. Here is the [workflow run](https://github.com/GuillaumeFalourd/poc-github-actions/actions/runs/1924534140) and the [.js file](https://github.com/GuillaumeFalourd/poc-github-actions/blob/main/test_workflow24.js). I updated the answer. – GuiFalourd Mar 02 '22 at 20:40
  • 1
    Thx, so the return is of no use from what I understand. – Paul Mar 03 '22 at 11:04
  • It's of no use if you only use the file on a github action workflow. If you use it somewhere else it can still be useful. – GuiFalourd Mar 03 '22 at 12:06
  • I understand, does env accept string array format? – Paul Mar 03 '22 at 16:51
  • https://user-images.githubusercontent.com/20476002/156613112-aae62ba5-72c1-48f3-a3cd-cc2fbd32f824.png – Paul Mar 03 '22 at 17:03
  • I don't think you can add the array directly in an env variable, but it could work using an [object](https://docs.github.com/en/actions/learn-github-actions/expressions#object-filters). I saw you ask another question here: https://stackoverflow.com/questions/71341486/github-action-assign-a-variable-of-type-env-an-array-of-strings – GuiFalourd Mar 03 '22 at 18:10