Welcome back to the #90DaysOfDevOps series! Today, we're going to discuss about Continuous Integration (CI) using GitHub Actions. CI is a crucial aspect of modern software development, ensuring that your code integrates smoothly and remains functional as you make changes.
Setting the Stage
In today's lab, we'll be working on a simple Python application. Before we start working on the GitHub Actions setup, let's create the necessary files for our project.
Step 1: Setting Up Your Project
1. Create a Python Project:
Start by setting up a new directory for your project. Inside it, create a Python file, say app.py, with a simple Python script.
# app.py
def greet(name):
return f"Hello, {name}!"
2. Create a Requirements File:
Next, let's add a requirements file, requirements.txt, to manage our project dependencies.
# requirements.txt
Flask==2.0.1
3. Dockerfile for Dockerization:
Now, let's Dockerize our application by creating a Dockerfile in the project root.
# Dockerfile
FROM python:3.9
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Step 2: GitHub Repository Setup
Create a GitHub Repository:
If you haven't already, create a new repository on GitHub. Initialize it with a README and add a .gitignore file for Python.
Push Your Code:
Push your local project to the GitHub repository.
Step 3: GitHub Actions Configuration
Now comes the exciting part – setting up GitHub Actions for continuous integration.
Create a Workflow File:
Inside your project, create a new directory named .github/workflows. In this directory, add a new file, say ci.yml, to define your CI workflow.
# .github/workflows/ci.yml
name: CI
on:
push:
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.9
- name: Install Dependencies
run: pip install -r requirements.txt
- name: Run Tests
run: |
python -m unittest discover -s tests -p '*_test.py'
Understanding the Workflow:
The workflow triggers on each push to the main branch.
It runs on the latest version of Ubuntu.
It checks out the repository, sets up Python 3.9, installs dependencies, and runs tests.
Step 4: Watch GitHub Actions in Action
Push Changes:
Make a change to your code, commit, and push it to the main branch.
Check GitHub Actions Tab:
Head to the "Actions" tab in your GitHub repository. You'll see your CI workflow in action, with details on each step's status.
Review Results:
If all steps pass, congratulations! You now have a working CI setup.
In just a few steps, you've successfully implemented Continuous Integration using GitHub Actions for your Python project. This ensures that your code remains robust and functional with every change. Stay tuned for more DevOps adventures in the #90DaysOfDevOps series!
Thank you for reading!
*** Explore | Share | Grow ***
Commentaires