|
| 1 | +specificationVersion: 'jobtemplate-2023-09' |
| 2 | +name: Pip Self-Contained Job |
| 3 | + |
| 4 | +description: | |
| 5 | + Demonstrates managing pip packages entirely within a job bundle, without |
| 6 | + relying on a queue environment. The job defines its own Open Job Description |
| 7 | + job environment that creates a Python virtual environment, installs the |
| 8 | + requested pip packages into it, and adds it to the PATH for all steps. |
| 9 | +
|
| 10 | + Use this style when you want a self-contained job bundle that runs on any |
| 11 | + Linux worker with python3 available, and you do not want to configure a |
| 12 | + queue environment. If you would rather share one pip environment definition |
| 13 | + across many jobs, see the pip_package_job sample and the |
| 14 | + queue_environments/pip_queue_env.yaml queue environment instead. |
| 15 | +
|
| 16 | +parameterDefinitions: |
| 17 | +- name: Message |
| 18 | + type: STRING |
| 19 | + default: "Hello from a self-contained pip virtual environment!" |
| 20 | + description: "The message the cowsay package prints." |
| 21 | + userInterface: |
| 22 | + control: LINE_EDIT |
| 23 | + label: Message |
| 24 | +- name: PipPackages |
| 25 | + type: STRING |
| 26 | + default: "cowsay" |
| 27 | + description: "A space-separated list of pip requirement specifiers to install into the job's virtual environment." |
| 28 | + userInterface: |
| 29 | + control: LINE_EDIT |
| 30 | + label: Pip Packages |
| 31 | + groupLabel: Software Environment |
| 32 | +- name: PipIndexUrl |
| 33 | + type: STRING |
| 34 | + default: "" |
| 35 | + description: "The base Python package index URL. Leave empty to use the default PyPI index." |
| 36 | + userInterface: |
| 37 | + control: LINE_EDIT |
| 38 | + label: Pip Index URL |
| 39 | + groupLabel: Software Environment |
| 40 | + |
| 41 | +jobEnvironments: |
| 42 | +- name: PipVirtualEnv |
| 43 | + description: Creates a Python virtual environment and installs the PipPackages into it. |
| 44 | + script: |
| 45 | + actions: |
| 46 | + onEnter: |
| 47 | + command: bash |
| 48 | + args: |
| 49 | + - "{{Env.File.Enter}}" |
| 50 | + embeddedFiles: |
| 51 | + - name: Enter |
| 52 | + type: TEXT |
| 53 | + data: | |
| 54 | + #!/bin/env bash |
| 55 | + set -euo pipefail |
| 56 | +
|
| 57 | + if [ -z '{{Param.PipPackages}}' ]; then |
| 58 | + echo "Skipping pip virtual environment as PipPackages parameter was empty." |
| 59 | + exit 0 |
| 60 | + fi |
| 61 | +
|
| 62 | + PIP_PACKAGES='{{Param.PipPackages}}' |
| 63 | +
|
| 64 | + INDEX_OPTS="" |
| 65 | + if [ -n '{{Param.PipIndexUrl}}' ]; then |
| 66 | + INDEX_OPTS="--index-url {{Param.PipIndexUrl}}" |
| 67 | + fi |
| 68 | +
|
| 69 | + # Find a Python interpreter. Prefer python3, but fall back to python. |
| 70 | + if command -v python3 > /dev/null 2>&1; then |
| 71 | + PYTHON=python3 |
| 72 | + else |
| 73 | + PYTHON=python |
| 74 | + fi |
| 75 | +
|
| 76 | + echo "Creating a Python virtual environment in the session directory..." |
| 77 | +
|
| 78 | + # Create the virtual environment in the session working directory so it |
| 79 | + # is cleaned up automatically when the session ends. |
| 80 | + ENV_DIR="$(mktemp -d '{{Session.WorkingDirectory}}/.venv-XXXXX')" |
| 81 | + "$PYTHON" -m venv "$ENV_DIR" |
| 82 | +
|
| 83 | + # A venv puts its executables in bin/ on Linux and macOS, and in |
| 84 | + # Scripts/ on Windows. |
| 85 | + if [ -d "$ENV_DIR/bin" ]; then |
| 86 | + VENV_BIN="$ENV_DIR/bin" |
| 87 | + else |
| 88 | + VENV_BIN="$ENV_DIR/Scripts" |
| 89 | + fi |
| 90 | +
|
| 91 | + # Capture the environment variables before activating the virtual environment |
| 92 | + "$PYTHON" '{{Env.File.OpenJDVarsStart}}' .vars |
| 93 | +
|
| 94 | + # Activate the virtual environment. |
| 95 | + set +u |
| 96 | + source "$VENV_BIN/activate" |
| 97 | + set -u |
| 98 | +
|
| 99 | + # Pass the index options to both commands so a private PipIndexUrl also |
| 100 | + # works on workers that cannot reach public PyPI. |
| 101 | + pip install $INDEX_OPTS --quiet --upgrade pip |
| 102 | + pip install $INDEX_OPTS $PIP_PACKAGES |
| 103 | +
|
| 104 | + # Export the environment variables set by activation to all steps |
| 105 | + "$VENV_BIN/python" '{{Env.File.OpenJDVarsCapture}}' .vars |
| 106 | +
|
| 107 | + echo "Virtual environment created at $ENV_DIR" |
| 108 | + pip list |
| 109 | + - name: OpenJDVarsStart |
| 110 | + filename: openjd-vars-start.py |
| 111 | + type: TEXT |
| 112 | + data: | |
| 113 | + import json |
| 114 | + import os |
| 115 | + import sys |
| 116 | +
|
| 117 | + # Exclude the env var "_" as it has special meaning to shells |
| 118 | + before = dict(os.environ) |
| 119 | + if "_" in before: |
| 120 | + del before["_"] |
| 121 | +
|
| 122 | + with open(sys.argv[1], "w", encoding="utf8") as f: |
| 123 | + json.dump(before, f) |
| 124 | + - name: OpenJDVarsCapture |
| 125 | + filename: openjd-vars-capture.py |
| 126 | + type: TEXT |
| 127 | + data: | |
| 128 | + import json |
| 129 | + import os |
| 130 | + import sys |
| 131 | +
|
| 132 | + # Get the snapshot from `openjd-vars-start.py`, and the current environment state. |
| 133 | + with open(sys.argv[1], "r", encoding="utf8") as f: |
| 134 | + before = json.load(f) |
| 135 | + after = dict(os.environ) |
| 136 | + # Exclude the env var "_" as it has special meaning to shells |
| 137 | + if "_" in after: |
| 138 | + del after["_"] |
| 139 | +
|
| 140 | + # Identify the modified and deleted environment variables |
| 141 | + vars_to_put = {k: v for k, v in after.items() if v != before.get(k)} |
| 142 | + vars_to_delete = {k for k in before if k not in after} |
| 143 | +
|
| 144 | + # Print the env var changes following the Open Job Description specification |
| 145 | + for k, v in vars_to_put.items(): |
| 146 | + kv = json.dumps(f"{k}={v}", ensure_ascii=True) |
| 147 | + print(f"openjd_env: {kv}") |
| 148 | + for k in vars_to_delete: |
| 149 | + print(f"openjd_unset_env: {k}") |
| 150 | +
|
| 151 | +steps: |
| 152 | +- name: SayHello |
| 153 | + hostRequirements: |
| 154 | + attributes: |
| 155 | + - name: attr.worker.os.family |
| 156 | + anyOf: ["linux"] |
| 157 | + script: |
| 158 | + actions: |
| 159 | + onRun: |
| 160 | + command: python |
| 161 | + args: ['{{Task.File.Run}}'] |
| 162 | + embeddedFiles: |
| 163 | + - name: Run |
| 164 | + type: TEXT |
| 165 | + data: | |
| 166 | + # cowsay is provided by the PipVirtualEnv job environment, which put its |
| 167 | + # virtual environment on the PATH for this step. |
| 168 | + import cowsay |
| 169 | +
|
| 170 | + cowsay.cow("{{Param.Message}}") |
0 commit comments