一、搭建
1、準備htpasswd.txt文件
該文件內容包含上傳包至倉庫時驗證的用戶名和密碼
pip install htpasswd
htpasswd -sc htpasswd.txt <username>
2、啟動容器
docker run --name pypiserver --restart=always -v /data/pypi/packages:/data/packages -v /root/htpasswd.txt:/data/htpasswd.txt -p 8080:8080 -d pypiserver/pypiserver -P htpasswd.txt packages
#需在宿主機上提前建立好data目錄及htpasswd.txt文件
3、設置nginx反向代理
cat /usr/local/nginx/conf/exten/pypi.conf
upstream pypi {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name pypi.local.me;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://pypi;
}
}
二、使用
1、建立測試項目
# 建立項目目錄
mkdir -p linode_example/linode_example
# 建立setup.py
cat linode_example/setup.py
from setuptools import setup
setup(
name='linode_example',
packages=['linode_example'], #上傳到倉庫后的目錄,如http://pypi.local.me/linode_example
description='Hello world enterprise edition',
version='0.1', # 版本號
url='http://github.com/example/linode_example',
author='Linode',
keywords=['pip','linode','example']
)
# 該文件內容為說明性內容,可根據自己的包的情況進行設置
# 建立__init__.py 主程序
cat linode_example/linode_example/__init__.py
def hello_word():
print("hello world")
# 打包并上傳
python3.7 setup.py sdist # 打包,執行完后會在dist目錄下有個tar包
twine upload --repository-url http://pypi.local.me dist/* # 上傳時需要輸入用戶名和密碼:admin/admin123
2、使用上傳至倉庫的包
pip install -i http://pypi.local.me --trusted-host pypi.local.me linode_example
打包注意事項:
1、所有需要打包的項目在git倉庫中的目錄結構必須一致,便于jenkinsfile自動化集成;
2、所有需要打包的項目的setup.py文件必須位于項目根目錄下;
3、python使用統一版本,每個項目的版本需要固定,便于迭代。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。