Use Variables in a .NET App and GitHub Actions

This post will show how to use variables inside a .NET application workflow in GitHub Actions.

In the following case, I am building a .NET application with a GitHub Actions workflow and using a variable to define the application working directory and use across the entire workflow.


Inside a GitHub Actions workflow we can define variables in every level of the workflow (workflow, step and job).

In the example below, I’m defining a variable called APP_PATH on the workflow level. To define it on a job or a step level, move the code to the step or job level.


Below is the entire workflow; to see it without the variables, check this post.

name: .NET Core
    branches: [ main ]
    branches: [ main ]
 APP_PATH: "./myApp" 
    runs-on: ubuntu-latest
    - name: Checkout GitHub actions
      uses: actions/[email protected]
    - name: Setup .NET Core
      uses: actions/[email protected]
        dotnet-version: 5.0.x
    - name: Install dependencies
      run: dotnet restore ${{env.APP_PATH}}
    - name: Build code
      run: dotnet build ${{env.APP_PATH}} --configuration Release --no-restore
    - name: Test code 
      run: dotnet test ${{env.APP_PATH}} --no-restore --verbosity normal
    - name: Publish application
      run: dotnet publish ${{env.APP_PATH}} -c Release -o myApp
    - name: Upload Artifacts
      uses: actions/[email protected]
       name: my-artifact
       path: ${{env.APP_PATH}}

When defining variables, please remember that variables name are case sensitive, and you can’t use special characters.

Leave a comment

Your email address will not be published. Required fields are marked *

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