GitHub Actions Workflow Syntax: name with Examples
Introduction
GitHub Actions is a powerful feature provided by GitHub that allows developers to automate, customize, and execute their software development workflows right in their repositories. One of the key components of GitHub Actions is the Workflow Syntax. In this blog post, we will focus on the name
attribute in the GitHub Actions Workflow Syntax, its usage, and examples.
Understanding the name
Attribute
The name
attribute in a GitHub Actions workflow is an optional field that you can use to label your workflow. This label is displayed on GitHub when the workflow is run, making it easier for you to identify different workflows in your repository.
The name
attribute is defined at the root of the workflow file and is not associated with any specific job or step within the workflow. It’s a simple string that can be anything you want, but it’s best to choose a name that accurately represents the tasks the workflow performs.
Usage of the name
Attribute
The name
attribute is used at the beginning of your workflow file, right after the on
keyword that specifies the event that triggers the workflow. Here’s a basic example of how to use the name
attribute:
1 2 3 4 5 6 7 8 9 10 11 |
name: My First Workflow on: [push, pull_request] jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Run a one-line script run: echo Hello, world! |
In this example, the workflow is named “My First Workflow”. This name will be displayed on GitHub whenever this workflow is run. The workflow is triggered whenever there’s a push
or pull_request
event on the repository.
Detailed Example with Explanation
Let’s take a look at a more detailed example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
name: CI/CD Pipeline on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Setup Node.js environment uses: actions/setup-node@v2 with: node-version: 14 - name: Install dependencies run: npm ci - name: Run tests run: npm test |
In this example, the workflow is named “CI/CD Pipeline”. This workflow is triggered whenever there’s a push
or pull_request
event on the main
branch of the repository.
The workflow has one job named build
that runs on the latest version of Ubuntu. This job has four steps:
- Checkout code: This step uses the
actions/checkout@v2
action to checkout the repository’s code onto the runner. - Setup Node.js environment: This step uses the
actions/setup-node@v2
action to setup a Node.js environment with Node.js version 14. - Install dependencies: This step runs the
npm ci
command to install the project’s dependencies. - Run tests: This step runs the
npm test
command to execute the project’s tests.
Conclusion
The name
attribute in GitHub Actions Workflow Syntax is a simple yet powerful tool that helps you label and identify your workflows. While it’s an optional attribute, using it effectively can greatly improve your workflow management experience.