Deploying to a Staging Environment
Set up a workflow to automatically deploy your application to a staging environment for pre-production testing.
Deploying to a Staging Environment is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a Staging Environment?
Imagine you've built a new feature for your app. Before showing it to everyone (production), you want to test it in a safe, realistic setting. That's where a staging environment comes in!
A staging environment is a copy of your production environment. It's used for final testing before a release, allowing you to catch issues and ensure quality without affecting live users.
Staging vs. Production
While staging mirrors production, their purposes differ:
- Staging: For quality assurance (QA), user acceptance testing (UAT), and final checks. Only a limited audience (testers, stakeholders) interacts with it.
- Production: The live environment used by all your end-users. Any issues here directly impact your customers.
Deploying to staging first is a critical step to minimize risks in production.
GitHub Environments Feature
GitHub Actions provides a powerful feature called Environments. These allow you to define logical environments (like staging or production) within your repository.
Environments help you:
- Apply protection rules (manual approvals, wait timers).
- Manage environment-specific secrets.
- Track deployments to specific environments.
Declaring a Staging Environment
To use an environment in your workflow, you first define it in your repository's settings:
- Go to your GitHub repository.
- Click Settings > Environments.
- Click New environment and name it, for example,
staging.
You can add protection rules here, like requiring a reviewer to approve deployments.
Basic Deployment Workflow Structure
Now, let's look at how to tell GitHub Actions to deploy to our staging environment. You specify the environment within a job:
name: Deploy to Staging
on: push
jobs:
deploy-to-staging:
runs-on: ubuntu-latest
environment: staging # This links to your 'staging' environment
steps:
- name: Say Hello
run: echo "Deploying to staging!"Configuring Environment Protection
The environment: staging line in your workflow does more than just label the deployment. If you've set up protection rules for the staging environment in your repo settings, they will automatically apply.
For example, if you require a manual approval, this job will pause until an authorized person reviews and approves the deployment.
Building Your Application
Before deploying, you usually need to build your application. This might involve compiling code, running tests, or packaging assets. We'll add these steps to our job:
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js (Example build step)
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Build application
run: npm run buildDeploying the Built Artifacts
After building, the next step is to transfer your application files to the staging server. This often involves SSH, FTP, or a cloud-specific deployment tool.
Here's a conceptual step. In a real scenario, you'd replace echo with your actual deployment command:
- name: Deploy to Staging Server
run: |
echo "Simulating deployment to staging..."
# scp -r ./dist user@staging.example.com:/var/www/html
# Or use a specific deployment action for AWS, Azure, etc.Putting It All Together: Staging Workflow
This complete workflow will trigger on pushes to the develop branch. It checks out code, builds the app, and then conceptually deploys it to the staging environment, respecting any protection rules you've set.
name: Deploy to Staging Environment
on:
push:
branches:
- develop # Trigger only on pushes to the 'develop' branch
jobs:
deploy:
runs-on: ubuntu-latest
environment: staging # Link this job to the 'staging' environment
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js (Example)
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Install dependencies
run: npm ci
- name: Build application
run: npm run build
- name: Deploy to Staging Server
run: echo "Deployment to staging completed!"Staging Deployment Check
You've learned about setting up a GitHub Actions workflow to deploy to a staging environment. Let's test your knowledge!
Recap: Staging Deployments
Great job! You've learned how to set up GitHub Actions for deploying to a staging environment.
- Staging is a production-like environment for pre-production testing.
- GitHub Environments help manage deployment safety and secrets.
- Workflows use the
environmentkey to target specific environments. - This process minimizes risks, ensuring a smoother transition to production.
Next, we'll explore how to manage sensitive information with environment variables and secrets!
Frequently asked questions
Is the “Deploying to a Staging Environment” lesson free?
Yes — the full text of “Deploying to a Staging Environment” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Deploying to a Staging Environment”?
Set up a workflow to automatically deploy your application to a staging environment for pre-production testing. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Deploying to a Staging Environment” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Continuous Deployment
- Deploying to a Staging Environment
- Environment Variables and Secrets
- Deploying to Production with Approval Gates