forked from getsentry/action-setup-venv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
90 lines (79 loc) · 3.59 KB
/
action.yml
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
name: 'venv setup'
description: 'configures venv, python and caches'
inputs:
python-version:
description: "Version range or exact version of Python or PyPy to use, using SemVer's version range syntax."
cache-dependency-path:
description: "Requirement files to install. Can be a glob pattern."
default: '**/requirements*.txt'
venv-dir:
default: '.venv'
install-cmd:
description: 'Command to install python dependencies.'
outputs:
cache-hit:
description: "A boolean value to indicate if a cache was restored"
value: ${{ steps.cache-venv.outputs.cache-hit }}
runs:
using: 'composite'
steps:
- uses: thebrowsercompany/check-python-exists@6334b969448dd1526acacf8ba093bfbd3c5dce64
id: setup-python
with:
python-version: ${{ inputs.python-version }}
is-self-hosted: ${{ !startsWith(runner.name, 'GitHub Actions') }}
- name: Check venv
id: check-venv
shell: bash
run: |
# Will either print the directory to the venv, or `None` if it is not set.
VENV=$("${{ steps.setup-python.outputs.python-path }}" -c "import os; print(os.environ.get('VIRTUAL_ENV'))")
if [ "$VENV" = "None" ]; then
echo "No venv detected. Setting up venv."
echo "already_venv=false" >> "$GITHUB_OUTPUT"
else
echo "Python venv already detected. Skipping the rest of venv setup."
echo "already_venv=true" >> "$GITHUB_OUTPUT"
fi
- name: SHA the install command
if: steps.check-venv.outputs.already_venv == 'false'
id: install-cmd-sha
shell: bash
# macOS doesn't include shasum256 and Windows doesn't include shasum, so
# use python.
run: |
CMD_HASH="$(echo '${{ inputs.install-cmd }}' | python3 -c 'import hashlib,sys;print(hashlib.sha256(sys.stdin.buffer.read()).hexdigest())')"
echo -n "install-cmd-sha=$CMD_HASH" >> $GITHUB_OUTPUT
- uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0
if: steps.check-venv.outputs.already_venv == 'false'
id: cache-venv
with:
path: ${{ inputs.venv-dir }}
key: setup-venv-${{ runner.os }}-py-${{ steps.setup-python.outputs.python-version }}-${{ steps.setup-python.outputs.python-path }}-${{ hashFiles(inputs.cache-dependency-path) }}-${{ steps.install-cmd-sha.outputs.install-cmd-sha }}
- name: Create venv
# cache-hit is a weird tribool string: https://github.com/actions/cache/issues/1466
if: steps.cache-venv.outputs.cache-hit != 'true' && steps.check-venv.outputs.already_venv == 'false'
shell: bash
run: |
'${{ steps.setup-python.outputs.python-path }}' -m venv ${{ inputs.venv-dir }}
- name: venv save env (posix)
if: runner.os != 'Windows' && steps.check-venv.outputs.already_venv == 'false'
shell: bash
run: |
source ${{ inputs.venv-dir }}/bin/activate
echo "VIRTUAL_ENV=${VIRTUAL_ENV}" >> $GITHUB_ENV
echo "${VIRTUAL_ENV}/bin" >> $GITHUB_PATH
- name: venv save env (windows)
if: runner.os == 'Windows' && steps.check-venv.outputs.already_venv == 'false'
shell: pwsh
run: |
${{ inputs.venv-dir }}\Scripts\Activate.ps1
echo "$Env:VIRTUAL_ENV"
echo "VIRTUAL_ENV=$Env:VIRTUAL_ENV" >> $Env:GITHUB_ENV
echo "$Env:VIRTUAL_ENV\Scripts" >> $Env:GITHUB_PATH
- run: echo $PATH
if: steps.check-venv.outputs.already_venv == 'false'
shell: bash
- run: ${{ inputs.install-cmd }}
if: inputs.install-cmd != '' && steps.cache-venv.outputs.cache-hit != 'true' && steps.check-venv.outputs.already_venv == 'false'
shell: bash