Welcome to the first part of our DevOps Pipeline series! In this post, we’re kicking things off with Source Code Management (SCM)—the bedrock of any efficient DevOps workflow. By using Git and GitHub, you’ll learn how to organize your code, manage branches, and collaborate seamlessly with your team.
SCM is your first step toward building a fully automated pipeline, so let’s dive in and set the stage for smooth development and powerful automation!
Table Of Contents
1. Why SCM is Crucial for DevOps
In the world of DevOps, a Source Code Management system allows teams to:
- Track Code Changes: Every change made to the codebase is logged, ensuring transparency and traceability.
- Collaborate Effectively: Multiple developers can work simultaneously on different features, bug fixes, or updates without conflicts.
- Automate CI/CD Workflows: SCM integrates seamlessly with CI/CD tools, enabling automation of testing, building, and deploying code.
By using Git for SCM, and leveraging GitHub as a platform, you can implement a robust workflow that lays the foundation for an efficient DevOps pipeline.
2. Getting Started with Git
Git is an open-source version control system, and you can install it on any operating system.
For Windows: Download the installer from git-scm.com.
For macOS: Use Homebrew to install Git:
brew install git
For Linux: Use the package manager:
sudo apt-get install git
After installation, verify Git by checking its version:
git --version
Once installed, configure Git with your name and email address:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
This information will be associated with all your commits.
3. Setting Up a GitHub Repository
3.1 Creating a New Repository on GitHub
- Sign up or log in to GitHub: Visit GitHub and create an account if you don’t already have one.
- Create a new repository:
- Click on the “+” icon in the top right corner.
- Select New Repository.
- Fill out the details: name, description, and privacy (public or private).
- Initialize with a README: Optionally, add a README file to describe your project.
- Clone the repository to your local machine:
- Go to your repository’s page.
- Click Code > HTTPS and copy the URL.
- Clone the repo:
git clone https://github.com/username/my-devops-project.git
3.2 Connecting Local Repository to GitHub
To push an existing local project to a new GitHub repository:
1. Navigate to your local project
cd path/to/your/project
2. Initialize Git:
git init
3. Add all project files to Git:
git add . git commit -m "Initial commit"
4. Link to GitHub:
git remote add origin https://github.com/username/my-devops-project.git git branch -M main git push -u origin main
4. Branching Strategies and Collaboration
4.1 Creating and Merging Branches
Branching allows developers to work on isolated features, bugs, or changes without affecting the main codebase.
Creating a New Branch:
git checkout -b feature/my-feature
This creates and switches to a new branch named feature/my-feature
.
Switching Between Branches:
git checkout main
Merging a Branch: Once the feature is complete, merge it back to main
:
git checkout main git merge feature/my-feature
4.2 Using Pull Requests for Code Reviews
Pull Requests (PRs) are an essential part of collaboration, allowing team members to review and discuss code before it’s merged.
Creating a Pull Request on GitHub:
1. Push your branch to GitHub:
git push origin feature/my-feature
2. Go to your repository on GitHub, and you’ll see a prompt to open a new pull request.
3. Fill out the details, assign reviewers, and submit.
Reviewing and Approving a PR:
- Once a PR is open, reviewers can comment on specific lines, suggest changes, and approve or request changes.
- After approval, the PR can be merged into the
main
branch through GitHub’s interface.
Pro Tip: Use descriptive PR titles and descriptions to make the review process smooth.
5. Pro Tips for Effective SCM
1. Using .gitignore
to Exclude Files
A .gitignore
file specifies files and directories Git should ignore.
touch .gitignore
Add entries like:
node_modules/ .env *.log
2. Tagging Releases with Git
Tags are used to mark specific points in your codebase, such as releases:
git tag -a v1.0 -m "Version 1.0 release" git push origin v1.0
3. Enforcing Code Quality with Git Hooks
Git hooks are scripts that run before or after Git events like commits or pushes. For example, you can enforce code linting before each commit:
1. Create a pre-commit
hook:
touch .git/hooks/pre-commit
2. Add your script to enforce linting, testing, etc:
#!/bin/sh npm run lint
3. Make the hook executable:
chmod +x .git/hooks/pre-commit
6. Conclusion & Next Steps
By setting up Source Code Management (SCM) with Git and GitHub, you’ve built a strong foundation for your DevOps pipeline. You’ve learned how to manage your codebase efficiently, establish branch strategies, and enable seamless collaboration, setting the stage for automation and continuous improvement.
Next Up: It’s time to automate your builds and tests to keep your code quality high and your feedback loop fast. In our next post, we’ll dive into Continuous Integration (CI) with Jenkins, guiding you through setting up automated workflows that trigger every time you push code.
Let’s Keep the SCM Conversation Going!
What branching strategies or Git tips do you find most effective for SCM? Share your thoughts and experiences below! And if you found this post valuable, please share it with your fellow developers and DevOps enthusiasts. 🚀
Read “Automating Continuous Integration (CI) with Jenkins: A Step-by-Step Guide” →
Discover more from Abdelrahman Algazzar
Subscribe to get the latest posts sent to your email.