Is it possible to combine fields instead of writing them multiple times?

I would like to get into Github Actions. Whenever a PR was started I want to run all tests.

Currently I have a run-tests-on-pull-request.yml with this content

name: Run tests on pull request

on: [pull_request]

jobs:
  run-tests-on-pull-request:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Setup .Net
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: 5.0.x

      - name: Restore dependencies
        run: dotnet restore
        working-directory: ./sln-name

      - name: Build project
        run: dotnet build --no-restore
        working-directory: ./sln-name

      - name: Run tests
        run: dotnet test --no-build --verbosity diagnostic
        working-directory: ./sln-name

I would like to know suggestions for improvement. E.g. is it possible to pass in the field “working-directory” only once?

Thanks in advance! :slight_smile:

You can set a default working-directory for a whole workflow (defaults.run) or a job (jobs.<job_id>.defaults.run). That’s only an improvement if all (or at least most) of your steps use the same working directory, but that seems to be the case in your workflow. :slightly_smiling_face:

@airtower-luna thank you very much! If I understood correctly the optimized version would be

name: Run tests on pull request

on: pull_request

jobs:
  run-tests-on-pull-request:

    runs-on: ubuntu-latest
    
    defaults:
      run:
        working-directory: ./DotNetBackend

    steps:
    - uses: actions/checkout@v2
    
    - name: Setup .NET
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.x

    - name: Restore dependencies
      run: dotnet restore
      
    - name: Build project
      run: dotnet build --no-restore
      
    - name: Run tests
      run: dotnet test --no-build --verbosity normal

Is that correct?

Looks good to me, but to be sure you should try it out. :wink: