Post

GitHub Actions - Python with Custom Package Index

Aim: build python code that pulls packages from a custom pypy (Python Package Index). And pushes own packages to that private index.

Add to git repo

In the git repo, add the following:

  1. Add the file, pip.config, that contains the URLs to the private pypi index to pull packages from.
  2. Add the file, .pypirc, that contains credentials to the private pypi index where you want to push your packages to.

GH-Actions Workflow yml

In the GitHub Actions workflow, do the following:

  1. Add the environment variable PIP_CONFIG_FILE that points to a pip.config file.
  2. When pushing the python package, pass the path of .pypirc to twine to upload the package.
1
2
3
[global]
index = https://nexus.example.com/repository/my-pypi-group/pip
index-url = https://nexus.example.com/repository/my-pypip-group/simple
1
2
3
4
5
6
 [distutils]
index-servers = nexus
[nexus]
repository = https://nexus.example.com/repository/my-pypip-custom-packages
username = ${NEXUS_TOKEN_USERNAME}
password = ${NEXUS_TOKEN_PASSCODE}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
name: hello-python
on:
  workflow_dispatch:
env:
  PIP_CONFIG_FILE: pip.config
  # for pushing to custom artifact store like Artifactor or Nexus
  PYPIRC_FILE: .pypirc
   
jobs:
  jobA:
    name: jobA
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        
      - name: Setup Python 3.11
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'
          
      - name: Check Python
        run: |
          python3 --version
          pip config list -v
        
      - name: Install packages
        run: |
          pip install -r requirements.txt
      - name: Run
        run: |
          python3 path/to/script.py
      
      - name: Push Python Packages
        run: |
          sudo apt-get install -y gettext-base
          envsubst < $PYPIRC_FILE
          pip install twine
          twine upload \
            --config-file $PYPIRC_FILE \
            --repository nexus --non-interactive \
            myPackage-0.0.1-py3-none-any.whl"
This post is licensed under CC BY 4.0 by the author.