Git and GitOps
Source Code Management
Two kinds of code
Source code - written in high-level language
Binaries - compiled from the source code
Provided a central location to store your code, version changes and allow multiple developers to collaborate on the same source code.
SCM tools - Git, subversion , mercurial and cvs
Git
Initialize the git repo
$ mkdir repo-name && cd repo-name/ $ git init Initialized empty Git repository in ~/repo-name/.git/
Staging code changes
add remove from staging area
git add
git restore
$ touch file1 $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) file1 nothing added to commit but untracked files present (use "git add" to track) $ git add file1 $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: file1 $ git commit -m "First commit" [master (root-commit) cecfb61] My first commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 file1
Commit History
git log
Differences in commit
git diff <first-commit-id> <second-commit-id>
Amending last commit
git commit --amend
Connecting the local repository to the remote repository
git remote add origin git@github.com:<username>/<repo-name>.git
Pushing changes from the local repository to the remote repository
git push -u origin master
Pulling and rebasing code
pulling - downloading changes
rebasing - applying changes on top of latest remote commit
git pull --rebase
Merge conflict
Create Git Branch
git branch feature/feature1
Switch to branch
git branch feature/feature1
Pushing to branch
git push -u origin feature/feature1
Pull requests
- A pull request is a request for merging a source branch to a base branch.
GitOps
Implementing DevOps so that Git is single source of truth
Focuses on Git to manage infrastructure provisioning and application software deployments.
Why GitOps?
Deploys software quickly.
Provides faster recovery from errors.
Better credential management
Deployments are self-documenting.
Promotes shared ownership and knowledge.
Branching strategies and GitOps workflow
Two kind of repository
Application repository
Environment repository
Deployment models
Push model
Any changes in git repo pushed to environment
Jenkins, CircleCI, or Travis CI + Terraform and ansible is used.
Need to store credentials within tool.
Not recommended
Pull model
agent-based deployment model
Agent monitors changes in git repo and applies it when needed.
Advantage - it monitors and reacts to environment changes alongside repo changes.
No need to store credential in tool as operators is within environment.
Branching strategies - Gitflow, GitHub flow.