Deploy to Multiple Environments with Just One GitHub Actions Workflow (Step by Step)

Normally, you have one GitHub Repo and you want to deploy it to many Environments with GitHub Actions. So you probably create many Workflows, one for each Environment. But if the deploy steps are the same, you end up with Duplicated Code in your Workflows.
The good news is that GitHub can deploy to many Environments with just one Workflow.
Let’s try it today.
I have a GitHub Repo (https://github.com/ChaiyoKung/github-actions-environment). It is a simple Web App, just for this Demo.

I also have one Azure App Service for each Environment, for the Demo.

My goal is to deploy this Web App to many Environments with just one GitHub Actions Workflow.
First, we create one GitHub Actions Workflow for the deploy.
Then Commit and Push the Code.
name: "Deploy"
run-name: "Deploy to ${{ inputs.environment }}"
on:
workflow_dispatch:
inputs:
environment:
type: environment
description: "Environment to deploy to"
required: true
default: "dev"
env:
DOTNET_VERSION: "8"
DOTNET_OUTPUT_DIR: "GithubActionsEnvironment"
jobs:
build-and-deploy:
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
steps:
- name: "Checkout"
uses: actions/checkout@v4
- name: "Setup .NET Core"
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: "Build and Publish"
run: |
dotnet restore
dotnet build -c "Release"
dotnet publish -c "Release" -o "${{ env.DOTNET_OUTPUT_DIR }}"
- name: "Deploy to Azure App Service"
uses: azure/webapps-deploy@v3
with:
app-name: ${{ vars.AZURE_APP_SERVICE_NAME }}
publish-profile: ${{ secrets.AZURE_APP_SERVICE_PUBLISH_PROFILE }}
package: "${{ env.DOTNET_OUTPUT_DIR }}"
Look at the Workflow. It has an Input with the Type “environment”.

The “Deploy to Azure App Service” step uses a Variable and a Secret. They change with the Environment, because the Job sets an Environment.

Now let’s add the Variable and the Secret for each Environment.
Go back to your GitHub Repo, click Settings, choose Environments, and click the New environment button to create an Environment.

Type a name for the Environment, then click the Configure environment button to create it.

When it is ready, Scroll down a little. You will see where to add an Environment Secret and an Environment Variable.

Let’s add the Secret first. Click the Add environment secret button, type the name and the value of the Secret, and click Add secret.

Now the Variable. Click the Add environment variable button, type the name and the value of the Variable, and click Add variable.

All done. That is one Environment finished.

Now do the same for the other 2 Environments. Create the Environment, add the Secret, and add the Variable.
Finished.

Now let’s test the deploy.
Click Actions, choose the “Deploy” Workflow, click Run workflow, choose the Environment you want to deploy to, and click Run workflow.

The deploy is done.

Let’s open the Workflow run and check if it deployed to the right place.
It did, because we chose the “dev” Environment.

Let’s open the website and see if it works.
It works.

Let’s deploy one more time. This time I will deploy the “uat” Environment.
It deployed to the right place and it works.

The website for the “prod” Environment looks like this, because we have not deployed it yet.

This is the last one. Let’s deploy the “prod” Environment.
Awesome, it works.

That is all you need. You can deploy to many Environments with just one GitHub Actions Workflow.
Just in case you wonder
Q: What happens if we do not add an Environment Variable/Secret?
A: The GitHub Actions Workflow will use the Repository Variable/Secret instead. If there is no Repository Variable/Secret either, you get an empty value.

Last thing: this is only one way to do it. In the real world, one method cannot solve every problem. So it is better to choose the method that fits the problem you really want to solve.
Thank you!