0% found this document useful (0 votes)
23 views

Git Cookbook For Devops Network Systems Management 3

The document discusses using Git for version control of source code, configuration scripts, and documentation. It provides an overview of managing Git repositories including planning, creating, updating repositories, adding/removing files, comparing versions, and access control. It also discusses best practices for Git and includes a hands-on lab. The document then discusses using Git repositories in automation and continuous integration/continuous delivery pipelines. It includes a Git cheat sheet with commands for creating repositories, pushing to remote repositories, cloning repositories, comparing changes, and branching.

Uploaded by

fortune
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Git Cookbook For Devops Network Systems Management 3

The document discusses using Git for version control of source code, configuration scripts, and documentation. It provides an overview of managing Git repositories including planning, creating, updating repositories, adding/removing files, comparing versions, and access control. It also discusses best practices for Git and includes a hands-on lab. The document then discusses using Git repositories in automation and continuous integration/continuous delivery pipelines. It includes a Git cheat sheet with commands for creating repositories, pushing to remote repositories, cloning repositories, comparing changes, and branching.

Uploaded by

fortune
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

Git Cookbook for DevOps,

Network & Systems Management


DevOps & DevNet Associate
Yvan Rooseleer
Belgian IT Academy Support Center BiASC
Amsterdam 11 May and Geel 13 May 2022
Cisco NetAcad DevNet Associate Course

Git repositories are an integral part of the Cisco


Networking Academy DevNet Associate Course.

Git is able to manage:


- Source code (software)
- Configuration scripts (systems, networks, security)
- Documentation
Managing Git (and CI/CD Pipelines)
Git Repositories

● Planning, creating and managing repositories


● Local and Remote repositories
● Updating repositories
● Adding and removing folders and files
● Comparing versions
● Logging
● Giving access to repositories
● Creating and managing branches and mergers
● Best practices
● Git Hands-on lab

Continuous Integration/ Continuous Deployment (CI/CD) Pipelines => only FYI

● Pipeline scripting: Commit, Build, Test, Stage, Deploy


● Deployment of Python apps in an agile, containerized environment
● CI/CD Hands-on lab
Network Programmability
Network Programmability -- NetDevOps Toolbag Overview
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 8
Version Control
Git
Open source

git reset Distributed version control


system

Stream of snapshots
Key difference between Git and other
version control systems is that Git stores
data as snapshots instead of differences
(the delta between the current file and the
previous version)

Repositories are often used in automation and CI/CD pipelines


Preparing Git => Cheat Sheet
Create a new git repository on the command line
Example
echo "# PythonExperiments" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/yroosel/PythonExperiments.git
git push -u origin main
● git branch -M is a flag (shortcut) for --move --force
allows renaming the branch even if the new branch name already exists
Push a new git repository on the command line
Example
git remote add origin https://github.com/yroosel/PythonExperiments.git
git branch -M main
git push -u origin main

git push -u
is important for syncing your local git repository with your remote git
repository when pushing to remote for the first time. The advantage is, you
may use git pull without any arguments.
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 18
Git clone
When using git clone (from GitHub, or any source repository) the default name for
the source of the clone is "origin".

Using git remote show will display the information about this remote name.

Example 1: $ git remote show


Response: origin

Here is a method to find the url of the remote repo

Example 2: $ git remote get-url origin


Response: https://github.com/yro/python_scripts.git
Remote Git Repository URL

Here is a method to find the url of the remote url for the name
origin
Example $ git config --get remote.origin.url

Response:
https://github.com/yroosel/python_scripts.git
Remote Git Repository URL (2)

Here is a method to find the url of the remote url both for
fetching or for pushing
Example $ git remote -v
Response:
origin https://github.com/yro/python_scripts.git (fetch)
origin https://github.com/yro/python_scripts.git (push)
Remote Git Remote Repository Details

Example $ git remote show origin


Response:
* remote origin
Fetch URL: https://github.com/yro/python_scripts.git
Push URL: https://github.com/yro/python_scripts.git
HEAD branch: main
Remote branches:
main tracked
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 24
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 25
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 26
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 27
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 28
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 29
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 30
*Main is often used as a replacement of master

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 31
Git branching

● Branching is one of Git's major selling points


● Branching enables users to work on code independently without affecting the main
code in the repository
● When a repository is created, the code is automatically put on a branch named
master
● Users can have multiple branches and those are independent of each other as well
$ git merge testing

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 33
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 34
*Main is often used as a replacement of master

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 35
Comparing changes with git diff
● Diffing is a function that takes two input data sets and outputs the changes
between them
● git diff is a multi-use Git command that when executed runs a diff function on
Git data sources.
● These data sources can be commits, branches, files and more
Comparing changes with git diff (2)
Comparison input

$ diff --git a/diff_test.txt b/diff_test.txt


Metadata displayed by git
Internally git works with version hash identifiers and therefore shows (part of the )
the hashes of the two files.

Example:
index 5c3b621..a0d8bf9 100644
Comparing changes with git diff (3)
Markers for changes

These lines are a legend that assigns symbols to each diff input source.

In this case, changes from a/diff_test.txt are marked with a --- and the changes
from b/diff_test.txt are marked with the +++ symbol.
Comparing changes with git diff (4)
Diff chunks

The main diff output is a list of diff 'chunks'

A diff only displays the sections of the file that have changes.

The example shows that one line was deleted and one line was added.
This indicates a replacement or an update
Comparing changes with git diff (5)
Comparing files from two branches

$ git diff main new_branch ./diff_test.txt


To compare a specific file across branches, pass in the path of the file as the third
argument to git diff
CHANGED FILE
Practising git diff
def add(n1,n2):
def add(n1,n2):
return n1+n2
return n1+n2
def subtract(n1, n2):
def subtract(n1, n2):
return n1-n2
return n1-n2
def multiply(n1, n2):
def multiply(n1, n2):
return n1*n2
return n1*n2

def divide(n1, n2): ⇐ line 8


def divide(n1, n2): ⇐ line 8
if n2 != 0:
return n1/n2
return n1/n2
else:
def main():
return ("Error: Division by Zero")
print (add(100,10))
def main():
if __name__ == "__main__":
print (add(100,10))
main()
print (subtract(100,10))
print (multiply(100,10))
print (divide(100,10))
Practising git diff --B
>git diff
diff --git a/calc.py b/calc.py
index 2519d86..57a02fd 100644
--- a/calc.py
+++ b/calc.py
@@ -8,10 +8,16 @@ def multiply(n1, n2):
return n1*n2

def divide(n1, n2):


- return n1/n2
+ if n2 != 0:
+ return n1:n2
+ else:
+ return ("Error: Division by Zero")

def main():
print (add(100,10))
+ print (subtract(100,10))
+ print (multiply(100,10))
+ print (divide(100,10))
Downloading git existing repository
$ git config --global user.email "[email protected]"

$ git config --list

$ git init

$ git clone https://github.com/CiscoDevNet/python_code_samples_network

$ git status
Tips for Deploying Python Applications
Create a Python Virtual Environment

Install the necessary libraries and functionalities

Create a git repository

Push, Pull or Clone when necessary

Create branches and test code before merging into main branch

Use pipelines to automate deployment => CI/CD


© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 53
DevASC 4.0 -- Continuous Integration/ Continuous Delivery

CI/CD Pipeline
GitLab
Jenkins Pipeline => Stages
The main role of Jenkins is to execute the stages of
a pipeline and to verify/ test the results

Jenkins Pipeline is a suite of plugins that supports


implementing and integrating continuous delivery
pipelines into Jenkins. Pipeline provides an
extensible set of tools for modeling
simple-to-complex delivery pipelines "as code"

There are plugins for: Bash, Git, Docker, Ansible,


and many more ...
DevASC 4.0 -- Components of a CI/CD pipeline
Jenkins Jobs
Preparation
Retrieve a version of the code
from GitHub
Build
Run the build script
Testing
Test the build to ensure that
it’s working properly

Pipeline in three stages


Preparation
Build
Testing

Stages are run sequentially. If an


early stage fails, the pipeline stops
Create a Jenkins job or pipeline: new item

BuildFlaskAppJob

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 60
Use Personal Access Token
In place of password

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 61
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 62
stages

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 63
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 64
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 65
Practical Examples

© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 66
Git fork vs. clone: What's the difference?

Any public Git repository


can be forked or cloned. A
fork creates a completely
independent copy of Git
repository. In contrast to a
fork, a Git clone creates a
linked copy that will
continue to synchronize
with the target repository.
What’s the aim of the following git commands?
$ git config --global user.email "[email protected]"

$ git config --list

$ git checkout -b test origin /main

$ git branch

$ git fetch

$ git diff

$ git remote -v

$ git add .

$ git status
© 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 72
Infrastructure Automation: IaC Tooling Comparison
Infrastructure as Code, Repositories => Git

Infrastructure as Code, Cloud Provisioning => Terraform

Infrastructure as Code, Configuration management => Ansible

Continuous Integration/Deployment => Docker, Kubernetes

Continuous Integration/Pipelines => Jenkins, Github Actions, Gitlab

Config/Secret Management => Consul, etcd, Vault

Monitoring => Prometheus

Connecting, managing, and securing microservices (service mesh) => Istio


Devops Terms & Concepts
Repository => git
A collection of scripts, programs and documents is called a repository.
It is possible to manage and store files locally or on github. One repo per application. One application per repository.

Image => docker build


It is possible to create a (micro)service for a specific application.
An image is the file containing all the necessary artifacts for that (micro)service.
Typically, we use a dockerfile to create an image.
An image is stored on a file system or on docker hub. One image per application. One application per image.

Container => docker run, docker compose, kubernetes


A container is a (micro)service available to users by means of an IP address and a Port. IN order to make an application
available it is necessary to run or deploy the image. As many services as necessary for one application.

Pipeline => jenkins


A jenkins pipeline consists of stages in order to deploy a (micro)service. The pipeline starts with fetching the relevant
version of the application code from a git repository. Then a docker image is created and stored locally (or on github). In the
next phase it is possible to make the application image available (run) to make sure that users can connect to it.
Git Cheat Sheet - 2
Git Cheat Sheet - 1
Git Cheat Sheet - 3
Git Cheat Sheet - 4
Git Cheat Sheet - Glossary

● git: an open source, distributed version-control system


● GitHub: a platform for hosting and collaborating on Git repositories
● commit: a Git object, a snapshot of your entire repository compressed into a SHA
● branch: a lightweight movable pointer to a commit
● clone: a local version of a repository, including all commits and branches
● remote: a common repository on GitHub that all team member use to exchange
their changes
● fork: a copy of a repository on GitHub owned by a different user
● pull request: a place to compare and discuss the differences introduced on a
branch with reviews, comments, integrated tests, and more
● HEAD: representing your current working directory, the HEAD pointer can be
moved to different branches, tags, or commits when using git checkout
Pro Git book, written by Scott Chacon and Ben Straub

Getting Started
Git Basics
Git Branching
Git on the Server
Distributed Git
GitHub
Git Tools

References
Cisco Networking Academy, DevNet Associate 1.0 (DEVASC)
URL: www.netacad.com
Login necessary

C. Jackson, A Practical Introduction to DevOps Practices and Tools,


DGTL-BRKCLD-1003, CiscoLive 2020, URL: Hyperlink
Login necessary

H. Preston, Overview of Git, Introduction to Version Control, Lesson 2,


Cisco Learning Network and DevNetOctober 2020, URL: Hyperlink,
Login necessary

CiscoDevNet, python_code_samples_network, Cisco DevNet, GitHub,


URL: Hyperlink
References 2
A. Khan, DevOps Culture at Amazon, Amazon Web Services, 47.01, 2 JUL
2018, YouTube, URL: https://www.youtube.com/watch?v=mBU3AJ3j1rg

Atlassian BitBucket, Comparing changes with git diff


URL: URL: https://www.atlassian.com/git/tutorials/saving-changes/git-diff

Semaphore, CI/CD Pipeline: A Gentle Introduction, URL: Hyperlink

S. Chacon & B. Straub, Pro Git Book, 2nd Edition (2014),


URL: http:/com/book/en/v2/git-scm

You might also like