Master GitHub Actions with hands-on labs and exercises. Learn how to automate workflows, run tests, deploy applications, and more using GitHub's powerful automation platform. This repository has everything you need to get started with continuous integration and continuous deployment.
This lab will guide you through the process of creating a GitHub Actions workflow to handle pull request events, dynamically create environments, and simulate deployment tasks. By the end of this lab, you will have a functioning workflow that responds to pull requests on the main
branch.
event-pull-request.yml
and place it under .github/workflows/
.Copy the following YAML configuration into the workflow file:
name: Pull Request Event
on:
pull_request:
branches:
- main
jobs:
create-environment:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the repository
- name: Checkout Code
uses: actions/checkout@v3
# Step 2: Set up dynamic environment name
- name: Set Environment Name
id: set-environment
run: |
# Generate a dynamic environment name based on PR number
PR_NUMBER=$
echo "environment-name=pr-${PR_NUMBER}" >> $GITHUB_ENV
# Step 3: Deploy to the dynamically created environment
- name: Deploy Application
env:
DEPLOY_ENV: $
run: |
echo "Deploying application to environment: $DEPLOY_ENV"
# Add your deployment commands here
Trigger:
The workflow triggers on pull_request
events targeting the main
branch.
Steps:
pr-42
).GITHUB_ENV
for use in later steps.Commit the file to your repository:
git add .github/workflows/pull-request-workflow.yml
git commit -m "Add pull request workflow"
git push origin main
main
branch in your repository.pr-2
).Add a Cleanup Job:
Include a job to delete the dynamic environment when the pull request is closed.
on:
pull_request:
types:
- closed
Integrate Real Deployment Steps:
Replace the echo
command in the deployment step with real deployment logic (e.g., deploy to a cloud environment).
main
branch.Actions
are enabled for the repository.This lab demonstrates how to create a simple and dynamic GitHub Actions workflow to handle pull request events. You can extend this workflow to include additional steps, such as testing or actual deployment processes, based on your project needs.