GitHub Actions Workflow Syntax: ‘runs-on’ – Selecting Specific Runners
GitHub Actions provide a flexible platform for developers to automate, customize, and execute software development workflows. This post will delve into a critical component of GitHub Actions workflow syntax: the ‘runs-on’ keyword that determines the type of runner on which the jobs will execute. We’ll explore its syntax, how it’s used, and provide practical examples.
Introduction
GitHub Actions Workflow facilitates building, testing, and deploying code directly from GitHub. It employs workflows, which are automated procedures that you configure in your GitHub repository. These workflows are composed of ‘jobs’ that represent a series of ‘steps’ or commands.
The execution environment where these jobs run is determined by the ‘runs-on’ keyword. This feature lets you select the type of runner, i.e., the virtual environment, that best suits your needs.
Understanding the ‘runs-on’ Syntax
The ‘runs-on’ keyword is used in GitHub Actions workflow to specify the type of runner that a job will run on. A runner is a server with a specific operating system that runs the jobs. GitHub offers runners with Linux, Windows, and macOS operating systems.
Here is the syntax for specifying the ‘runs-on’ keyword:
1 2 3 4 5 6 |
jobs: job_id: runs-on: runner_type steps: - name: Step 1 run: command |
The ‘runner_type’ can be one of the following: ubuntu-latest
, windows-latest
, or macos-latest
. These runner types represent the latest versions of Ubuntu, Windows, and macOS, respectively.
Example of ‘runs-on’ in a Workflow
Let’s look at a practical example of a workflow that uses the ‘runs-on’ keyword. In this example, the workflow builds and tests a Python application on an Ubuntu and a Windows runner.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
name: Build and Test Python Application on: [push] jobs: build_on_ubuntu: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Build run: echo "Building on Ubuntu..." build_on_windows: runs-on: windows-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Build run: echo "Building on Windows..." |
In this example, there are two jobs. The first job, ‘build_on_ubuntu’, runs on the latest version of Ubuntu. The second job, ‘build_on_windows’, runs on the latest version of Windows. By using different ‘runs-on’ keywords, we can ensure our Python application builds successfully on both Ubuntu and Windows.
Conclusion
The ‘runs-on’ keyword in GitHub Actions Workflow syntax plays a pivotal role in defining the execution environment for your jobs. By choosing the right runner, you can ensure your workflows are run in an environment that matches your application’s requirements.