Read from file and assign to Env variable

In my GitHub Workflow, I want to read a properties file. The content of the file will be release_version=1.0.0 . I want to read the content and assign the value of 1.0.0 to an environment variable (say RELEASE_VERSION) in a step and use it in subsequent steps. I referred the link Workflow Commands for GitHub Actions, but can’t get it working. Can anyone please help me?

Following is the snippet from my workflow.

 - name: Read
        id: version
        uses: juliangruber/read-file-action@v1
          path: ./build/

 - name: Assign release version from to Env variable
        run: |
           echo "${{ steps.version.outputs.content }}" >> $GITHUB_ENV          
  - name: Echo release version from  Env variable
         run: |
            echo "${{ env.release_version }}"
1 Like

That should work assuming the workflow isn’t running on a Windows runner, for that case there’s a warning box right above the documentation section you linked.

However I’m confused why you’d need to use an action to read the properties file, if the content is as simple as you describe. You could cut out the action as a potential source of bugs by reading the properties file and writing directly to the environment file:

 - name: Assign release version from to Env variable
   run: |
      cat build/ >> $GITHUB_ENV          

The workflow logs should show you how the environment looks on each step, so you can check if release_version is present.

1 Like

First of all, thanks a lot for your quick response. I forgot to mention that it is indeed running on a Windows runner. If I read the file, I get the content as “release_version=1.0.0”. However, I want the value (1.0.0) from the file to be assigned to a variable (say RELEASE_VERSION), so that I can use it in subsequent steps.

Then you’ll need to take a look at that warning box about how to write to GITHUB_ENV on Windows.

The format name=value is exactly what GITHUB_ENV needs, that’s why I suggested to just copy the contents directly. :wink: Only if you want to reformat it (e.g. to use a different name) you’ll have to use an intermediate step.