Git Cookbook For DevOps, Network & Systems Management - Typepad

Transcription

Git Cookbook for DevOps,Network & Systems ManagementDevOps & DevNet AssociateYvan RooseleerBelgian IT Academy Support Center BiASCAmsterdam 11 May and Geel 13 May 2022

Cisco NetAcad DevNet Associate CourseGit repositories are an integral part of the CiscoNetworking 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 repositoriesLocal and Remote repositoriesUpdating repositoriesAdding and removing folders and filesComparing versionsLoggingGiving access to repositoriesCreating and managing branches and mergersBest practicesGit Hands-on labContinuous Integration/ Continuous Deployment (CI/CD) Pipelines only FYI Pipeline scripting: Commit, Build, Test, Stage, DeployDeployment of Python apps in an agile, containerized environmentCI/CD Hands-on lab

Network Programmability

Network Programmability -- NetDevOps Toolbag Overview

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential8

Version ControlGitOpen sourcegit resetDistributed version controlsystemStream of snapshotsKey difference between Git and otherversion control systems is that Git storesdata as snapshots instead of differences(the delta between the current file and theprevious version)Repositories are often used in automation and CI/CD pipelines

Preparing Git Cheat Sheet

Create a new git repository on the command lineExampleecho "# PythonExperiments" README.mdgit initgit add README.mdgit commit -m "first commit"git branch -M maingit remote add origin t push -u origin main git branch -M is a flag (shortcut) for --move --forceallows renaming the branch even if the new branch name already exists

Push a new git repository on the command lineExamplegit remote add origin t branch -M maingit push -u origin maingit push -uis important for syncing your local git repository with your remote gitrepository when pushing to remote for the first time. The advantage is, youmay use git pull without any arguments.

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential18

Git cloneWhen using git clone (from GitHub, or any source repository) the default name forthe source of the clone is "origin".Using git remote show will display the information about this remote name.Example 1: git remote showResponse: originHere is a method to find the url of the remote repoExample 2: git remote get-url originResponse: https://github.com/yro/python scripts.git

Remote Git Repository URLHere is a method to find the url of the remote url for the nameoriginExample git config --get el/python scripts.git

Remote Git Repository URL (2)Here is a method to find the url of the remote url both forfetching or for pushingExample git remote -vResponse:origin https://github.com/yro/python scripts.git (fetch)origin https://github.com/yro/python scripts.git (push)

Remote Git Remote Repository DetailsExample git remote show originResponse:* remote originFetch URL: https://github.com/yro/python scripts.gitPush URL: https://github.com/yro/python scripts.gitHEAD branch: mainRemote branches:maintrackedmaster trackedLocal branch configured for 'git pull':master merges with remote masterLocal ref configured for 'git push':master pushes to master (up to date)

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential24

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential25

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential26

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential27

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential28

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential29

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential30

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

Git branching Branching is one of Git's major selling pointsBranching enables users to work on code independently without affecting the maincode in the repositoryWhen a repository is created, the code is automatically put on a branch namedmaster 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 Confidential33

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential34

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

Comparing changes with git diff Diffing is a function that takes two input data sets and outputs the changesbetween themgit diff is a multi-use Git command that when executed runs a diff function onGit 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.txtMetadata displayed by gitInternally 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 changesThese 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 changesfrom b/diff test.txt are marked with the symbol.

Comparing changes with git diff (4)Diff chunksThe 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.txtTo compare a specific file across branches, pass in the path of the file as the thirdargument to git diff

Practising git diffdef add(n1,n2):return n1 n2def add(n1,n2):return n1 n2def subtract(n1, n2):return n1-n2def subtract(n1, n2):return n1-n2def multiply(n1, n2):return n1*n2def multiply(n1, n2):return n1*n2def divide(n1, n2):CHANGED FILE line 8return n1/n2def main():print (add(100,10))if name " main ":main()def divide(n1, n2): line 8if n2 ! 0:return n1/n2else:return ("Error: Division by Zero")def main():print (add(100,10))print (subtract(100,10))print (multiply(100,10))print (divide(100,10))

Practising git diff --B git diffdiff --git a/calc.py b/calc.pyindex 2519d86.57a02fd 100644--- a/calc.py b/calc.py@@ -8,10 8,16 @@ def multiply(n1, n2):return n1*n2def 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 "yro@biasc.com" git config --list git init git clone https://github.com/CiscoDevNet/python code samples network git status

Tips for Deploying Python ApplicationsCreate a Python Virtual EnvironmentInstall the necessary libraries and functionalitiesCreate a git repositoryPush, Pull or Clone when necessaryCreate branches and test code before merging into main branchUse pipelines to automate deployment CI/CD

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential53

DevASC 4.0 -- Continuous Integration/ Continuous DeliveryCI/CD Pipeline

GitLab

Jenkins Pipeline StagesThe main role of Jenkins is to execute the stages ofa pipeline and to verify/ test the resultsJenkins Pipeline is a suite of plugins that supportsimplementing and integrating continuous deliverypipelines into Jenkins. Pipeline provides anextensible set of tools for modelingsimple-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 pipelineJenkins JobsPreparationRetrieve a version of the codefrom GitHubBuildRun the build scriptTestingTest the build to ensure thatit’s working properlyPipeline in three stagesPreparationBuildTestingStages are run sequentially. If anearly stage fails, the pipeline stops

Create a Jenkins job or pipeline: new itemBuildFlaskAppJob 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential60

Use Personal Access TokenIn place of password 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential61

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential62

stages 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential63

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential64

2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential65

Practical Examples 2016 Cisco and/or its affiliates. All rights reserved. Cisco Confidential66

Git fork vs. clone: What's the difference?Any public Git repositorycan be forked or cloned. Afork creates a completelyindependent copy of Gitrepository. In contrast to afork, a Git clone creates alinked copy that willcontinue to synchronizewith the target repository.

What’s the aim of the following git commands? git config --global user.email "yro@biasc.com" 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 Confidential72

Infrastructure Automation: IaC Tooling ComparisonInfrastructure as Code, Repositories GitInfrastructure as Code, Cloud Provisioning TerraformInfrastructure as Code, Configuration management AnsibleContinuous Integration/Deployment Docker, KubernetesContinuous Integration/Pipelines Jenkins, Github Actions, GitlabConfig/Secret Management Consul, etcd, VaultMonitoring PrometheusConnecting, managing, and securing microservices (service mesh) Istio

Devops Terms & ConceptsRepository gitA 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 buildIt 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, kubernetesA container is a (micro)service available to users by means of an IP address and a Port. IN order to make an applicationavailable it is necessary to run or deploy the image. As many services as necessary for one application.Pipeline jenkinsA jenkins pipeline consists of stages in order to deploy a (micro)service. The pipeline starts with fetching the relevantversion of the application code from a git repository. Then a docker image is created and stored locally (or on github). In thenext 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 systemGitHub: a platform for hosting and collaborating on Git repositoriescommit: a Git object, a snapshot of your entire repository compressed into a SHAbranch: a lightweight movable pointer to a commitclone: a local version of a repository, including all commits and branchesremote: a common repository on GitHub that all team member use to exchangetheir changesfork: a copy of a repository on GitHub owned by a different userpull request: a place to compare and discuss the differences introduced on abranch with reviews, comments, integrated tests, and moreHEAD: representing your current working directory, the HEAD pointer can bemoved to different branches, tags, or commits when using git checkout

Pro Git book, written by Scott Chacon and Ben StraubGetting StartedGit BasicsGit BranchingGit on the ServerDistributed GitGitHubGit Tools

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. . Devops Terms & Concepts . Git Cheat Sheet - 2. Git Cheat Sheet - 1. Git Cheat Sheet - 3. Git Cheat Sheet - 4.