GitHub Actions Workflow Syntax: ‘Steps’ – Structuring Your Workflow Jobs
GitHub Actions offer a robust and flexible platform for automating software development workflows. In this post, we focus on the ‘steps’ keyword in the GitHub Actions Workflow syntax, which plays a key role in structuring jobs. We will delve into its syntax, demonstrate its usage, and provide practical examples.
Introduction
GitHub Actions Workflow empowers developers to automate their software development operations, including tasks such as building, testing, and deploying applications. Workflows are automated procedures that you configure in your GitHub repository. These workflows are made up of ‘jobs’, which are collections of ‘steps’ or commands that execute tasks.
Understanding the ‘Steps’ Syntax
In GitHub Actions Workflow syntax, the ‘steps’ keyword is used to represent a sequence of tasks that will be executed within a job. Each step in a job executes on the same runner, allowing the steps to share data with each other.
Here is a simple structure of how steps are defined within a job:
1 2 3 4 5 6 7 8 |
jobs: job_id: runs-on: runner_type steps: - name: Step 1 run: command - name: Step 2 run: command |
Each step can have an optional ‘name’ that describes what the step does, and a ‘run’ keyword that executes a command. Steps can also use an ‘id’ for reference in other parts of the workflow, and a ‘uses’ keyword to include an action.
Example of ‘Steps’ in a Workflow
Let’s look at an example of a workflow with multiple steps in a job. In this example, the workflow checks out the code and then builds a Node.js application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
name: Build Node.js Application on: [push] jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Set up Node.js uses: actions/setup-node@v2 with: node-version: 14 - name: Install dependencies run: npm ci - name: Build run: npm run build |
In this workflow, the ‘build’ job has four steps. The first step checks out the code using the actions/checkout
action. The second step sets up Node.js using the actions/setup-node
action, with the version set to 14. The third and fourth steps run commands to install dependencies and build the application respectively.
Conclusion
The ‘steps’ keyword in GitHub Actions Workflow syntax is a crucial component that structures the operations within a job. By effectively defining and sequencing steps, developers can design efficient and clear workflows that handle complex tasks.