三、模拟开发流程 (推送代码到代码仓库)

1. 将代码推送到 GitLab

  1. 准备代码和 Dockerfile

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 在一台有git工具的机器上操作
    [root@harbor250 ~]# wget http://192.168.21.253/Resources/Kubernetes/Project/DevOps/Jenkins/oldboyedu-yiliao.zip
    [root@harbor250 ~]# mkdir code && unzip oldboyedu-yiliao.zip -d code/
    [root@harbor250 ~]# cd code/
    # 编写Dockerfile
    [root@harbor250 code]# cat > Dockerfile <<EOF
    FROM harbor250.oldboyedu.com/oldboyedu-xiuxian/apps:v1
    MAINTAINER Jason Yin
    LABEL school=oldboyedu class=linux98
    ADD . /usr/share/nginx/html
    EXPOSE 80
    WORKDIR /usr/share/nginx/html
    EOF
  2. GitLab 创建项目

    • 登录 GitLab,点击 Projects -> Create a project。

    [此处插入图片9: GitLab主面板,红框标识了左侧的 “Projects” 和中间的 “Create a project”]

    • 选择 Create blank project。
    • dba023d5e9039cd60195d5f8a493b677

    [此处插入图片10: GitLab新建项目类型选择页面,箭头指向 “Create blank project”]

    • 填写项目信息,如项目名称 oldboyedu-yiliao,并勾选 Initialize repository with a README。
    • 4c4cb3647dce5a5ee741fa504da480ec

    [此处插入图片11: GitLab创建空白项目表单,红框标识了项目名称、可见性级别、初始化选项和 “Create project” 按钮]

    218d9c6eafdc0e3166ea58ee3b6ee67a

  3. 推送代码到 GitLab

    1
    2
    3
    4
    5
    6
    [root@harbor250 code]# git init .
    [root@harbor250 code]# git remote add origin http://10.0.0.153/root/oldboyedu-yiliao.git
    [root@harbor250 code]# git add .
    [root@harbor250 code]# git commit -m 'k8s yiliao demo'
    [root@harbor250 code]# git push -u origin master
    # 输入GitLab的用户名root和密码
  4. 验证

    • 刷新 GitLab 项目页面,确认代码和 Dockerfile 已成功推送。

    [此处插入图片12: GitLab仓库文件列表,红框标识了master分支和Dockerfile文件,证明代码推送成功]

    ddc4f2c04280cf11589922d542b0c0ce

2. (可选) 将代码推送到 Gitee

流程与 GitLab 类似,这里简要记录。

  1. Gitee 创建仓库

    [此处插入图片13: Gitee页面,数字标示了点击顺序:1.右上角’+’号 -> 2.新建仓库]

    dfcc5a0a94271a332aa3002c532f6a83

    [此处插入图片14: Gitee新建仓库表单,红框标识了仓库名称、设为开源、初始化仓库和 “创建” 按钮]

    65c51b5530bbf0b0ceb83d1217d1f475

  2. 添加 Gitee 远程仓库并推送

    1
    2
    3
    4
    # 在代码目录中
    [root@harbor250 code]# git remote add gitee https://gitee.com/yinzhengjie/oldboyedu-yiliao.git
    [root@harbor250 code]# git push gitee master
    # 输入Gitee的用户名和密码
  3. 验证

    • 刷新 Gitee 仓库页面,确认代码已推送。

    [此处插入图片15: Gitee仓库文件列表,显示了完整的项目文件,证明推送成功]

    b477e3a53c9af367757767aa9f75d58a