ToB企服应用市场:ToB评测及商务社交产业平台

标题: python在容器内克隆拉取git私有仓库 [打印本页]

作者: 锦通    时间: 2024-3-17 04:29
标题: python在容器内克隆拉取git私有仓库
前言

目前有个python应用需要在容器镜像内拉取git私有仓库的代码,一开始的想法是用GitPython,折腾一番ssh私钥和known_hosts问题后,发现还是在镜像中封装个git最省事,然后用subprocess调用系统命令,镜像体积也没有想象中增加特别多。
准备ssh私钥和known_hosts文件

应用内通过repo的ssh url克隆和拉取仓库,所以ssh私钥和known_hosts要封装到镜像中。
  1. ssh-keygen -t ed25519
复制代码
  1. gitee.com ssh-ed25519 AxxxxxxxxxxxxxxxxxxxxN
复制代码
在项目目录中创建一个名为.ssh的目录,然后把id_ed25519和known_hosts文件拷贝到这个目录下,并修改文件权限为600。这个目录待会需要封装到镜像中。
  1. chmod 600 id_ed25519 known_hosts
复制代码
编写python代码

这里只是个demo,拉取私有仓库的代码到本地,然后拷贝出需要的目录或文件。注意代码里面用的都是容器内路径。
  1. import subprocess
  2. import os
  3. import shutil
  4. repo_url = "git@gitee.com:zhangsan/scout.git"
  5. repo_dir = "/tmp/scout"
  6. def repo_clone():
  7.     cmd = f"git clone --depth=1 --single-branch {repo_url} {repo_dir}"
  8.     if os.path.exists(repo_dir):
  9.         print(f"{repo_dir} has exist")
  10.         return
  11.     runcmd(cmd)
  12. def repo_pull():
  13.     cmd = f"cd {repo_dir};git pull"
  14.     runcmd(cmd)
  15.     if not os.path.exists(f"{repo_dir}/prod"):
  16.         print(f"{repo_dir}/prod is not exist")
  17.         return
  18.     dest_path = "/home/zhangsan/app/prod"
  19.     if not os.path.exists(dest_path):
  20.         os.makedirs(dest_path)
  21.     shutil.copytree(f"{repo_dir}/prod", dest_path, dirs_exist_ok=True)
  22. def runcmd(command):
  23.     ret = subprocess.run(
  24.         command,
  25.         shell=True,
  26.         stdout=subprocess.PIPE,
  27.         stderr=subprocess.STDOUT,
  28.         encoding="utf-8",
  29.         timeout=10,
  30.         )
  31.    
  32.     if ret.returncode == 0:
  33.         print("success")
  34.         print(ret.stdout)
  35.     else:
  36.         print(f"fail code: {ret.returncode}")
  37.         print(ret.stdout)
  38. if __name__ == "__main__":
  39.     repo_clone()
  40.     repo_pull()
复制代码
Dockerfile

目录层级如下
  1. .
  2. ├── app
  3. │   └── demo.py
  4. ├── Dockerfile
  5. └── .ssh
  6.     ├── id_ed25519
  7.     └── known_hosts
复制代码
编写Dockerfile文件
  1. FROM python:3.8-alpine
  2. # 1. 修改apline镜像源
  3. # 2. 安装git和ssh客户端并删除apk缓存
  4. # 3. 创建普通用户及其用户组
  5. RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \
  6.         && apk add --no-cache git openssh \
  7.         && rm -rf /tmp/* /root/.cache /var/cache/apk/* \
  8.         && addgroup -g 1010 zhangsan \
  9.         && adduser -s /bin/sh -G zhangsan -u 10101 -h /home/zhangsan zhangsan -D
  10. # 将相关文件添加到镜像中
  11. ADD --chown=zhangsan:zhangsan .ssh /home/zhangsan/.ssh
  12. ADD --chown=zhangsan:zhangsan app /home/zhangsan/app
  13. # 指定运行用户, 工作目录和启动命令
  14. USER zhangsan
  15. WORKDIR /home/zhangsan/app
  16. CMD python3 demo.py
复制代码
打包docker镜像
  1. docker build -t pygit:0.0.1 .
复制代码
测试,创建一个临时容器
  1. docker run -it --rm --name pygit pygit:0.0.1 sh
复制代码
在测试容器内测试能否正常执行
  1. python3 demo.py
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4