githubgithub-actions

How can I run Github workflow job manually?


In Gitlab and Bitbucket, we can add manual step in pipeline. is there any way we can do same thing in a Github action?

#.gitlab-ci.yml
destroy:
  stage: destroy
  script:
    - [STEPS TO DESTROY]
  when: manual

I want to run the integration-test job manually when creating a pull request targeting master, main and develop branch. The job static-code-analysis job should run automatically when creating a pull request targeting master, release\*, develop or main.

#ci.yml
name: Pull Request

on:
  pull_request:
    branches:
      - master
      - release*
      - develop
      - main
  workflow_dispatch:

jobs:
  static-code-analysis:
    runs-on: macos-latest
    env:
      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
    steps:    
      - name: Checkout code
        id: Checkout-code
        uses: actions/checkout@v3

      - name: Notify Slack
        uses: act10ns/slack@v2
        with:
          channel: 's1-panel'
          status: ${{ job.status }}
          steps: ${{ toJson(steps) }}
        if: always()

  integration-test:
    if: ${{ github.event_name == 'workflow_dispatch' && (github.base_ref == 'main' || github.base_ref == 'master' || github.base_ref == 'develop') }}
    runs-on: macos-latest
    permissions: write-all
    env:
      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
    steps:
      - name: Checkout code
        id: Checkout-code
        uses: actions/checkout@v3

      - name: Notify Slack 
        uses: act10ns/slack@v2
        with:
          channel: 's1-panel'
          status: ${{ job.status }}
          steps: ${{ toJson(steps) }}
        if: always()

I have tried using an if statement in the integration-test job as below:

if: ${{ github.event_name == 'workflow_dispatch' && (github.base_ref == 'main' || github.base_ref == 'master' || github.base_ref == 'develop') }}

I created a pull request from branch testing to master. The workflow started and when I go to Github workflow and see integration-test job, it looks like this:

A screenshot of a Github workflow for the integration-test job showing "This job was skipped"


Solution

    1. Configure pull_request trigger with the base branches

       on:
         pull_request:
           types: [created]
           branches: [main,master,develop]
      
    2. Add environment to the job (no need to use if:)

       integration-test:
         runs-on: macos-latest
         permissions: write-all
         environment: manual
         env:
           SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
         steps:
      
    3. Create an environment 'manual' on the repo with deployment protection rule that requires manual approval.

    4. On PR creation, job static-code-analysis will run automatically. Job integration-test will block, waiting for manual approval. A prompt will appear on the workflow run UI to give your approval.

    5. Environments are free on public repos, private repos need a Team or Pro plan.