@@ -16,51 +16,73 @@ jobs:
1616 name : Get Environment Variables
1717 runs-on : ubuntu-latest
1818 outputs :
19- python-version : ${{ steps.load-env.outputs.python-version }}
19+ python-version : ${{ steps.load-internal-env.outputs.python-version }}
20+ github-actions : ${{steps.load-external-env.outputs.github-actions}}
2021 steps :
2122 - name : Checkout Code
2223 uses : actions/checkout@v3
2324
24- - name : Load Environment Variables
25- id : load-env
25+ - name : Load Project's Internal Environment Variables
26+ id : load-internal- env
2627 run : |
27- set -a
28+ set -x
2829 [ -f ${{ env.ENV_FILE }} ] && source ${{ env.ENV_FILE }}
29- echo "::set-output name=python-version::$PYTHON_VERSION"
30+ echo "python-version=$PYTHON_VERSION" >> $GITHUB_OUTPUT
31+ - name : Load External Environment Variables
32+ id : load-external-env
33+ run : |
34+ set -x
35+ if [ "$GITHUB_ACTIONS" = "true" ]; then
36+ echo "github-actions=$GITHUB_ACTIONS" >> $GITHUB_OUTPUT
37+ else
38+ echo "github-actions=false" >> $GITHUB_OUTPUT
39+ fi
3040
3141 env-setup :
3242 name : Setup Python and Install Dependencies
3343 runs-on : ubuntu-latest
3444 needs : env-vars
35- container : # Use a python pre-installed container instead of ubuntu-latest directly
36- image : python:${{ needs.env-vars.outputs.python-version }}-slim
3745
3846 steps :
3947 - name : Checkout Code
4048 uses : actions/checkout@v3
41- # Install becessary system dependencues on python-slim container
42- - name : Install sudo
43- run : |
44- apt-get update && apt-get install -y sudo
45- - name : Install Node
46- run : |
47- apt-get update && apt-get install -y nodejs
48- - name : Create Non-root User
49+ - name : Set up Python
50+ uses : actions/setup-python@v4
51+ with :
52+ python-version : ${{ needs.env-vars.outputs.python-version }}
53+ - name : Create Non-root User & Install dependencies # This step does not run on github actions
54+ if : ${{ needs.env-vars.outputs.github-actions == 'false' }}
4955 run : |
5056 useradd -ms /bin/bash pyrunner
5157 mkdir -p /home/pyrunner/.cache/pip
5258 chown -R pyrunner:pyrunner /home/pyrunner
5359 chmod -R 700 /home/pyrunner/.cache/pip
5460
55- - name : Install Python Dependencies
56- run : |
5761 sudo -u pyrunner bash -c "
5862 python -m venv /home/pyrunner/venv &&
5963 source /home/pyrunner/venv/bin/activate &&
6064 pip install --upgrade pip &&
6165 pip install -r requirements.txt
6266 "
63-
67+ - name : Install Python Dependencies
68+ if : ${{ needs.env-vars.outputs.github-actions == 'true' }}
69+ run : |
70+ python -m venv ~/venv &&
71+ source ~/venv/bin/activate &&
72+ pip install --upgrade pip &&
73+ pip install -r requirements.txt
74+ - name : Verify Python Version
75+ run : |
76+ source ~/venv/bin/activate
77+ ACTUAL_VERSION=$(python --version 2>&1 | sed 's/Python //')
78+ EXPECTED_VERSION="${{ needs.env-vars.outputs.python-version }}"
79+ echo "Actual: $ACTUAL_VERSION"
80+ echo "Expected: $EXPECTED_VERSION"
81+
82+ if [ "$ACTUAL_VERSION" != "$EXPECTED_VERSION" ]; then
83+ echo "ERROR : Python version mismatch: expected $EXPECTED_VERSION, got $ACTUAL_VERSION"
84+ exit 1
85+ fi
6486 - name : Save Python Environment Cache
6587 uses : actions/cache@v3
6688 with :
0 commit comments