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.

Variables

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.

Workflow

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

name: .NET Core
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
env:
 APP_PATH: "./myApp" 
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout GitHub actions
      uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        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/upload-artifact@v2
      with:
       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.


Posted

in

by