Continuous Integration (CI) and Continuous Deployment (CD) are essential practices for modern software development. CI automates the integration of code changes from multiple contributors into a shared repository, while CD automates the deployment of those changes to production environments. This tutorial will guide you through setting up a CI/CD pipeline using Git and GitHub Actions.
Before you begin, ensure you have the following:
Create a New Repository:
ci-cd-demo.Clone the Repository Locally:
git clone https://github.com/your-username/ci-cd-demo.git
cd ci-cd-demo
Add Sample Code:
git add .
git commit -m "Initial commit"
git push origin main
GitHub Actions is a powerful CI/CD platform that allows you to automate workflows directly in your repository.
Navigate to the Actions Tab:
Set Up a New Workflow:
Configure the Workflow:
Create a file named .github/workflows/nodejs.yml with the following content:
name: Node.js CI/CD
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
deploy:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: 14.x
- run: npm install
- run: npm run build
- name: Deploy to Vercel
uses: vercel/vercel-action@v2
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
vercel_token: ${{ secrets.VERCEL_TOKEN }}
app_id: your-vercel-app-id
push and pull_request events to the main branch.To securely manage secrets like API tokens, use GitHub Secrets:
VERCEL_TOKEN with your Vercel deployment token.By following this tutorial, you have set up a CI/CD pipeline using GitHub Actions. This setup automates testing and deployment, helping you deliver code changes more efficiently and reliably. As your project grows, consider adding more complex workflows to handle different environments (e.g., staging, production) and additional tasks like code linting or security checks.
Feel free to explore more advanced features of GitHub Actions and customize the workflow to fit your specific development needs.