Home > Blockchain >  Github Action js file return string to be saved in a variable
Github Action js file return string to be saved in a variable

Time:03-04

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!.jpg/440px-Meet_Truffle!.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/

CodePudding user response:

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!.jpg/440px-Meet_Truffle!.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.

  • Related