GitHub Actions Tutorial Using Python Flask Demo App

Muoki
5 min readMay 22, 2021

--

What is GitHub Actions.

GitHub Actions is a tool that allows you to automate, customize, and execute your software development workflows right in your GitHub repository.

Concepts of GitHub Actions.

1. Workflows — The workflow is an automated procedure that you add to your repository. Workflows are made up of one or more jobs and can be scheduled or triggered by an event. The workflow can be used to build, test, package, release, or deploy a project on GitHub.

2. Events — An event is a specific activity that triggers a workflow. For example, activity can originate from GitHub when someone pushes a commit to a repository or when an issue or pull request is created. You can also use the repository dispatch webhook to trigger a workflow when an external event occurs. For a complete list of events that can be used to trigger workflows, see Events that trigger workflows.

3. Jobs — A job is a set of steps that execute on the same runner. By default, a workflow with multiple jobs will run those jobs in parallel. You can also configure a workflow to run jobs sequentially. For example, a workflow can have two sequential jobs that build and test code, where the test job is dependent on the status of the build job. If the build job fails, the test job will not run.

4. Steps — A step is an individual task that can run commands in a job. A step can be either an action or a shell command. Each step in a job executes on the same runner, allowing the actions in that job to share data with each other.

5. Actions — Actions are standalone commands that are combined into steps to create a job. Actions are the smallest portable building block of a workflow. You can create your own actions, or use actions created by the GitHub community. To use an action in a workflow, you must include it as a step.

6. Runner — A runner is a server that has the GitHub Actions runner application installed. You can use a runner hosted by GitHub, or you can host your own. A runner listens for available jobs, runs one job at a time, and reports the progress, logs, and results back to GitHub. GitHub-hosted runners are based on Ubuntu Linux, Microsoft Windows, and macOS, and each job in a workflow runs in a fresh virtual environment.

GitHub Actions Example.

As an example, we will be using a simple Flaskrestful application that computes BMI given a person’s height and weight. Source code for the application can be found at

https://github.com/muokicaleb/GitHubAction_flaskrestful

Step 1: Create the action metadata yaml file.

Docker and JavaScript actions require a metadata file. The metadata filename must be either action.yml or action.yaml. The data in the metadata file defines the inputs, outputs, and main entrypoint for your action. The action.yaml file will be stored in the actions directory.

actions/action.yaml

name: "Flask GitHub actions"
description: "Learning GitHub actions using flask demo"
author: "muokicaleb@tuta.io"
runs:
using: "docker"
image: "../Dockerfile"
branding:
icon: "settings"
color: "purple"
  • name: (Required) The name of your action. GitHub displays the name in the Actions tab to help visually identify actions in each job.
  • description: (Required) A short description of the action.
  • author: (Optional) The name of the action’s author.
  • runs: (Required) Configures the image used for the Docker action.
  • branding: (Optional) You can use a color and Feather icon to create a badge to personalize and distinguish your action.

Step 2: Create Workflow file.

Workflow files use YAML syntax and must have either a .yml or .yaml file extension. You must store workflow files in the .github/workflows directory of your repository.

The .github/workflows/main.yaml file is defined as:

name: A workflow for Github Actions Tutorial
on: push
jobs:
build:
name: main workflow
runs-on: ubuntu-latest
steps:
- name: community-developed Action
uses: actions/checkout@v1
- name: Setup docker
uses: ./actions
  • name : The name of your workflow. GitHub displays the names of your workflows on your repository’s actions page. If you omit name, GitHub sets it to the workflow file path relative to the root of the repository.
  • on : (Required.) The name of the GitHub event that triggers the workflow. You can provide a single event string, array of events, array of event types, or an event configuration map that schedules a workflow or restricts the execution of a workflow to specific files, tags, or branch changes.
  • jobs: A workflow run is made up of one or more jobs. Jobs run in parallel by default.
  • jobs.build.name: The name of the job displayed on GitHub.
  • jobs.build.runs-on: The type of machine to run the job on. The machine can be either a GitHub-hosted runner or a self-hosted runner.
  • jobs.build.steps: A job contains a sequence of tasks called steps. Steps can run commands, run setup tasks, or run an action in a repository.
  • jobs.build.steps.name: A name for your step to display on GitHub.
  • jobs.build.steps.uses: Selects an action to run as part of a step in your job. An action is a reusable unit of code.

Step 3: Push to GitHub.

Push everything to git.

Whenever a commit is made to the repository the workflow will be triggered.

Under the “Actions” tab in the GitHub repository, you can view the logs as the different steps take place.

Step 4: Merge the branch with the main branch.

As the final step, you can merge the branch to the main branch through a PR.

References:

--

--

Muoki
Muoki

Written by Muoki

DevOps Engineer | OSINT Researcher | Open Source Contributor