Hermann.Gruber
Hermann.Gruber

Reputation: 1494

Github Actions: Deploy main branch to protected environment after pull request is merged

In our github repository, we have set up a protected environment named Sandbox, for which the main branch is the only allowed deployment branch. Now we want to deploy automatically to that environment if a pullrequest is merged into main (and the if the pullrequest in addition bears the label "Sandbox").

Our workflow is roughly as follows:

name: Pull Request Merged

concurrency:
  group: ${{ github.ref }}

on:
  pull_request:
    types: [closed]

jobs:
  deploy_to_sandbox:
    if: |
      github.event.pull_request.merged == true && 
      contains(github.event.pull_request.labels.*.name, 'Sandbox')
    name: Deploy to Sandbox
    uses: ./.github/workflows/deploy.yml
    with:
      environment: Sandbox
    secrets: inherit

The workflow is triggered as expected upon merging a PR, but somehow it tries to deploy from the feature branch instead of deploying from main. Since the environment is protected, the deployment fails accordingly. How can we achieve that the deployment uses the target branch (i. e. , main) that was merged into, instead of the source branch?

Upvotes: 3

Views: 1132

Answers (1)

mar0ne
mar0ne

Reputation: 218

There’s no way to specify that a workflow should be triggered when a pull request is merged. the reason why it's the feature branch that gets deployed is because it's the one that triggers the workflow. However, because a merged pull request always results in a push, you can use the push event to accomplish your goal.

For example, let’s say that you want to run a workflow whenever a pull request is merged to your main branch. You can do something like this:

on:
  push:
    branches:
      - main

also if you want to prevent push directly to main it's part of github pro plan.

Upvotes: 0

Related Questions