Creyke
Creyke

Reputation: 2107

Only run GitHub Actions step if not a pull request

I have a workflow which needs to execute either on a push or a pull request with the exception of the last step which pushes a package to NuGet (I don't want this to occur on a pull request, even to master).

How can I prevent the Publish NuGet step from running if the workflow is triggered from a pull request?

name: .NET Core

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.101
    - name: Install dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --configuration Release --no-restore
    - name: Test
      run: dotnet test --no-restore --verbosity normal
    - name: Publish NuGet
      uses: brandedoutcast/[email protected]
      with:
        PROJECT_FILE_PATH: "Orleans.Sagas/Orleans.Sagas.csproj"
        NUGET_KEY: ${{secrets.NUGET_KEY}}

Upvotes: 58

Views: 42142

Answers (2)

smoonin
smoonin

Reputation: 141

For future travellers, I found this action that worked quite well. I just needed to do an if: needs.pr-check.outputs.number != 'null' in order to filter by things being a PR or not.

https://github.com/8BitJonny/gh-get-current-pr

github.event_name != 'pull_request' did not work for me because the on.pull_request trigger doesn't exist for workflows that aren't launched by a PR.

Upvotes: 13

Edward Thomson
Edward Thomson

Reputation: 78783

You can inspect the github.event_name context variable which contains the name of the event that triggered the workflow run. (eg, pull_request or push)

In this case, you can run a step for all events whose name is not pull_request with a github.event_name != 'pull_request' conditional on your step.

For example:

 - name: Publish NuGet
   uses: brandedoutcast/[email protected]
   with:
     PROJECT_FILE_PATH: "Orleans.Sagas/Orleans.Sagas.csproj"
     NUGET_KEY: ${{secrets.NUGET_KEY}}
   if: github.event_name != 'pull_request'

Upvotes: 101

Related Questions