Introduction#
How should we differentiate between "notes" and "operation manuals"?
Main Content#
There are some projects that use Github Actions to run Python, and one of the solutions is to use Docker + pipenv. The latter is a package management tool for Python, similar to npm.
This article will document how to use this solution in a self-built Linux environment...
# Clone the project
GIT_PATH=~/Git
git clone [email protected]:VaultVulp/action-pipenv.git $GIT_PATH/action-pipenv
# Build the Docker image
cd $GIT_PATH/action-pipenv
docker build -t py-pipenv .
· Execute mkdir -p ~/test/py-pipenv
to create a test directory, and then create a main.py
file in that directory with the following content:
import json
import feedparser
import pprint
# Read RSS
rss_list = feedparser.parse('https://www.wdssmq.com/feed.php')
# Extract titles and links
rlt_list = [{'title': entry['title'], 'link':entry['link']} for entry in rss_list['entries']]
# Print the results
pprint.pprint(rlt_list)
# Save as a JSON file
with open('output.json', 'w') as f:
json.dump(rlt_list, f)
· Create a Pipfile
file in that directory with the following content:
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[scripts]
main = "python main.py"
[packages]
feedparser = "*"
# Run to build the virtual environment (virtualenv)
PY_DIR=~/test/py-pipenv
ENVS_DIR=${PY_DIR}/virtualenvs
docker run --name py-run \
--rm \
--workdir /app \
-v ${PY_DIR}:/app \
-v ${ENVS_DIR}/:/root/.local/share/virtualenvs \
py-pipenv \
"install -d"
· A virtualenvs
directory will be generated inside ${PY_DIR}
, which contains the relevant files for the virtual environment. If the dependencies in Pipfile remain unchanged, the build only needs to be executed once.
# Run the actual code
PY_DIR=~/test/py-pipenv
ENVS_DIR=${PY_DIR}/virtualenvs
docker run --name py-run \
--rm \
--workdir /app \
-v ${PY_DIR}:/app \
-v ${ENVS_DIR}/:/root/.local/share/virtualenvs \
py-pipenv \
"run main"
· This is equivalent to executing pipenv run main
, and an output.json
file will be generated inside ${PY_DIR}
.
Additional Information#
- The
--rm
parameter indicates that the container will be automatically deleted after running. --workdir
specifies the working directory.-v
mounts a directory into the container.py-pipenv
is the name specified when building the image."install -d"
or"run main"
will be passed as arguments to thepipenv
command inside the container.
Related Links#
"Minor" update to Python Docker image_Miscellaneous_Floating Ice and Sinking Water:
VaultVulp/action-pipenv: Use pipenv commands in your GitHub Actions Workflow: