Home > Blockchain >  Github action assign a variable of type env an array of strings
Github action assign a variable of type env an array of strings

Time:03-05

I am having the following problem what you see in the image.

I have a.js file which returns me an array of url strings. Then I should assign this array to an env variable as seen in the example below, but I get the following error.

You can tell me where I'm wrong.

enter image description here

on: 
  workflow_dispatch:
  
name: Test Download Multifile
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        name: Check out current commit
     
      - name: Url
        run: |
         URL=$(node ./actionMultifile.js)
         echo $URL
         echo "URL=$URL" >> $GITHUB_ENV

actionMultifile.js

async function getData(){
 const url = [
   "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Tyrannosaurus_rex_Sue_at_FMNH.jpg/440px-Tyrannosaurus_rex_Sue_at_FMNH.jpg",
   "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Mosasaurus_beaugei_34.JPG/440px-Mosasaurus_beaugei_34.JPG"
 ]
 return url;
}

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

CodePudding user response:

The problem occurs because you need to set the URL env variable using multiline strings

This is because the URL variable here isn't saved like this:

['https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Tyrannosaurus_rex_Sue_at_FMNH.jpg/440px-Tyrannosaurus_rex_Sue_at_FMNH.jpg', 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Mosasaurus_beaugei_34.JPG/440px-Mosasaurus_beaugei_34.JPG']

But like this:

[
'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Tyrannosaurus_rex_Sue_at_FMNH.jpg/440px-Tyrannosaurus_rex_Sue_at_FMNH.jpg',
'https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Mosasaurus_beaugei_34.JPG/440px-Mosasaurus_beaugei_34.JPG'
]

Therefore, you workflow should look like this:

    steps:
      - name: Checkout repository content
        uses: actions/checkout@v2 # Checkout the repository content to github runner.

      - name: Setup Node Version
        uses: actions/setup-node@v2
        with:
          node-version: 14 # Install the node version needed

      - name: set multiline env var
        run: |
          echo 'URL<<EOF' >> $GITHUB_ENV
          node ./actionMultifile.js >> $GITHUB_ENV
          echo 'EOF' >> $GITHUB_ENV

Then, be careful, you won't be able to use the ${{ env.URL }} variable directly, otherwise it will only print the first line ([) and break. You need to use it between quotes (").

For example:

      - name: use env var
        run: echo "${{ env.URL }}"

      - name: use env var directly
        run: echo "$URL"

I tested it here if you want to take a look:

  • Related