Week-6: Introduction to GitHub Actions: Basic Workflow

Mohammad Zeynali
4 min readSep 6, 2024

--

In today’s fast-paced development environment, automation has become a crucial element of the software development process. Continuous Integration (CI) and Continuous Deployment (CD) systems help streamline workflows by automating tasks such as testing, building, and deploying code. GitHub Actions is one such powerful tool that allows developers to automate workflows directly in GitHub repositories. In this blog post, we’ll break down the basics of GitHub Actions using a simple workflow example to demonstrate how it works.

🔙 Previous: Week-5: Understanding Docker and Docker Compose

🔙 Previous: Integrating Google Cloud Storage and AWS-S3 with DVC

🔜 Next:

What is GitHub Actions?

GitHub Actions is an automation tool that enables you to create custom workflows directly in your repository. These workflows can be triggered by various events such as pushing code, opening pull requests, or creating issues. GitHub Actions allows you to build, test, and deploy applications, making it an integral part of modern DevOps pipelines.

Example Workflow: GitHub Actions Basic Flow

Let’s walk through a simple GitHub Actions workflow configuration, which is triggered when a push event occurs in the repository.

name: GitHub Actions Basic Flow
on: [push]
jobs:
Basic-workflow:
runs-on: ubuntu-latest
steps:
- name: Basic Information
run: |
echo "🎬 The job was automatically triggered by a ${{ github.event_name }} event."
echo "💻 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
echo "🎋 Workflow is running on the branch ${{ github.ref }}"
- name: Checking out the repository
uses: actions/checkout@v2
- name: Information after checking out
run: |
echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."

Breakdown of the Workflow

  1. Triggering the Workflow

The first part of the configuration defines when the workflow will be triggered. In this example, the on: [push] line specifies that the workflow will run every time there is a push to the repository.

on: [push]

You can modify this trigger to other events like pull_request or schedule if needed.

2. Defining Jobs and Runners

The workflow defines a single job called Basic-workflow. This job will be executed on the ubuntu-latest virtual machine (runner), which is provided by GitHub.

jobs:
Basic-workflow:
runs-on: ubuntu-latest

GitHub provides various runners (Linux, macOS, Windows), and you can specify which environment to use for your job.

3. Defining Workflow Steps

Under steps, we define the individual tasks the workflow will perform. Let’s walk through each step:

  • Basic Information Step:

This step prints out information about the GitHub event that triggered the workflow, the OS running the workflow, and the branch name.

- name: Basic Information
run: |
echo "🎬 The job was automatically triggered by a ${{ github.event_name }} event."
echo "💻 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
echo "🎋 Workflow is running on the branch ${{ github.ref }}"

The github and runner variables provided by GitHub Actions offer useful metadata about the event and the environment.

  • Checkout Repository Step:

This step uses the actions/checkout action to clone the repository to the runner.

- name: Checking out the repository
uses: actions/checkout@v2

The checkout action is essential for making the repository’s code available on the runner, allowing further actions to interact with the code.

  • Post-Checkout Information:

After the repository is cloned, this step prints some information, confirming that the code is now available on the runner.

- name: Information after checking out
run: |
echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
echo "🖥️ The workflow is now ready to test your code on the runner."
  • Listing Repository Files:

This step lists all the files in the repository directory on the runner.

- name: List files in the repository
run: |
ls ${{ github.workspace }}

This helps in visualizing the contents of the cloned repository and ensures the workflow is interacting with the right files.

  • Job Status:

Finally, this step prints the status of the job.

- run: echo "🍏 This job's status is ${{ job.status }}."

Why Use GitHub Actions?

GitHub Actions offers several benefits:

  • Automation: Automate repetitive tasks like testing, building, or deployment.
  • Customizable: Highly customizable with a wide range of built-in actions or custom actions.
  • Easy Integration: Seamlessly integrates into your GitHub repository.
  • Scalable: Supports parallel jobs and matrix builds for testing in multiple environments.

Here are the codes:

Conclusion

This basic GitHub Actions workflow demonstrates how you can quickly set up automation in your repository to run tasks on push events. From simple steps like checking out code and printing system information to more complex CI/CD pipelines, GitHub Actions can be a powerful tool for streamlining your development process. As you become more familiar with it, you can expand your workflows to include additional jobs like testing, linting, or deploying your code automatically.

GitHub Actions can be an integral part of your DevOps strategy, enabling faster feedback and smoother deployment processes.

--

--

Mohammad Zeynali

Generative AI | Data Scientist l Machine Learning Engineer | MLOps l Edge Architect