Python Advanced - CI/CD with GitHub Actions
Setting up continuous integration and continuous deployment with GitHub Actions
GitHub Actions is a powerful automation tool that allows you to automate your software development workflows directly in GitHub. It enables continuous integration (CI) and continuous deployment (CD) by automating the build, test, and deployment processes. This tutorial explores how to set up CI/CD pipelines using GitHub Actions for Python applications.
Key Points:
- GitHub Actions is a powerful tool for automating workflows in GitHub.
- It supports CI/CD by automating the build, test, and deployment processes.
- GitHub Actions integrates seamlessly with GitHub repositories.
Creating a GitHub Actions Workflow
To create a GitHub Actions workflow, you need to define a YAML file in the .github/workflows
directory of your repository. Here is an example of a simple workflow file:
# .github/workflows/ci.yml
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: |
pytest
In this example, the workflow is triggered on push or pull request events to the main branch. It checks out the code, sets up Python, installs dependencies, and runs tests.
Adding a Deployment Step
To add a deployment step to the workflow, you can use various actions depending on your deployment target (e.g., AWS, Azure, Heroku). Here is an example of deploying to Heroku:
# .github/workflows/ci_cd.yml
name: CI/CD
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: |
pytest
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Deploy to Heroku
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
run: |
git remote add heroku https://git.heroku.com/.git
git push heroku main
In this example, a deployment job is added to the workflow that runs after the build job. It uses the Heroku API key stored in GitHub Secrets to deploy the application.
Setting Up GitHub Secrets
To securely store sensitive information like API keys, you can use GitHub Secrets. Here is how to set up a secret:
- Navigate to your repository on GitHub.
- Click on "Settings" and then "Secrets".
- Click on "New repository secret".
- Add a name (e.g.,
HEROKU_API_KEY
) and the value of your secret.
Now, you can reference this secret in your workflow file using ${{ secrets.SECRET_NAME }}
.
Testing the Workflow
To test the workflow, push your changes to the repository or create a pull request. You can monitor the workflow runs in the "Actions" tab of your repository on GitHub.
Summary
In this tutorial, you learned about setting up continuous integration and continuous deployment with GitHub Actions for Python applications. GitHub Actions allows you to automate your build, test, and deployment processes seamlessly. Understanding GitHub Actions is essential for maintaining a robust CI/CD pipeline and ensuring the reliability of your software.