How to Set Up a CI/CD Pipeline with GitHub Actions
Setting up a CI/CD pipeline with GitHub Actions automates code testing, building, and deployment directly from your repository. You start by creating a `.github/workflows` folder in your project root, adding a YAML file (e.g., `deploy.yml`) that defines triggers like `push` to `main`, then specifying jobs for linting, testing, and deploying to platforms like AWS, Azure, or a VPS. This eliminates manual errors, speeds up releases, and ensures every commit is production-ready. Below is a complete, practical guide to build your first pipeline using GitHub Actions, including real YAML examples and optimization tips for developers using freefile.store resources.

*Photo by RDNE Stock project on Pexels*
## What is a CI/CD Pipeline?
Continuous Integration (CI) automatically tests and merges code changes. Continuous Delivery (CD) deploys those changes to staging or production. GitHub Actions provides built-in runners, secrets management, and a vast marketplace of actions to handle both.
### Key Benefits
- Faster feedback – catch bugs within minutes
- Consistent builds – same environment every time
- Zero manual steps – no SSH or FTP errors
- Free tier – 2000 minutes/month for public repos
## Prerequisites
Before writing your first workflow, ensure you have:
- A GitHub repository (public or private)
- Basic YAML knowledge
- Deployment credentials (API keys, SSH keys, cloud tokens)
- A project with a test suite (e.g., Jest for Node.js, PyTest for Python)
## Step 1: Create Your Workflow File
Navigate to your repo root, create the directory structure:
“`
mkdir -p .github/workflows
touch .github/workflows/ci-cd.yml
“`
Open `ci-cd.yml` and add the following skeleton:
“`yaml
name: CI/CD Pipeline
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: npm test
“`
This triggers on every push to `main` and runs your tests.
## Step 2: Define Jobs for Testing and Linting
Expand your workflow with multiple jobs. Here’s a Node.js example:
“`yaml
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: npm ci
- name: Run ESLint
run: npm run lint
test:
needs: lint
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16, 18, 20]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
“`
Why a matrix? It tests across multiple Node versions, ensuring compatibility.

*Photo by Towfiqu barbhuiya on Pexels*
## Step 3: Add Deployment Job
For CD, you need credentials stored as GitHub Secrets:
- Go to repo Settings > Secrets and variables > Actions
- Add keys like `SERVER_SSH_KEY`, `DOCKER_PASSWORD`, or `AWS_ACCESS_KEY_ID`
Deploy to a VPS via SSH:
“`yaml
deploy:
needs: test
runs-on: ubuntu-latest
if: github.ref == ‘refs/heads/main’
steps:
- uses: actions/checkout@v4
- name: Deploy to Server
uses: appleboy/[email protected]
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
cd /var/www/myapp
git pull origin main
npm install –production
pm2 restart app
“`
This pulls the latest code, installs production dependencies, and restarts the app with PM2.
## Comparison Table: GitHub Actions vs. Other CI/CD Tools
| Feature | GitHub Actions | Jenkins | GitLab CI |
|---|---|---|---|
| Setup time | 5 minutes | 30+ minutes | 10 minutes |
| Hosting | Cloud (free tier) | Self-hosted | Cloud/Self-hosted |
| YAML syntax | Simple, readable | Complex (Groovy) | Moderate |
| Marketplace actions | 10,000+ | 1,000+ plugins | Limited |
| Cost for private repos | Free 2000 min/month | Free (self-hosted) | Free 400 min/month |
| Matrix builds | Native support | Plugin required | Native support |
Winner for developers: GitHub Actions – easiest to start, tightest GitHub integration.
## Step 4: Optimize Build Speed
Slow pipelines waste time. Apply these optimizations:
- Cache dependencies – use `actions/cache@v4`
- Parallel jobs – run lint, test, and build simultaneously
- Conditional steps – skip deploy on pull requests
- Self-hosted runners – for heavy workloads (free on your own hardware)
Example caching for npm:
“`yaml
- name: Cache node_modules
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles(‘**/package-lock.json’) }}
restore-keys: |
${{ runner.os }}-node-
“`
## Step 5: Add Notifications and Alerts
Get notified when your pipeline fails. Add a Slack or email step:
“`yaml
- name: Notify on failure
if: failure()
uses: slackapi/[email protected]
with:
payload: |
{
“text”: “Pipeline failed in ${{ github.repository }}”
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}
“`
## Common Pitfalls to Avoid
- Hardcoding secrets – always use `${{ secrets.* }}`
- Running long tests on every push – use `paths-ignore` for docs
- Missing `needs` declarations – jobs run in parallel unless specified
- Forgetting to pin action versions – use `@v4` not `@main`

*Photo by Daniil Komov on Pexels*
## Real-World Example: Full Pipeline for a Python App
Here’s a complete workflow for a Python Flask app:
“`yaml
name: Python CI/CD
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ‘3.11’
- run: pip install -r requirements.txt
- run: pytest –cov=app
build-and-deploy:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build Docker image
run: docker build -t myapp:${{ github.sha }} .
- name: Push to Docker Hub
run: |
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} –password-stdin
docker push myapp:${{ github.sha }}
- name: Deploy to Kubernetes
uses: actions-hub/kubectl@master
env:
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}
with:
args: set image deployment/myapp myapp=myapp:${{ github.sha }}
“`
## Testing Your Pipeline
Push a commit to trigger the workflow:
“`bash
git add .
git commit -m “Add CI/CD pipeline”
git push origin main
“`
Monitor progress under your repo’s Actions tab. Fix any red jobs by checking logs.
## Conclusion
You now have a complete CI/CD pipeline with GitHub Actions that tests, builds, and deploys your code automatically. Start small with linting and testing, then add deployment as you gain confidence. For more ready-to-use scripts, automation templates, and DevOps tools, visit freefile.store – download pre-built YAML workflows, Dockerfiles, and deployment scripts to accelerate your setup. Get your free resources now at freefile.store and ship code faster with zero manual effort.