I have a private Python package but I want to install pipenv on GitHub Actions and build a Docker Image

Introduction

Share your knowledge of building Docker Images on GitHub Actions in a Python project that uses Python packages in a private repository on GitHub.

Version / environment

manner

Check Pipfile.lock

Use pipenv.

Make sure that Pipfile.lock is set to install the private repository as shown below.

Pipfile.Part of lock


        "private-repository-package": {
            "editable": true,
            "git": "https://github.com/username/private-repository-package.git",
            "ref": "812u4dd76db6298ce50b5569a9a1d80759ba6e80"
        },

This time, it will be installed using netrc, so install it with https. You can install the version you want to specify by setting ref = commit hash.

If you haven't already installed it, install it locally like pipenv install private-repository-package, modify the ref and re-install pipenv install to update it.

See the official documentation for more details. pipenv official document

Dockerfile

The contents of the Dockerfile.

I'm using a -slim image. -You can reduce the size of the image by about 1/3 compared to the non-slim image.

Dockerfile


FROM python:3.8.2-slim

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    ca-certificates \
    git \
    && apt-get clean && \
    rm -rf /var/lib/apt/lists/*

ENV WORKDIR /app/
WORKDIR ${WORKDIR}
COPY Pipfile Pipfile.lock ${WORKDIR}

# setup .netrc to install dependencies in private GitHub repository
ARG PRIVATE_REPO_ACCESS_KEY
RUN echo "machine github.com" > /root/.netrc && \
    echo "login $PRIVATE_REPO_ACCESS_KEY" >> /root/.netrc && \
    echo "passowrd x-oauth-basic" >> /root/.netrc && \
    chmod 600 /root/.netrc

# install Python dependencies
RUN pip install pipenv --no-cache-dir && \
    pipenv install --system --deploy && \
    pip uninstall -y pipenv virtualenv-clone virtualenv

# remove anythings security and uncessary
RUN rm /root/.netrc && \
    rm Pipfile Pipfile.lock

COPY src/ $WORKDIR

CMD python ./run.py

The point is that it uses netrc. It takes the argument PRIVATE_REPO_ACCESS_KEY from the outside (this time GitHub Actions) and passes it to the /root/.netrc file to allow https access to the private repository on GitHub.

Then, using this .netrc file, after pipenv install is completed,

# remove anythings security and uncessary
RUN rm /root/.netrc && \
    rm Pipfile Pipfile.lock

By including this process, the value of PRIVATE_REPO_ACCESS_KEY will not be erased and left in Docker Image.

GitHub Actions

Get a GitHub personal token and set it to Secret

Get a personal token to access your private repository on GitHub. The method is easy to understand this official document ..

Then, register the obtained personal token in Secret from Setting of the target GitHub repository. This method is also shown in other articles with figures. The official documentation is here.

GitHub Actions Yaml

Obtain the personal token set above from the secret and pass it to the docker command as PRIVATE_REPO_ACCESS_KEY in the Dockerfile earlier to perform the Image build process.

Part of GitHubActions Workflow Yaml


      env:
        PRIVATE_REPO_ACCESS_KEY: ${{ secrets.PRIVATE_REPO_ACCESS_KEY }}
      run: |
        docker build --build-arg PRIVATE_REPO_ACCESS_KEY=${PRIVATE_REPO_ACCESS_KEY} .

reference

Recommended Posts

I have a private Python package but I want to install pipenv on GitHub Actions and build a Docker Image
Build a Docker image containing the private repository Python library on GitHub Actions
I want to build a Python environment
I want to find a popular package on PyPi
How to build a Django (python) environment on docker
I want to install a package of Php Redis
How to build a beautiful Python environment on a new Mac and install Jupter Notebook
I want to install a package from requirements.txt with poetry
Steps to create a Job that pulls a Docker image and tests it with Github Actions
I want to know if you install Python on Mac ・ Iroha
I want to create a pipfile and reflect it in docker
I want to tweet on Twitter with Python, but I'm addicted to it
[Python] Create a linebot to write a name and age on an image
I tried to install Docker on Windows 10 Home but it didn't work
I want to format and check Python code to my liking on VS Code
Build Linux on a Windows environment. Steps to install Laradock and migrate
I want to make a web application using React and Python flask
I want to pass an argument to a python function and execute it from PHP on a web server
I want to use Python in the environment of pyenv + pipenv on Windows 10
I just want to add scipy, but it's a messy note. Ubuntu, Python 3.
I want to drop a file on tkinter and get its path [Tkinter DnD2]
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
I want to exe and distribute a program that resizes images Python3 + pyinstaller
[Linux] You do not have root privileges. But I want to yum install.
Build a command line app in Python to understand setup.py, argparse, GitHub Actions
I want to create a window in Python
I want to make a game with Python
I want to do pyenv + pipenv on Windows
I want to write to a file with Python
I want to take a screenshot of the site on Docker using any font
I want to write a triple loop and conditional branch in one line in python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
Install Python3 on Mac and build environment [Definitive Edition]
100 image processing knocks !! (021-030) I want to take a break ...
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to easily build a model-based development environment
I want to work with a robot in python.
[Go + Gin] I tried to build a Docker environment
[Python] I want to make a nested list a tuple
I want to AWS Lambda with Python on Mac!
How to build a Python environment on amazon linux 2
I want to run a quantum computer with Python
I tried to install scrapy on Anaconda and couldn't
It is difficult to install a green screen, so I cut out only the face and superimposed it on the background image
How to build a Python virtual execution environment using Visual Studio Code and pipenv on a Windows machine (also Jupyter notebook)
I want to cut out only the face from a person image with Python and save it ~ Face detection and trimming with face_recognition ~
[Linux] How to install a package on a server that does not have a network environment (standalone)
When I tried to build a Rails environment on WSL2 (Ubuntu 20.04LTS), I stumbled and fell.
I want to set up a GUI development environment with Python or Golang on Mac
I want to absorb the difference between the for statement on the Python + numpy matrix and the Julia for statement
I want to create a karaoke sound source by separating instruments and vocals using Python
I want to make a voice changer using Python and SPTK with reference to a famous site
Install Python 3.6 on Docker
Install LAMP on Amazon Linux 2 and build a WordPress environment.
How to build a new python virtual environment on Ubuntu
I made a package to filter time series with python
I want to start a lot of processes from python