TestPyPiPyPi via marcaçãoEste é um exemplo de funcionamento que usa um fluxo de trabalho do GitHub Ações CI/CD para testar, criar e fazer upload de um pacote Python para testpi e Pypi.
Criei este pacote de exemplo trabalhando nesses guias;
O restante do ReadMe descreve para configurar um novo projeto da mesma maneira.
Quando configurado;
pip install example-package-grumbit Em um nível alto, o processo de configuração da embalagem do projeto Github CI/CD, incluindo pytesting, se parece com isso;
./.github/workflows/publish-to-test-pypi.yml por enquanto../.github/workflows/publish-to-test-pypi.yml e obtenha o github CI/CD cd < pacakges directory >
python3 -m venv .venv # Create the venv if it doesn't exist yet
source .venv/bin/activate
python3 -m pip install --upgrade pip setuptools wheel pip-tools pytest # Install the tools needed for the build tool
python3 -m pip install --upgrade build # Install the build tool itself
python3 -m build # build the packagepython3 -m pip install --upgrade twine # Install the twine upload tool
python3 -m twine upload --repository testpypi dist/ * # Upload to TestPyPi
# When prompted, the username is __token__ and the password is the TestPyPi global scope API tokenDepois de carregar o pacote, um token de API específico do pacote deve ser configurado e salvo no testpypi
Verifique se o pacote pode ser baixado e usado em um novo venv ;
cd < some new tmp directory >
python3 -m venv .venv
source .venv/bin/activate
package_name= " example-package-grumBit "
python3 -m pip install --index-url https://test.pypi.org/simple/ --pre ${package_name} # Check the package can be installed
python3 -c " from example_package_grumbit import example; print(example.add_one(1)) " # Check package functionspython3 -m twine upload dist/ * # Upload to PyPi
# When prompted, the username is __token__ and the password is the PyPi global scope API token[project] do ./pyProject.toml, então ele precisa ser reconstruído e carregado; vs ./pyproject.toml
python3 -m build # build the package
python3 -m twine check dist/ * # check the package can be uploaded
python3 -m twine upload --repository testpypi dist/ * # test uploading using TestPyPi
python3 -m twine upload dist/ * # Upload to PyPi
cd " <the project's directory> "
repo_name= " <the new repo's name> "
gh repo create " ${repo_name} " --private
git init
git add --all
git commit -m " init "
git branch -M master
git remote add origin [email protected]:grumBit/ ${repo_name} .git
git push -u origin master Se a filial padrão não for master , altere-a no github ou altere .github/workflows/publish-to-test-pypi.yml .
Adicione os tokens TestPypi e Pypi API ao repo
Abra o repositório no Github usando gh browse . No navegador, clique em Settings -> Secrets -> Actions . Em seguida, adicione dois novos segredos chamados PYPI_API_TOKEN e TEST_PYPI_API_TOKEN , com os tokens da API criados após o upload dos pacotes acima
Crie e configure .github/workflows/publicish to test-pypi.yml Workflow Definição
publish-to-test-pypi.yml deste pacote deste pacote já possui as peças necessárias para o teste automático incluído (veja abaixo)TestPyPimaster , o Github CI/CD será executado.TestPyPiPyPi via marcaçãoColocar uma etiqueta em uma confirmação e empurrá -la fará com que o Github CI/CD seja executado e crie uma liberação do PyPI.
Use o seguinte para marcar o último comprometimento (ou HEAD ) com a versão atualmente configurada em ./pyproject.toml ;
version_tag=v $( cat ./pyproject.toml | egrep " ^version " | cut -d ' " ' -f2 )
version_tag_info= " Some release info "
git tag -a " ${version_tag} " -m " ${version_tag_info} "
git push --tagversion_tag= " vX.X.X "
version_tag_info= " Some release info "
commit_sha= " 16fb0fd "
git tag -a " ${version_tag} " " ${commit_sha} " -m " ${version_tag_info} "
git push --tag - name : Install requirements
run : >-
python -m
pip install
--requirement requirements.txt
- name : Run tests
run : >-
python -m
pytest__init__.py necessário para adicionar ao src/ e aos tests/ diretório como este;test/ pastas na src/ árvore. ./pyproject.toml pode ser configurado para que test/ pastas incorporado seja excluído, mas eu fui com o "padrão" por enquanto. packaging_tutorial/
├── src/
│ ├── __init__.py
│ └── example_package_grumbit/
│ ├── __init__.py
│ └── example.py
└── tests/
├── __init__.py
└── example_package_grumbit/
├── __init__.py
└── test_example.py