Last Updated on 2022-06-24 by James Croft

GitHub Actions is becoming quite robust with a wide variety of actions you can take advantage of in your workflows. For those unaware, these are workflows for your open-source GitHub repositories to automate jobs like build and release pipelines.

As your workflows become complex, you might come across scenarios where you want to automate setting environment variables within them to use later.

A good example of this that I came across recently, getting the build version number from the branch name. After, I could use the output environment variable to generate NuGet packages with that specific version number.

Below is an example of how you can set environment variables in your GitHub Actions workflows using PowerShell.

Setting an environment variable with PowerShell in GitHub Actions

The important line here is the echo which takes my variable and assigns it to BUILD_VERSION. Afterward, the variable is appended to the GITHUB_ENV variables for you to use in the rest of your GitHub Actions workflow.

Here’s the line to copy into your workflow.

echo "MY_VARIABLE=hello" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append

You can then use the environment variable as you do ones set manually in your workflow, for example, $MY_VARIABLE.

Setting an environment variable with other scripting languages

GitHub’s documentation contains an example of how you can also achieve this using other scripting languages.

Essentially, the line is very similar and is shown below.

echo "MY_VARIABLE=hello" >> $GITHUB_ENV

You can find more information on working with Workflow commands for GitHub Actions on GitHub docs.

Found this article useful?

Thank you for taking the time to read this article. I’m committing to sharing more of my knowledge through my blog and open-source projects! You’ll also catch me in casual conversation on Bluesky and LinkedIn too.

If you enjoy what you see, please consider subscribing to get a notification when new articles go live!


Discover more from James Croft

Subscribe to get the latest posts sent to your email.

James Croft

James is a senior software engineer at Microsoft with over 10 years experience designing and building large-scale, distributed, cloud-native systems. He's deeply experienced in C#, Python, and TypeScript, with specialization in AI agent architectures, retrieval-augmented generation (RAG), and production-grade Azure systems.

3 Comments

  • Dominik says:

    Thank you for the article, but your PS example fails with:
    “`
    Out-File : Cannot validate argument on parameter ‘Encoding’. The argument “utf-8” does not belong to the set
    “unknown,string,unicode,bigendianunicode,utf8,utf7,utf32,ascii,default,oem” specified by the ValidateSet at
    tribute. Supply an argument that is in the set and then try the command again.
    “`

    so, the correct encoding value should be “utf8” (not “utf-8”)

  • Randy Pagels says:

    Hi James, nice to meet you.

    Great article at https://www.jamescroft.co.uk/setting-github-actions-environment-variables-in-powershell/. I’m trying to use the output variable from PowerShell to set an Environment variable to be used in another step. Would you mind sharing the file for article “GetBuildVersion.psm1”?
    Is “GetBuildVersion” the output from PowerShell?

    Thank you so much!

    Randy Pagels
    rpagels@microsoft.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.