If you’re looking to dip your toes into the world of CI/CD (that’s Continuous Integration/Continuous Deployment for non-jargon fans), you’ve come to the right place. Today, we’ll create your first CI/CD pipeline using GitHub Actions—like a boss—to automate tests on a very simple Python script. Don’t worry; we’re taking this slow, with a light-hearted approach and simple explanations. Buckle up, it’s time to automate all the boring parts!
What Do You Need Before We Start?
- A GitHub account. (If you don’t have one yet, seriously, go make one—it’s free and gets you access to lots of cool stuff like, well, this tutorial!)
- Your trusty WSL (Windows Subsystem for Linux) Ubuntu environment.
- A Python script. Any Python script. Even if it’s just
print('Hello, world!')
. We’re keeping it simple today.
Step 1: Create Your Repository
Let’s kick things off by creating a GitHub repository.
- Log into GitHub and click the “New” button in the Repositories section.
- Name your repository something awesome like
ci-cd-python-pipeline
. You can make it public or private; up to you. Don’t forget to check the box that says “Add a README file”—it makes life easier. - Click “Create repository.” Bam! You now have a home for your future CI/CD masterpiece.
Step 2: Add Your Python Script
Time to bring some Python power.
- In your new repository, click the ‘Add file’ button, then select ‘Create new file’.
- Name your file something like
hello.py
(classic). - Paste in your Python code, e.g. :
def my_function():
return "Hey, this is my first CI/CD!"
if __name__ == "__main__":
print(my_function())
- Commit your new file to the
main
branch by clicking “Commit Changes…”. A popup window will require you to enter a commit message—just add a brief description like ‘Add hello.py’. Congratulations! You’re now an official Python script owner.
Step 3: Let’s Get Action-Packed with GitHub Actions
Now, here comes the fun part—setting up GitHub Actions for some automation magic!
- Click on the Actions tab in your repository. GitHub will suggest some templates, but we’re making our own!
- Click Set up a workflow yourself.
- Name your workflow file
.github/workflows/python-ci.yml
. The path matters, so make sure it’s exactly like that.
Step 4: Write the Workflow
Time to tell GitHub what to do. Don’t worry, it listens well.
Here’s the code you’ll paste in to create a workflow that runs whenever you push code to your repository:
name: Python CI Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Run Python script
run: |
python hello.py
What’s Happening Here?
- name: We call our workflow
Python CI Pipeline
. Feel free to get creative here, likeDeploy-o-Tron 3000
. - on: This workflow will trigger whenever you push to the
main
branch or create a pull request to it. - jobs: We define the job called
build
. - runs-on: This job will run on
ubuntu-latest
, which is basically a virtual machine that GitHub is nice enough to lend us. - steps: These are the individual commands we’ll run.
- First, we checkout the code (so GitHub knows what files to work with).
- We set up Python.
- Next, we install dependencies (if any are needed—for now, there aren’t any).
- Lastly, we run the script to ensure it works, and then run tests using
pytest
to verify everything behaves as expected.
Step 5: Push Your Changes and Watch the Magic Happen
- Save the workflow file by clicking Commit changes…. After clicking this button, a popup window will require you to enter a commit message—just add a brief description like ‘Add CI workflow’.
- If everything goes smoothly, you’ll see a big green checkmark in the Actions tab indicating that your workflow ran successfully. If there’s an issue, you might see a red cross instead, meaning something needs fixing.
Step 6: Make a Change and Test It
CI/CD isn’t just about running your script once; it’s about automating tests every time your code changes.
- Edit your
hello.py
script—maybe change the message toHello, GitHub Actions!
. - Commit the change.
- Your workflow will automatically run again. Easy, right?
Bonus Step: Add Some Tests
If you want to go one step further, add a test! Create a new file named test_hello.py
and include the following code:
import pytest
from hello import my_function
def test_my_function():
assert my_function() == "Hello, GitHub Actions!"
Update your .github/workflows/python-ci.yml
:
- First, add
pip install pytest
under the ‘Install dependencies’ step, so that it looks like this:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- Second, add the test after “Run Python script”:
- name: Run Python script
run: |
python hello.py
- name: Run tests
run: |
pytest
Conclusion
And there you have it! You just set up a basic CI/CD pipeline that runs your Python script every time you make a change. This example only covers the CI part, but we will explore the CD part in a future post. It might be simple now, but the skills you learn here will be the foundation of more complex automation in the future.