Skip to content

Instantly share code, notes, and snippets.

@adrienbernede
Created October 4, 2022 19:56
Show Gist options
  • Save adrienbernede/124b7e1d4b291ea193b564ab965fcbc4 to your computer and use it in GitHub Desktop.
Save adrienbernede/124b7e1d4b291ea193b564ab965fcbc4 to your computer and use it in GitHub Desktop.
A minimal example of cached python environment in GitLab CI
##############################################################################
# Copyright (c) 2022, Lawrence Livermore National Security, LLC and RADIUSS
# project contributors. See the COPYRIGHT file for details.
#
# SPDX-License-Identifier: (MIT)
##############################################################################
variables:
PYTHON_BIN_DIR: /usr/tce/packages/python/python-3.8.2/bin
PYTHON_ENVIRONMENT_PATH: ${CI_PROJECT_DIR}/.venv
PIP_CACHE_DIR: ${CI_PROJECT_DIR}/.cache/pip
stages:
- env
- run
.on-ruby:
tags:
- shell
- ruby
# This jobs creates a python env and caches it.
# Note: The cache is updated automatically when the requirements.txt changes.
# Cache can be manually cleared by clicking "CI/CD - Pipelines - Clear Runner Caches".
configure_python:
extends: [.on-ruby]
stage: env
script:
- ${PYTHON_BIN_DIR}/virtualenv -p ${PYTHON_BIN_DIR}/python3 ${PYTHON_ENVIRONMENT_PATH}
- . ${PYTHON_ENVIRONMENT_PATH}/bin/activate
- pip install -r ./requirements.txt
cache:
key:
files:
- requirements.txt
paths:
- ${PYTHON_ENVIRONMENT_PATH}
- ${PIP_CACHE_DIR}
.venv-readonly:
cache:
key:
files:
- requirements.txt
paths:
- ${PYTHON_ENVIRONMENT_PATH}
- ${PIP_CACHE_DIR}
policy: pull
job-using-cache-readonly:
extends: [.venv-readonly, .on-ruby]
stage: run
script:
- which python3
- python3 --version
- pip freeze
- . ${PYTHON_ENVIRONMENT_PATH}/bin/activate
- which python3
- python3 --version
- pip freeze
#requirements.txt (example)
# ###### Requirements without Version Specifiers ######
# lxml
#
# ###### Requirements with Version Specifiers ######
# # foo == 0.6.1 # Version Matching. Must be version 0.6.1
# # bar >= 4.1.1 # Minimum version 4.1.1
# # xxx != 3.5 # Version Exclusion. Anything except version 3.5
# # yyy ~= 1.1 # Compatible release. Same as >= 1.1, == 1.*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment