Hello CI World

Overview

Teaching: 5 min
Exercises: 10 min
Questions
  • How do I run a simple GitHub Actions job?

Objectives
  • Add CI/CD to your project.

Adding CI/CD to a project

The first thing we’ll do is create a .github/workflows/main.yml file in the project.

cd virtual-pipelines-eventselection/
mkdir -p .github/workflows

Open .github/workflows/main.yml with your favorite editor and add the following

name: example
on: push
jobs:
  greeting:
    runs-on: ubuntu-latest
    steps:
      -run: echo hello world

Run GitHub Actions

Run on your computer

At this point we need the nektos/act tool.

You can run

act -l  # -l stands for list (list workflows)

What happened??? We can see that this failed because the YAML was invalid… (Error: yaml: line …)

We should fix this accondingly to the Error message. Note that act is not a YAML Validator but can help. Some YAML Validators: https://yamllint.readthedocs.io, https://codebeautify.org/yaml-validator, http://www.yamllint.com/, …

Let’s rerun:

act -l

This should print out the job name, i.e greeting:

ID          Stage  Name       
greeting    0      greeting

To run greeting do

act -j greeting  # -l stands for job (run job)
[example/greeting] 🚀  Start image=catthehacker/ubuntu:act-latest
[example/greeting]   🐳  docker run image=catthehacker/ubuntu:act-latest entrypoint=["/usr/bin/tail" "-f" "/dev/null"] cmd=[]
[example/greeting] ⭐  Run echo hello world
| hello world
[example/greeting]   ✅  Success - echo hello world

Run on GitHub

We’ve created the .github/workflows/main.yml file but it’s not yet on GitHub. Next step we’ll push these changes to GitHub so that it can run our job. Since we’re adding a new feature (Actions) to our project, we’ll work in a feature branch. This is just a human-friendly named branch to indicate that it’s adding a new feature.

cd virtual-pipelines-eventselection/
git checkout -b feature/add-actions
git add .github/workflows/main.yml
git commit -m "my first actions"
git push -u origin feature/add-actions

And that’s it! You’ve successfully run your CI/CD job and you can view the output. You just have to navigate to the GitHub webpage for the virtual-pipelines-eventselection project and hit Actions button, you will find details of your job (status, output,…).

GitHub Actions Page

From this page, click through until you can find the output for the successful job run which should look like the following CI/CD Hello World Success Output

Pull Request

Lastly, we’ll open up a pull request for this branch, since we plan to merge this back into master when we’re happy with the first iteration of the Actions.

Work In Progress?

If you expect to be working on a branch for a bit of time while you have a pull request open, it’s good etiquette to mark it as a Work-In-Progress (WIP). However this is not a built-in feature of GitHub, it requires to install it from GitHub Marketplace. Free for personal account.

Work In Progress

Key Points

  • Creating .github/workflows/main.yml is the first step to salvation.

  • Pipelines are made of jobs with steps.

  • ACT is especially useful to check and run GitHub Actions jobs (locally) before pushing changes.