一、搭建
1、準(zhǔn)備htpasswd.txt文件
該文件內(nèi)容包含上傳包至倉庫時驗(yàn)證的用戶名和密碼
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
#需在宿主機(jī)上提前建立好data目錄及htpasswd.txt文件
3、設(shè)置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、建立測試項(xiàng)目
# 建立項(xiàng)目目錄
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']
)
# 該文件內(nèi)容為說明性內(nèi)容,可根據(jù)自己的包的情況進(jìn)行設(shè)置
# 建立__init__.py 主程序
cat linode_example/linode_example/__init__.py
def hello_word():
print("hello world")
# 打包并上傳
python3.7 setup.py sdist # 打包,執(zhí)行完后會在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
打包注意事項(xiàng):
1、所有需要打包的項(xiàng)目在git倉庫中的目錄結(jié)構(gòu)必須一致,便于jenkinsfile自動化集成;
2、所有需要打包的項(xiàng)目的setup.py文件必須位于項(xiàng)目根目錄下;
3、python使用統(tǒng)一版本,每個項(xiàng)目的版本需要固定,便于迭代。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。