GitHub CLI is a command-line tool that lets you manage GitHub's web features right in your local terminal. You can manage repositories, issues, pull requests, and automation workflows without needing to switch back and forth to a web browser.
GitHub Actions, on the other hand, can help you automate tasks such as building, testing, and deploying your applications from your GitHub repository using GitHub Actions workflows. It lets you create, edit, and delete issues, open and close pull requests, add comments, trigger other workflows, inspect workflow runs, and interact with GitHub's and third-party APIs.
Both are part of the GitHub ecosystem, and you can use them to help manage your codebase: GitHub Actions automates workflows, and GitHub CLI helps you manage your GitHub account.
Most devs use GitHub CLI locally on their laptop or desktop to manage their GitHub account. In this tutorial, I'll teach you how to use GitHub CLI in a GitHub Actions workflow with a real-life example, explaining why it's important, as well as the benefits and how to use it.
Table of Contents
- Prerequisites
- How to Use GitHub Actions in Your Repository
- How to Use GitHub CLI Locally on Your Laptop and Desktop
- How to Use GitHub CLI in your GitHub Workflows
Prerequisites
To follow along, you need a basic understanding of the GitHub platform itself. You should understand what GitHub Actions and GitHub Workflows are and how to use them. And lastly, you should know how the GitHub CLI is installed and how it works on your machine.
How to Use GitHub Actions in Your Repository
GitHub Actions are reusable components or specialized individual tasks that you can call directly from the GitHub Marketplace or write locally to handle specific operations.
As a beginner, you'll mostly rely on third-party open-source GitHub actions. These are published by the developer community on the GitHub Marketplace. You can use them in your GitHub workflow to automate certain tasks in your code repository, such as setting up Node.js, Deno, or Bun, installing pnpm, and so on.
Some actions come from official resources, others are managed by GitHub itself, and still others are built by the developer community.
Check out the following example: here, we're using the actions/setup-node@v7 (maintained by GitHub) action in our workflows to install and set up the right Node version in our workflows/action:
- name: Set up Node.js
uses: actions/setup-node@v7 // Third party action
with:
node-version: 'latest'
check-latest: false
package-manager-cache: true
In the example above, we used the actions/setup-node@v7 third-party action, officially maintained by the GitHub team. It sets up a Node.js environment in our workflows, allowing us to perform other actions such as testing, deployment, and more.
Risks of Using Third-Party Actions in Your Workflows
An over-dependence on third-party Action packages (that is, those published on GitHub Marketplace by the developer community) can be problematic. If you're just installing a third-party action to handle every small task, without thinking that much about whether you need it or not, you may be introducing risks and issues where they're not necessary.
Let's understand the third-party dependency problem with a real-life example.
Let's say I want to use an Action to create an issue in one of my repos if something happens in my application (like an issue during build or testing). So to do this, perhaps I use a third-party Action dacbd/create-issue-action@main in my GitHub workflow, like this:
- name: create an issue
uses: dacbd/create-issue-action@main // Third party action
with:
token: ${{ github.token }}
title: Simple test issue
body: my new issue
The dacbd/create-issue-action@main action creates a new issue in my repository with a GitHub Workflow.
But I don't know whether the dbg/create-issue-action@v2 package is secure or not. In the future, somebody could use this action package to steal my data and repository secrets, environment variables, and so on.
There are other problems you might face when you use third-party Actions, such as:
- Runtime compatibility and maintenance: Third-party Actions require regular maintenance to remain compatible with GitHub runner environment updates, including Node.js runtime upgrades.
- Project abandonment: Workflows may break if the action's creator stops maintaining the project.
- Security risks: Third-party actions introduce external dependencies that may expose workflows to supply-chain attacks.
- Dependency on external maintainers: Bug fixes and new features depend on the action's maintainers, potentially delaying development.
- Limited flexibility: You're restricted to the inputs and functionality provided by the action, making custom logic and advanced configurations more difficult.
While I can't say that you'll never use third-party Actions, you shouldn't have to use them too often.
To make sure you're using them responsibly, make sure that the actions you use come from official sources or providers.
And for basic tasks like creating repositories, managing issues, labels, and pull requests, running GitHub Actions workflows, checking workflow runs, and creating releases, you can use the GitHub CLI without depending on third-party actions.
When to Use Third-Party Actions in GitHub Workflows
Third-party Actions can be useful when they provide functionality that would otherwise require significant custom development and time. Consider using them in these situations:
- Complex integrations: If you're trying to integrate external services such as Slack, AWS, Docker Hub, or cloud platforms.
- Specialized functionality: When an action provides features that the GitHub CLI or built-in workflow steps don't offer directly.
- Reusable workflows: When you want to reuse well-maintained actions instead of writing and maintaining custom scripts.
- Time savings: When an existing action can simplify complex tasks and reduce development effort.
- Community-supported solutions: When an action is actively maintained, widely adopted, well-documented, and regularly updated.
My recommendation is to use GitHub's built-in features and CLI whenever possible. But choose third-party Actions when they offer meaningful benefits that justify the additional dependency and maintenance overhead, especially when they save significant time or help you handle complex tasks.
How to Use GitHub CLI Locally on Your Laptop and Desktop
GitHub CLI is a command-line tool, and it's built to eliminate context switching between the terminal (Git) and web browser (GitHub).
For example, when you're working on a project, you may often switch between your terminal (to commit code) and your web browser (to open pull requests, check issues, or manage GitHub Actions).
The GitHub CLI lets you interact with GitHub.com without switching between the terminal and web browser.
With GitHub CLI, you can do many things such as creating issues (gh issue create), creating pull requests (gh pr create), and running workflows (gh workflow run) in the terminal without opening a web browser.
Open your project, open the terminal, and create an issue with GitHub CLI on your machine without using a browser.
gh issue create --title "Automated issue" --body "This issue was created by GitHub Actions."
This command gh issue create creates a new issue in a GitHub repository using the terminal.
Similarly, with the GitHub CLI, you can create, delete, list, and manage pull requests and issues, as well as create new repositories, all without opening your browser.
For more details, refer to the official documentation: https://cli.github.com
How to Use GitHub CLI in your GitHub Workflows
The GitHub CLI simplifies working with issues, labels, pull requests, comments, and releases. For simple tasks like creating, deleting, and editing, there's no need to rely on third-party actions in GitHub Workflows. You can manage them directly from GitHub Workflows.
To illustrate this, let's create a new issue using both third-party actions and the GitHub CLI, and then compare them and understand the code syntax.
Create an Issue with GitHub CLI:
First, let's create a new issue in a workflow using the GitHub CLI.
In the following example, we'll create a new issue with a GitHub secret token, title, and body using the GitHub CLI.
name: Create a new issue
on:
push:
branches: [ "main" ]
jobs:
create-issue-job:
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Create issue
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh issue create \
--title "Automated issue" \
--body "GitHub Actions created this issue."
Now let's look at two more examples of how you can use GitHub CLI in your workflows without relying on third-party action packages.
Issues
If a deployment fails, your workflow can use gh issue create to automatically create an issue with information about the failure.
name: Create a New Issue
on:
push:
branches: [ "main" ]
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run Deployment
run: |
echo "Deploying application..."
# Simulating a build/deploy step
- name: Create issue when deployment fails
if: failure()
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh issue create \
--title "Deployment failed" \
--body "The deployment failed. Check the workflow run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
Releases
If your application build is successful, you can create a new release and upload the build file to it using the gh release create command to release a tag on GitHub.
name: Create GitHub release
on:
push:
branches: [ "main" ]
jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Build application
run: |
mkdir -p dist
echo "Build output" > dist/app.txt
- name: Create GitHub release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release create v1.0.0 dist/app.txt \
--title "Version 1.0.0" \
--generate-notes
I built a demo to show how the GitHub CLI works with GitHub workflows, and all the code is available on GitHub: https://github.com/officialrajdeepsingh/github-actions-deploy-demo.
Benefits of Using the GitHub CLI in Your Workflows
The first benefit of using the GitHub CLI is reducing dependencies on other third-party action packages. Instead of using third-party packages in your workflows, you can now replace them with the GitHub CLI.
Second, using the GitHub CLI in your GitHub Actions workflow simplifies automating GitHub tasks. Lastly, it allows you to easily comprehend the code syntax and understand what's happening.
You can use simple commands to create issues and pull requests, add comments, trigger workflows, create releases, and get information from your repository.
Most importantly, it also helps you avoid writing complex scripts or API requests in your workflows.
Conclusion
GitHub CLI makes it easier to automate GitHub tasks directly from your workflows. You can use it to manage issues, pull requests, releases, and other GitHub features without writing complex API requests.
By combining GitHub CLI with GitHub Actions, you can save time and automate repetitive tasks in your development workflow without relying on third-party action packages.