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.
In this lab, you will create a GitHub Actions workflow that automatically cleans up the C:\temp
folder on a self-hosted Windows runner. The workflow will:
C:\temp
folder if it exists.C:\temp
.C:\temp
doesn’t exist.Estimated Duration: 10-15 minutes
By the end of this lab, you will:
In your repository, you will create the following directory structure if it does not already exist:
.github/
└── workflows/
└── misc-temp-folder-cleanup.yml
This folder will contain the workflow file where you will define the steps for cleaning up the C:\temp
directory on the self-hosted runner.
Create the workflow file .github/workflows/misc-temp-folder-cleanup.yml
with the following content:
name: Misc - Temp Folder Cleanup
on:
push:
branches:
- main
paths:
- '.github/workflows/misc-temp-folder-cleanup.yml'
workflow_dispatch: # Allows manual triggering of the workflow
jobs:
cleanup-temp:
runs-on: self-hosted # Uses the self-hosted runner
steps:
- name: Checkout Repository
uses: actions/checkout@v3
# Step 1: Check if the runner is on Windows OS using PowerShell
- name: Check OS
run: |
echo "Running on OS: $env:RUNNER_OS"
shell: pwsh
# Step 2: List all folders in C:\temp (only on Windows, and if C:\temp exists)
- name: List all folders in C:\temp
if: $ && (exists 'C:\temp')
run: |
echo "Listing all folders in C:\temp..."
# PowerShell command to list directories in C:\temp
Get-ChildItem -Path C:\temp -Directory | Select-Object -ExpandProperty Name
shell: pwsh
# Step 3: Delete all folders in C:\temp
- name: Delete all folders in C:\temp
if: $ && (exists 'C:\temp')
run: |
echo "Deleting all folders in C:\temp..."
# PowerShell command to remove all directories in C:\temp
Get-ChildItem -Path C:\temp -Directory | Remove-Item -Recurse -Force
shell: pwsh
# Step 4: If C:\temp doesn't exist, log a message and skip
- name: Skip Cleanup if C:\temp doesn't exist
if: $ && !(exists 'C:\temp')
run: |
echo "C:\temp does not exist. Skipping cleanup step."
shell: pwsh
This workflow performs the following tasks:
Checkout Repository:
actions/checkout@v3
action is used to check out the repository so that subsequent steps can execute correctly.Check OS:
Check OS
step ensures the runner is running on Windows by using PowerShell ($env:RUNNER_OS
).List Folders in C:\temp
:
C:\temp
exists and, if it does, lists all the directories within it using the Get-ChildItem
cmdlet.Delete Folders in C:\temp
:
C:\temp
directory exists, the workflow recursively deletes all directories inside it.Skip Cleanup if C:\temp
Doesn’t Exist:
C:\temp
is not found, the workflow logs a message and skips the cleanup process.Set Up the Workflow:
.github/workflows/misc-temp-folder-cleanup.yml
in your repository.Run the Workflow:
workflow_dispatch
event or by pushing changes to the workflow file in the main
branch.Review the Logs:
C:\temp
exists and contains folders, the workflow will:
C:\temp
does not exist:
Example log output for the List all folders in C:\temp
step:
Listing all folders in C:\temp...
folder1
folder2
folder3
Example log output for the Delete all folders in C:\temp
step:
Deleting all folders in C:\temp...
Successfully deleted folder1
Successfully deleted folder2
Successfully deleted folder3
Self-Hosted Runners:
C:\temp
directory.Skipping Steps:
if: $ && !(exists 'C:\temp')
to skip the cleanup step if C:\temp
doesn’t exist, preventing failure and ensuring smooth execution.Debugging:
In this lab, you have created a GitHub Actions workflow that automates the cleanup of the C:\temp
folder on a self-hosted Windows runner. By using conditional checks and PowerShell commands, the workflow ensures efficient management of the file system, including listing and deleting directories while skipping steps when necessary. This demonstrates the power of GitHub Actions in automating system maintenance tasks.