经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » Git » 查看文章
python在容器内克隆拉取git私有仓库
来源:cnblogs  作者:花酒锄作田  时间:2024/1/3 9:16:08  对本文有异议

前言

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

准备ssh私钥和known_hosts文件

应用内通过repo的ssh url克隆和拉取仓库,所以ssh私钥和known_hosts要封装到镜像中。

  1. 生成ssh密钥文件。一般来说提示输入直接回车即可。生成的$HOME/.ssh/id_ed25519为私钥文件,是需要拷贝到镜像中的。$HOME/.ssh/id_ed25519.pub为公钥文件,文件内容需要添加到远程仓库的ssh密钥配置中。
  1. ssh-keygen -t ed25519
  1. 准备known_hosts文件,文件内容可以从其它主机拷贝一份。其实ssh密钥文件也可以从其它主机拷贝,只要对应的公钥在git远程仓库的ssh配置中即可。known_hosts文件内容示例。
  1. gitee.com ssh-ed25519 AxxxxxxxxxxxxxxxxxxxxN

在项目目录中创建一个名为.ssh的目录,然后把id_ed25519known_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. if ret.returncode == 0:
  32. print("success")
  33. print(ret.stdout)
  34. else:
  35. print(f"fail code: {ret.returncode}")
  36. print(ret.stdout)
  37. if __name__ == "__main__":
  38. repo_clone()
  39. 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 && apk add --no-cache git openssh && rm -rf /tmp/* /root/.cache /var/cache/apk/* && addgroup -g 1010 zhangsan && adduser -s /bin/sh -G zhangsan -u 10101 -h /home/zhangsan zhangsan -D
  6. # 将相关文件添加到镜像中
  7. ADD --chown=zhangsan:zhangsan .ssh /home/zhangsan/.ssh
  8. ADD --chown=zhangsan:zhangsan app /home/zhangsan/app
  9. # 指定运行用户, 工作目录和启动命令
  10. USER zhangsan
  11. WORKDIR /home/zhangsan/app
  12. 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

原文链接:https://www.cnblogs.com/XY-Heruo/p/17941013

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号