In GitHub Actions, a YAML file is used to define workflows, which are a series of jobs that can be executed when certain events occur, such as pushing code to a repository or creating a pull request. Here is an example YAML file for a simple workflow:
name: My Workflow
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Build project
run: |
npm install
npm run build
Here’s an explanation of each part of the YAML file:
- name: This is the name of the workflow. It can be any string, and is used to identify the workflow in the GitHub Actions dashboard.
- on: This section defines the events that will trigger the workflow. In this case, the workflow will be triggered when code is pushed to the main branch.
- jobs: This section defines the individual jobs that make up the workflow. In this case, there is only one job called build.
- build: This is the name of the job. It can be any string, and is used to identify the job in the GitHub Actions dashboard.
- runs-on: This defines the operating system and version that the job will run on. In this case, the job will run on the latest version of Ubuntu.
- steps: This section defines the individual steps that make up the job. In this case, there are two steps.
- name: This is the name of the step. It can be any string, and is used to identify the step in the GitHub Actions dashboard.
- uses: This step uses the actions/checkout action to check out the code from the repository.
- run: This step runs a shell command to install the project’s dependencies and build the project.
Note that the YAML file must be placed in a .github/workflows directory in the root of the repository. The file name must end in .yml or .yaml.