|
| 1 | +# Resource Properties |
| 2 | + |
| 3 | +Resources can have custom **properties** (name:value pairs) that are exposed |
| 4 | +as environment variables when the resource is locked. |
| 5 | + |
| 6 | +## Defining properties |
| 7 | + |
| 8 | +Properties can be added to a resource via: |
| 9 | + |
| 10 | +- **Web UI** — Manage Jenkins → Lockable Resources → edit a resource → add properties |
| 11 | +- **JCasC** (Jenkins Configuration as Code): |
| 12 | + |
| 13 | +```yaml |
| 14 | +unclassified: |
| 15 | + lockableResourcesManager: |
| 16 | + resources: |
| 17 | + - name: "staging-server" |
| 18 | + properties: |
| 19 | + - name: "HOST" |
| 20 | + value: "192.168.1.10" |
| 21 | + - name: "PORT" |
| 22 | + value: "8080" |
| 23 | +``` |
| 24 | +
|
| 25 | +## Accessing properties in a pipeline |
| 26 | +
|
| 27 | +Properties are exposed as environment variables **only when the `variable` |
| 28 | +parameter is specified** in the `lock()` step. |
| 29 | + |
| 30 | +### Naming pattern |
| 31 | + |
| 32 | +| Variable | Value | |
| 33 | +|----------|-------| |
| 34 | +| `{variable}` | Comma-separated list of all locked resource names | |
| 35 | +| `{variable}0` | Name of the first locked resource | |
| 36 | +| `{variable}0_{PROPERTY_NAME}` | Value of that resource's property | |
| 37 | +| `{variable}1` | Name of the second locked resource (if any) | |
| 38 | +| `{variable}1_{PROPERTY_NAME}` | Value of the second resource's property | |
| 39 | + |
| 40 | +### Example: Read properties after locking by name |
| 41 | + |
| 42 | +```groovy |
| 43 | +pipeline { |
| 44 | + agent any |
| 45 | + stages { |
| 46 | + stage('Deploy') { |
| 47 | + options { |
| 48 | + lock(resource: 'staging-server', variable: 'LOCKED') |
| 49 | + } |
| 50 | + steps { |
| 51 | + echo "Resource: ${env.LOCKED0}" // staging-server |
| 52 | + echo "Host: ${env.LOCKED0_HOST}" // 192.168.1.10 |
| 53 | + echo "Port: ${env.LOCKED0_PORT}" // 8080 |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Example: Lock by label and read properties |
| 61 | + |
| 62 | +```groovy |
| 63 | +pipeline { |
| 64 | + agent any |
| 65 | + stages { |
| 66 | + stage('Test') { |
| 67 | + options { |
| 68 | + lock(label: 'gpu', quantity: 1, variable: 'GPU') |
| 69 | + } |
| 70 | + steps { |
| 71 | + echo "Got: ${env.GPU0}" |
| 72 | + echo "GPU model: ${env.GPU0_MODEL}" |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## Filtering resources by properties |
| 80 | + |
| 81 | +Use a `resourceMatchScript` to lock only resources whose properties match |
| 82 | +specific criteria: |
| 83 | + |
| 84 | +```groovy |
| 85 | +lock(extra: [ |
| 86 | + [$class: 'LockableResourcesStruct', |
| 87 | + resourceMatchScript: [ |
| 88 | + $class: 'SecureGroovyScript', |
| 89 | + script: ''' |
| 90 | + resourceInstance.properties.any { |
| 91 | + it.name == "ENV" && it.value == "staging" |
| 92 | + } |
| 93 | + ''', |
| 94 | + sandbox: true |
| 95 | + ], |
| 96 | + resourceNumber: '1' |
| 97 | + ] |
| 98 | +]) { |
| 99 | + echo "Got a staging resource: ${env.LOCKED_RESOURCE0}" |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## Common pitfalls |
| 104 | + |
| 105 | +1. **Missing `variable` parameter** — without it, no environment variables are |
| 106 | + created. This is the most common reason properties appear to be `null`. |
| 107 | +2. **Property name is case-sensitive** — if the property is named `host`, the |
| 108 | + env var is `LOCKED0_host`, not `LOCKED0_HOST`. |
| 109 | +3. **Properties are only available inside the lock block** — they cannot be |
| 110 | + accessed after the lock is released. |
0 commit comments