Day01-01-git
- Content List
- 0. 運維發展過程
- 1. ci-cd-co基礎
- 1.1 軟件的生命周期
- 1)軟件的聲明周期:軟件從0-->1-->0過程
- 2)ci/cd
- 3)Devops
- 4) 小結
- 1.2 什么是環境
- 1.3 什么是代碼部署
- 1.4 代碼發布方式
- 3. DevSecOpt之Git極速入門到放棄
- 3.1 什么是git
- 3.2 應用場景
- 3.3 1分鐘部署git
- 1)Win
- 2)Linux(CentOS/Fedora/Rocky Linux/RHEL)
- 3)MacOS
- 4)Ubuntu/Debian
- 4. lidao直播app辛酸史
- 4.1單槍匹馬勇闖天涯-開發階段
- 4.2 git區域與狀態
- 4.3 git分支 branch
- 1)分支?branch
- 2)詳解
- 3)案例(了解)
- 4)git 分支命令總結
- 4.4 入住CBD(遠程倉庫)
- 1)創建 倉庫
- 2)連接遠程倉庫(用戶名密碼方式)
- 3)連接遠程倉庫(密鑰認證方式)
- 3)倉庫管理命令小結
- 4.5 補充
- 1 tag
- 2 gitignore
- 4.6 總結
Content List
- 運維發展過程
- 軟件生命周期
- CI/CD/CO/DevOps
- 持續部署實施流程以及對應工具?*
- git使用(核心掌握拉取代碼,拉取指定版本代碼,分支)
- gitlab私有代碼倉庫
0. 運維發展過程
- 云原生時代:
以Devops、SRE框架為指導,Docker/K8S/微服務為基礎,用分布式方式加上 Python和Go語言,構建一套云技術產品體系,以及進行高級管理工具的二次開發,實現屬于公司自己的自動化運維體系以及云技術體系的自動化產品。使用的自動化運維產品,可以用ran cherargoCDtecktonCl gitops 等,流水線及管理工具
1. ci-cd-co基礎
1.1 軟件的生命周期
1)軟件的聲明周期:軟件從0–>1–>0過程
- 代碼上線流程:
- 開發書寫代碼
- 把代碼上傳到代碼倉庫
- 通過工具拉取代碼到測試環境中,運行代碼,測試人員測試
- 通過工具拉取代碼部署到生產環境.
2)ci/cd
- ci cd
- ci:持續集成:開發的代碼集成到代碼倉庫。
- cd:持續交付:從代碼倉庫拉取代碼部署到測試環境。
- cd:持續部署:從代碼倉庫拉取代碼部署到生產環境。
3)Devops
-
DevOps: 理念讓開發人員可以持續集成,運維人員可以持續交付和持續部署.讓整個開發過程自動部署,上線…
- 開發寫完的代碼自動集成,自動交付與部署。本質是自動化。
- DevOps是一套快速、頻繁、可靠的軟件交付實踐.
-
DevOps
- Development 開發
- Operation 運維
-
DevOPS目標是:解決開發書寫代碼,集成,交付,部署進度,審核緩慢。
-
DevOPS實現
- 開發(存放收集代碼 CI):git/gitlab/github/gitee(代碼平臺)
- 運維(部署代碼 CD):jenkins maven …插件 …sonarqube (運維)
DevSecOps
Dev開發
Sec安全
Ops運維
4) 小結
- 熟悉 軟件生命周期
- 必會:ci/cd/devops 持續集成 持續交付 持續部署
1.2 什么是環境
- 開發環境
- 測試環境
- 預發布環境/準生產環境/預生產環境
- 生產環境
- 誰部署,誰維護/負責
準生產環境/預生產環境 | 生產環境 | |
---|---|---|
相同點 | 存放代碼,服務配置與生產環境一致 | 存放代碼… |
區別 | 服務器的配置可以低一些,數量少一些,甚至省錢可以用內部測試環境兼職 | |
關于數據庫 | 連接生產環境數據庫(從庫),nginx鏡像流量(alb鏡像流量) |
1.3 什么是代碼部署
- 代碼發布/部署 --> 開發書寫的程序代碼 --> 部署測試/生產環境 web服務器(站點目錄中)
1.4 代碼發布方式
發布方式 | 效率 | 干擾 | 安全性 |
---|---|---|---|
手動發布 | 慢 | 干擾因素很多 | 不安全 |
自動發布 | 快 | 干擾因素很少 | 安全 |
3. DevSecOpt之Git極速入門到放棄
- 學習目標:
- 使用Git命令不參考文檔的情況下可以拉取代碼,上傳代碼
- 理解什么是分支。
- 可以給公共代碼倉庫和私有代碼倉庫配置密鑰認證訪問
角色 | 主機名 | ip | 內存 |
---|---|---|---|
gitlab私有代碼倉庫 | oldboy-devops-gitlab | 10.0.0.71/172.16.1.71 | 2G(至少) 推薦2C4G |
jenkins | oldboy-devops-jenkins | 10.0.0.72/172.16.1.72 | 1G(至少) 推薦2C4G |
sonarqube代碼檢查 | oldboy-devops-sonar | 10.0.0.73/172.16.1.73 | 1c1G |
nexus私服 | oldboy-devops-nexus | 10.0.0.74/172.16.1.74 | 1c1G |
3.1 什么是git
-
scm
-
分布式版本控制系統
- 分布式:svn vs git
- 版本控制
- 軟件
Git | SVN | |
---|---|---|
共同點 | 存放代碼,版本控制 | 存放代碼,版本控制 |
工作模式 | 分布式 | 中心化(權限集中) |
使用 | 入門較難,熟練后容易使用,目前使用率高 | 入門比較簡單,服務端linux,客戶端windows |
分支 | 創建和維護分支方便 | 創建和維護分支繁瑣 |
3.2 應用場景
3.3 1分鐘部署git
- 其他系統安裝傳送門https://git-scm.com/docs
1)Win
https://git-scm.com/download/
2)Linux(CentOS/Fedora/Rocky Linux/RHEL)
#C7
yum install -y git#c8
dnf install -y git #或yum也可以
3)MacOS
#需要提前安裝brew
brew install git
4)Ubuntu/Debian
#
apt install -y git
4. lidao直播app辛酸史
- 單槍匹馬勇闖天涯-開發階段
4.1單槍匹馬勇闖天涯-開發階段
- 配置git相關信息
#創建項目目錄(代碼目錄)#成為git倉庫#配置用戶
git config
git config --global user.name 'oldboy'
git config --global user.email '918391635@qq.com'
git config --global color.ui true
git config --global --list
- 代碼目錄初始化
#初始化代碼目錄
[root@devops-gitlab ~]# mkdir -p /app/code/live-lidao-app
[root@devops-gitlab ~]# cd /app/code/live-lidao-app
[root@devops-gitlab live-lidao-app]# ll -a
總用量 0
drwxr-xr-x 2 root root 6 2024-05-15 10:20 .
drwxr-xr-x 3 root root 28 2024-05-15 10:20 ..
[root@devops-gitlab live-lidao-app]# git init
初始化空的 Git 版本庫于 /app/code/live-lidao-app/.git/
[root@devops-gitlab live-lidao-app]# ll -a
總用量 0
drwxr-xr-x 3 root root 18 2024-05-15 10:20 .
drwxr-xr-x 3 root root 28 2024-05-15 10:20 ..
drwxr-xr-x 7 root root 119 2024-05-15 10:20 .git
[root@devops-gitlab live-lidao-app]# ll .git/
總用量 12
drwxr-xr-x 2 root root 6 2024-05-15 10:20 branches
-rw-r--r-- 1 root root 92 2024-05-15 10:20 config
-rw-r--r-- 1 root root 73 2024-05-15 10:20 description
-rw-r--r-- 1 root root 23 2024-05-15 10:20 HEAD
drwxr-xr-x 2 root root 242 2024-05-15 10:20 hooks
drwxr-xr-x 2 root root 21 2024-05-15 10:20 info
drwxr-xr-x 4 root root 30 2024-05-15 10:20 objects
drwxr-xr-x 4 root root 31 2024-05-15 10:20 refs
- git相關的倉庫
- 書寫代碼,提交到暫存區
#書寫代碼 添加文件
echo oldboy >index.html
echo new app 完成度40% >index.html#查看狀態 倉庫狀態
git status
# 位于分支 master
#
# 初始提交
#
# 未跟蹤的文件:
# (使用 "git add <file>..." 以包含要提交的內容)
#
# index.html
提交為空,但是存在尚未跟蹤的文件(使用 "git add" 建立跟蹤)#進行提交
git add .
git status
# 位于分支 master
#
# 初始提交
#
# 要提交的變更:
# (使用 "git rm --cached <file>..." 撤出暫存區)
#
# 新文件: index.html
#
- 提交代碼到本地倉庫
#提交到本地倉庫
git commit -m '項目開始 完成60%'
[root@devops-gitlab live-lidao-app]# git commit -m "說明:項目剛剛成立,代碼40%"
[master(根提交) 5faade3] 說明:項目剛剛成立,代碼40%1 file changed, 1 insertion(+)create mode 100644 index.html#查看狀態
git status
# 位于分支 master
無文件要提交,干凈的工作區
- 再次修改文件并提交
#修改文件內容# 再次提交
git add .
git commit -m '項目完成90%'
- 進行回滾一步到位的
git checkout .
名字 | 含義 |
---|---|
git init | 初始化本地倉庫目錄 |
git config --global | 郵箱,用戶名,顏色 |
git add | 提交數據到緩沖區(暫存區) git add . (所有文件) 或 git add 文件 |
git commit | 把暫存區的數據提交到本地倉庫 git commit -m “標記/說明” |
git status | 顯示工作空間的狀態 |
git reset | 回滾 |
git reset --soft cid(版本號) | 把指定的版本數據內容下載到暫存區 |
git reset HEAD | 暫存區 -->工作空間(被修改的狀態) |
git checkout | 文件下載到工作空間并可以使用 git checkout . 或 git checkout 文件 |
git reset --mix 版本號 | |
git reset --hard 版本號 | 把本地倉庫指定版本信息數據下載到工作目錄中 |
4.2 git區域與狀態
- 區域
- 狀態了解 熟悉
4.3 git分支 branch
1)分支?branch
分支即是平行空間,假設你在為某個手機系統研發拍照功能,代碼已經完成了80%,但如果將這不完整的代碼直接
提交到git倉庫中,又有可能影響到其他人的工作,此時我們便可以在該軟件的項目之上創建一個名叫”拍照功能”的分支,
這種分支只會屬于你自己,而其他人看不到,等代碼編寫完成后再與原來的項目主分支合并下即可,這樣即能保證代碼不丟失,又不影響其他人的工作。
默認的分支-master 主分支,這個分支的代碼一般都是可用,可以部署到生產環境的。
一般開發人員開發代碼的時候創建dev分支,shopping分支。
應用名稱分支每個分支對應獨立功能。
2)詳解
3)案例(了解)
- 主分支完成提交
[root@devops-gitlab live-lidao-app]# cat index.html
new app live 完成度100%
[root@devops-gitlab live-lidao-app]# git add .
[root@devops-gitlab live-lidao-app]# git commit -m "live_app_100%"
[master b0d7f3d] live_app_100%1 file changed, 1 insertion(+), 1 deletion(-)
- 創建shopping分支 書寫代碼
#查看分支
git branch
* master#創建分支
git branch shopping
git branch
* mastershopping#切換分支
git checkout shopping
Switched to branch 'shopping'
#查看 切換結果 git branchmaster
* shopping#書寫shopping代碼·并提交
echo 'shopping 90%' >shopping.html
git add .
git commit -m 'shopping 90%'
echo 'shopping 100%' >shopping.html
git add .
git commit -m 'shopping 100%'#把shopping分支合并到master分支中。
#切換到master 然后執行merge.
git checkout master
git merge shopping
[root@devops-gitlab live-lidao-app]# git merge shopping
更新 b0d7f3d..f847a2a
Fast-forwardshopping.html | 1 +1 file changed, 1 insertion(+)create mode 100644 shopping.html
[root@devops-gitlab live-lidao-app]# ll
總用量 8
-rw-r--r-- 1 root root 27 2024-05-15 11:29 index.html
-rw-r--r-- 1 root root 17 2024-05-15 11:37 shopping.html
4)git 分支命令總結
git 分支相關命令 | |
---|---|
git branch | 查看分支 |
git branch name | 創建分支 |
git branch -d name | 刪除分支 |
git checkout 分支名字 | 切換分支 |
git merge 分支名字 | 合并(吸收)分支(把指定的分支合并到當前分支中) |
git checkout -b name | 創建分支并切換到這個分支 |
4.4 入住CBD(遠程倉庫)
- 家 --> 公司 --> 代碼
- 家---->代碼
- 公共倉庫:gitee.com/github.com
- 私有倉庫:gitlab,gogs
1)創建 倉庫
2)連接遠程倉庫(用戶名密碼方式)
#添加遠程倉庫(用戶名密碼方式)
git remote add origin https://gitee.com/dws123456/lidao_live_app.git
#右邊是密鑰認證方式
git@gitee.com:dws123456/lidao_live_app.git git remote -v # 查看
#上傳本地倉庫的內容 到遠程倉庫
git push -u origin master
#需要輸入賬號的用戶名和密碼[root@devops-gitlab live-lidao-app]# git push -u origin "master"
Counting objects: 15, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (15/15), 1.28 KiB | 0 bytes/s, done.
Total 15 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:dws123456/lidao_live_app.git* [new branch] master -> master
分支 master 設置為跟蹤來自 origin 的遠程分支 master。
用戶名是啥?下圖里面的 :和/之間的就是用戶名,可以在個人設置里面更改。
https://gitee.com/dws123456/lidao_live_app.git #用戶名密碼進行訪問,上傳.
git@gitee.com:dws123456/lidao_live_app.git #配置了密鑰認證,密鑰認證進行訪問.
# 設置遠程倉庫的地址 git remote add origin git@gitee.com:dws123456/lidao_live_app.git # 推送本地倉庫的數據到遠程倉庫 git push -u origin "master"
- 拉取的代碼
[root@devops-gitlab ~]# git clone https://gitee.com/dws123456/lidao_live_app.git
正克隆到 'lidao_live_app'...
Username for 'https://gitee.com': 15733168732
Password for 'https://15733168732@gitee.com':
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 15 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (15/15), done.
[root@devops-gitlab ~]# ll
總用量 4
-rw-------. 1 root root 1354 2024-02-18 16:37 anaconda-ks.cfg
drwxr-xr-x 3 root root 57 2024-05-15 13:38 lidao_live_app
[root@devops-gitlab ~]# cd lidao_live_app/
[root@devops-gitlab lidao_live_app]# ll -a
總用量 8
drwxr-xr-x 3 root root 57 2024-05-15 13:38 .
dr-xr-x---. 5 root root 215 2024-05-15 13:38 ..
drwxr-xr-x 8 root root 163 2024-05-15 13:38 .git
-rw-r--r-- 1 root root 27 2024-05-15 13:38 index.html
-rw-r--r-- 1 root root 17 2024-05-15 13:38 shopping.html
[root@devops-gitlab lidao_live_app]# git remote -v
origin https://gitee.com/dws123456/lidao_live_app.git (fetch)
origin https://gitee.com/dws123456/lidao_live_app.git (push)
3)連接遠程倉庫(密鑰認證方式)
- 創建秘鑰對
[root@devops-gitlab live-lidao-app]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:/wgYf5XN71M955MeuftotOhgui5yW2/3bXJuF/Km8Gw root@devops-gitlab
The key's randomart image is:
+---[RSA 2048]----+
| |
| |
| |
| + |
| . S o o .|
| + . . ..+=|
| . + =. oo=B|
| . o. *.+*E+XO|
| o.++ooo+**X@|
+----[SHA256]-----+
[root@devops-gitlab lidao_live_app]# ll /root/.ssh/
總用量 12
-rw------- 1 root root 1679 2024-05-15 12:00 id_rsa
-rw-r--r-- 1 root root 400 2024-05-15 12:00 id_rsa.pub
[root@devops-gitlab live-lidao-app]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4o7LVm5RA+id+QjbFy6JtpJsfVZj25ICN13C2zNvjFIhalVAAE1DYsuvt4njgPaSmW1JfU06Lci6HV+7HI5dEfVHO0dmA1HzxHzNquge99Zn6GaLTSwrnuvwfBQnUZrWbGasny2SyyuMvBw0THflDqiH8whMcbKtsel+Fkwm1TENqtvH9dJx2G9c+leoMUFIrq9/bPwTYx0vkYggx4i2/d1hadYIGS9BEeDjNI0m5H400ZHOEMesvMcXt8x4BbLBftrQeqzxst/uKpjysGER4bsL3+5ZM/ZHwLrxyTv4dic+bqh4N2f30677HvDUaE/3faROe8X5CDfGkTGYiqGgl root@devops-gitlab
- 在遠程倉庫配置密鑰
代碼倉庫訪問方法 | 說明 |
---|---|
https | 每次連接的時候需要輸入賬號的用戶名和密碼 |
ssh | 必備常用. 密鑰認證 |
#訪問倉庫的主機上 創建密鑰對。
ssh-keygen#修改之前配置的遠程倉庫地址
git remote remove origin
git remote -v
git remote add origin git@gitee.com:dws123456/lidao_live_app.git#查詢
git remote -v
origin git@gitee.com:dws123456/lidao_live_app.git (fetch)
origin git@gitee.com:dws123456/lidao_live_app.git (push)#創建新的文件上傳到遠程倉庫測試
touch lidao.txt
git add .
git commit -m "110%"#上傳到遠程倉庫
git push -u origin master
3)倉庫管理命令小結
9.git遠程倉庫:1.配置與遠程倉庫的認證ssh√√√√√: 需要將服務器的公鑰推送到指定用戶下;https:(私有) 需要有用戶名稱,密碼,每次都需要輸入;2.添加遠程倉庫:git remote add origin [https://或者git(ssh)]刪除 git remote remove orgin 即可git remote -v3.將本地的倉庫內容,推送到遠程倉庫: git add .git commit -m "Messages"git push -u origin branch (分支名字) [ master | .... ]git pull origin branch(分支名字)4.如果有新員工加入指定倉庫的代碼都下載。git clone [https://|git]git push origin 分支名字git pull origin 分支名字
4.5 補充
1 tag
- tag標簽:給commit id設置別名,方便我們快速查看與使用,一般tag標簽表示軟件的版本.
#創建標簽
COMMITID的一個別名,COMMITID不好記憶, 標簽相對的好記憶.git tag -a "標簽名稱" -m "描述" 基于當前最新的COMMITIDgit tag -a "標簽名稱" -m "描述" commitID 指定版本打標簽
#如何上傳標簽git push origin --tagsgit push origin "標簽名稱"Master: V1.0 V2.0Dev: b1.0 b2.0git clone -b 標簽、分支 https/git[root@devops-gitlab live-lidao-app]# git tag -a v1.0 -m "new 1.0"
[root@devops-gitlab live-lidao-app]# git push origin master
Everything up-to-date
[root@devops-gitlab live-lidao-app]# git push origin --tags
Counting objects: 1, done.
Writing objects: 100% (1/1), 156 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:dws123456/lidao_live_app.git* [new tag] v1.0 -> v1.0
2 gitignore
-
上傳代碼的時候,代碼中可能包含臨時文件cache .swp 排除
-
在代碼根目錄中創建文件.gitignore 寫上要排除的內容
-
忽略上傳到本地倉庫的內容
gitignore忽略文件!!!!pyccache/*config $ cat .gitignore
#*.jpg
*.tmp
cache/*
-
應用場景:
- 程序運行時產生的垃圾文件(臨時文件)
- 程序運行時產生的緩存文件(臨時文件)
- 程序本地開發使用的圖片文件
- 程序連接數據一類的配置文件(什么時候用,什么時候配置)
-
各類語言的ignore文件
鏈接: https://github.com/github/gitignore
4.6 總結
- git clone
- git add 與 git commit
- git push
- git remote add origin
- git remote remove
- git tag 標簽
- 分支
- ci,cd,devops,devsecops
- 代碼上線流程