View on GitHub

presentations

Presentation notes from JMU Unix Users Group meetings

What is CI/CD?

Continuous Integration (CI): Automate the process of merging new code into a shared repository, early and often.

Continuous Deployment/Delivery (CD): One step further than CI, code that passes all tests is automatically deployed to production, without manual intervention.

Most developer platforms have their own CI/CD solutions (GitHub Actions), but many people also self-host these solutions themselves (a topic for another time).


The Theory

The idea is to automate the testing and delivery of software from the initial code commit all the way through to deployment.

Why do this?

Is it worth it? It depends!


The Agenda

You will be setting up a sample GitHub repository, and setting up a GitHub Actions configuration file to ensure that code will be checked and, if it passes the tests, deploy a new build automatically.

Terminology to understand:

Here is a simple example to demonstrate these concepts in action!

# Make your names descriptive!
name: Lint

# Only run whenever new code is pushed to the repo
on: push

# Linting job
jobs:
  lint:
    name: Lint code base
    runs-on: ubuntu-latest # Define the OS for your runner.
    steps: # What the job will do, in order, once the event triggers
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Run linter
        uses: github/super-linter@v3
        env:
          DEFAULT_BRANCH: main # This job will ONLY run on the main branch
          # Never use plaintext to store sensitive information, use secrets!
          GITHUB_TOKEN: $

Getting Started

Note: In order to follow along, you must install Git and have an active GitHub account.

Fork my sample repository

For demonstration purposes, I have created a sample repository that contains a simple Python “Hello World” file.

  1. Navigate to the sample repository.
  2. In the top-right corner of the page, click Fork. Leave everything as default.
  3. Click Create fork.

Download the repository to your local device

  1. On GitHub, navigate to your fork of the sample repository.
  2. Above the list of files, click the green Code button
  3. Copy the URL for the repository.
    1. If you have an SSH key associated with your GitHub account already, use the SSH option.
    2. Otherwise, use the HTTPS option.
  4. Open your terminal, and type the following:
cd ~/Documents # Or use whatever directory you want!
git clone <PASTE YOUR LINK HERE>

After you’ve completed these steps, you should have the repository in the location you specified.


Create your first workflow

cd gh-actions-example
mkdir .github/workflows && cd .github/workflows
touch static.yml

These commands do the following:


Open your workflow in a text editor

Let’s say, for instance, we want our groundbreaking, revolutionary “Hello World” app to conform to standard PEP 8 formatting. How would we accomplish this?

name: Hello World application

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - name: Set up Python 3.10
        uses: actions/setup-python@v3
        with:
          python-version: "3.10"
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install flake8 pytest
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
      - name: Lint with flake8
        run: |
          # stop the build if there are Python syntax errors or undefined names
          flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
          # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
          flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
      - name: Test with pytest
        run: |
          pytest

Time out! What’s going on here?

This seems like a lot. What is this specifically doing?

Notice that these are things that you would ordinarily do manually (I hope) if you’re deploying or pushing code to a repository.

By taking the extra five or so minutes to define a workflow first, you’ve saved yourself (or in an enterprise setting, your team) an insane amount of time in the future, especially as the project gets larger.

This is where CI/CD shines!


Play around with the workflow


More Examples of CI/CD Pipelines

Simple

Complex


What’s next?

This post is only really scratching the surface on what’s possible with CI/CD, so here are some recommendations on where to look to improve your knowledge.

h:400px