CI/CD Basics with GitHub Actions
Continuous Integration and Continuous Deployment (CI/CD) help automate the process of testing and deploying code. With GitHub Actions, it's easier than ever to set up automation right in your repository.
1. What is CI/CD?
CI (Continuous Integration) ensures that code changes are automatically tested and integrated into the main branch. CD (Continuous Deployment) means automatically deploying new versions of your app after passing the tests.
2. Why GitHub Actions?
GitHub Actions is a free and powerful CI/CD service built directly into GitHub. You can define workflows in YAML files to build, test, and deploy your code based on events like commits or pull requests.
3. Getting Started
Create a file in your repository at:
.github/workflows/main.yml
Here's a basic example for Python:
name: Run Tests
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
pip install -r requirements.txt
- name: Run tests
run: |
pytest
4. Key Terms
- Workflow – The entire automation pipeline defined in YAML
- Job – A sequence of steps run on the same machine
- Step – An individual task (e.g., install, test)
- Runner – The virtual machine where jobs are executed
- Event – A GitHub action trigger (push, pull_request, etc.)
5. Real Use Cases
- Run unit tests on pull requests
- Check code style with linters
- Build and push Docker images
- Deploy code to production
6. Helpful Actions
actions/checkout
– Checks out your repoactions/setup-python
– Installs Pythondocker/build-push-action
– Builds and uploads Docker imagesactions/cache
– Caches dependencies between runs
7. Security Best Practices
- Use GitHub Secrets for sensitive info
- Limit permissions on your workflows
- Always pin to specific action versions (e.g.,
@v3
)
8. Debugging Tips
Each job and step has logs available right in the GitHub interface. Failed jobs are marked clearly and can be rerun or edited quickly.