Setting Up CI/CD with GitHub Actions
Learn how to create a complete CI/CD pipeline using GitHub Actions for building, testing, and deploying applications.
ci-cdgithub-actionsautomation
What You’ll Build
In this tutorial, you’ll create a GitHub Actions workflow that:
- Runs tests on every pull request
- Builds a Docker image on merge to main
- Deploys to a staging environment
Step 1: Create the Workflow File
Create .github/workflows/ci.yml in your repository:
name: CI/CD Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm test
build:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/build-push-action@v5
with:
push: true
tags: ghcr.io/${{ github.repository }}:latest
Step 2: Configure Secrets
Go to your repository settings and add the following secrets:
REGISTRY_TOKEN- Your container registry tokenDEPLOY_KEY- SSH key for deployment
Step 3: Add a Deployment Step
Extend the workflow with a deployment job:
deploy:
needs: build
runs-on: ubuntu-latest
environment: staging
steps:
- name: Deploy to staging
run: |
echo "Deploying to staging..."
Summary
You now have a working CI/CD pipeline that automatically tests, builds, and deploys your application.