Git & GitHub - Git and CI/CD
Using Git in continuous integration and delivery
Continuous Integration (CI) and Continuous Delivery (CD) are practices that automate the processes of code integration, testing, and deployment. Git is an integral part of these practices, enabling smooth integration with CI/CD pipelines. This guide covers how to use Git in CI/CD workflows.
Key Points:
- CI/CD automates the process of integrating, testing, and deploying code.
- Git hooks and CI/CD tools like Jenkins, GitHub Actions, and GitLab CI are essential for setting up CI/CD pipelines.
- Effective use of branches and tags in Git can streamline CI/CD workflows.
Setting Up CI/CD Pipelines
Using GitHub Actions
GitHub Actions provides a way to automate workflows directly in your GitHub repository:
# Example: .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 Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
Using Jenkins
Jenkins is a widely-used open-source automation server that supports building, deploying, and automating projects:
# Example: Jenkinsfile
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/example/repo.git'
}
}
stage('Build') {
steps {
sh 'npm install'
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
stage('Deploy') {
steps {
sh 'npm run deploy'
}
}
}
}
Using GitLab CI
GitLab CI is a continuous integration tool built into GitLab. You define CI/CD pipelines in a .gitlab-ci.yml
file:
# Example: .gitlab-ci.yml
stages:
- build
- test
- deploy
build:
stage: build
script:
- npm install
test:
stage: test
script:
- npm test
deploy:
stage: deploy
script:
- npm run deploy
only:
- main
Effective Branching Strategies
Feature Branch Workflow
Use feature branches for developing new features. Merge them back into the main branch after testing:
# Create a new feature branch
$ git checkout -b feature/new-feature
# Develop and commit changes
$ git add .
$ git commit -m "Add new feature"
# Push the feature branch to the remote repository
$ git push origin feature/new-feature
# Create a pull request for code review and merge
Git Flow
Git Flow is a branching model that defines a strict branching strategy for managing releases:
# Initialize Git Flow
$ git flow init
# Start a new feature
$ git flow feature start new-feature
# Finish the feature
$ git flow feature finish new-feature
# Start a release
$ git flow release start 1.0.0
# Finish the release
$ git flow release finish 1.0.0
Using Git Hooks for CI/CD
Pre-Commit Hook
Use a pre-commit hook to run tests before allowing a commit:
# .git/hooks/pre-commit
#!/bin/bash
npm test
if [ $? -ne 0 ]; then
echo "Tests failed. Commit aborted."
exit 1
fi
Post-Receive Hook
Use a post-receive hook to deploy code after it has been pushed to the repository:
# .git/hooks/post-receive
#!/bin/bash
GIT_WORK_TREE=/path/to/deploy git checkout -f main
cd /path/to/deploy
npm install
npm run deploy
Best Practices
Follow these best practices for using Git in CI/CD workflows:
- Keep CI/CD Pipelines Fast: Optimize your pipelines to run quickly and efficiently.
- Use Descriptive Commit Messages: Write clear and descriptive commit messages to make it easier to track changes.
- Maintain Clean Branches: Regularly clean up and delete stale branches to keep the repository organized.
- Automate Tests: Automate tests to ensure code quality and catch issues early.
- Monitor Pipeline Performance: Monitor the performance of your CI/CD pipelines and make improvements as needed.
Summary
This guide covered how to use Git in CI/CD workflows, including setting up pipelines with GitHub Actions, Jenkins, and GitLab CI, using effective branching strategies, and leveraging Git hooks. By integrating Git with CI/CD, you can automate and streamline your development process, ensuring faster and more reliable software delivery.