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 learn how to integrate the GitHub CLI (gh
) into GitHub Actions workflows. The GitHub CLI allows you to programmatically interact with GitHub, enabling operations like managing repositories, workflows, and more directly from your workflows.
Estimated Duration: 10-15 minutes
Ensure the following directory structure exists in your repository:
.github/
└── workflows/
└── misc-github-cli-integration.yml
Create the workflow file .github/workflows/misc-github-cli-integration.yml
with the following content:
name: Misc - GitHub CLI Integration
on:
workflow_dispatch:
push:
paths:
- '.github/workflows/misc-github-cli-integration.yml'
jobs:
run-on-ubuntu-latest:
runs-on: ubuntu-latest
env:
GH_TOKEN: $
steps:
- uses: actions/checkout@v4
- run: env
- run: gh --version
- run: gh auth status
- run: gh repo list
- run: gh workflow list
run-on-windows-latest:
runs-on: windows-latest
env:
GH_TOKEN: $
steps:
- uses: actions/checkout@v4
- run: env
- run: gh --version
- run: gh auth status
- run: gh repo list
- run: gh workflow list
run-on-macos-latest:
runs-on: macos-latest
env:
GH_TOKEN: $
steps:
- uses: actions/checkout@v4
- run: env
- run: gh --version
- run: gh auth status
- run: gh repo list
- run: gh workflow list
run-on-self-hosted:
runs-on: self-hosted
env:
GH_TOKEN: $
steps:
- uses: actions/checkout@v4
# - run: env # Code commented as env command is not available in Windows
- run: gh --version
- run: gh auth status
- run: gh repo list
- run: gh workflow list
This workflow demonstrates the use of the GitHub CLI across four environments:
ubuntu-latest
: A Linux-based runner.windows-latest
: A Windows-based runner.macos-latest
: A macOS-based runner.self-hosted
: A custom runner hosted in your environment.Install and Authenticate GitHub CLI:
GITHUB_TOKEN
secret.CLI Commands:
gh --version
: Displays the CLI version to verify installation.gh auth status
: Confirms authentication status.gh repo list
: Lists repositories accessible via the authenticated token.gh workflow list
: Lists workflows in the current repository.Set Up the Workflow:
.github/workflows/misc-github-cli-integration.yml
file in your repository.Run the Workflow:
workflow_dispatch
event or by pushing changes to the workflow file.Review the Logs:
The GitHub CLI commands should produce the following outputs:
gh --version
: Confirms the installed version of the CLI.gh auth status
: Verifies authentication using GITHUB_TOKEN
.gh repo list
: Lists all accessible repositories.gh workflow list
: Lists the workflows available in the repository.Example log output:
gh --version
gh version 2.0.0 (2024-12-03)
gh auth status
Logged in to github.com as username (GITHUB_TOKEN)
gh repo list
my-org/my-repo
gh workflow list
NAME ID STATE ...
build-and-test 12345 active
Self-Hosted Runners:
env
might not work on certain environments (e.g., Windows).Debugging:
env
command to inspect environment variables where available.By completing this lab, you’ll have practical experience using the GitHub CLI within workflows to enhance automation capabilities.