Skip to content

Commit 8d4b9ad

Browse files
achristie-ima.b.christie
andauthored
Support for image extraction of environment constants (#2)
* feat: Add get_environment_constants() * test: Add test for constants * ci: Update action versions --------- Co-authored-by: a.b.christie <alan.christie@matildapeak.com>
1 parent a1d6145 commit 8d4b9ad

4 files changed

Lines changed: 55 additions & 5 deletions

File tree

.github/workflows/build.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ jobs:
3737
- '3.13'
3838
steps:
3939
- name: Checkout
40-
uses: actions/checkout@v4
40+
uses: actions/checkout@v6
4141
- name: Set up Python ${{ matrix.python-version }}
42-
uses: actions/setup-python@v5
42+
uses: actions/setup-python@v6
4343
with:
4444
python-version: ${{ matrix.python-version }}
4545
- name: Install requirements

.github/workflows/publish.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ jobs:
3131
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
3232
steps:
3333
- name: Checkout
34-
uses: actions/checkout@v4
34+
uses: actions/checkout@v6
3535
- name: Inject slug/short variables
36-
uses: rlespinasse/github-slug-action@v4
36+
uses: rlespinasse/github-slug-action@v5
3737
- name: Set up Python
38-
uses: actions/setup-python@v5
38+
uses: actions/setup-python@v6
3939
with:
4040
python-version: '3.12'
4141
- name: Install dependencies

decoder/decoder.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,27 @@ def get_environment_assets(job_definition: Dict[str, Any]) -> List[Dict[str, str
323323
return env_assets
324324

325325

326+
def get_environment_constants(job_definition: Dict[str, Any]) -> List[Dict[str, str]]:
327+
"""Given a Job definition this function returns the list of all the
328+
environment-based image constants declared. What's returned is a list
329+
of the environment variable's name and its value.
330+
"""
331+
env_constants: List[Dict[str, str]] = []
332+
# Iterate through the environment block...
333+
environment: List[Dict[str, Any]] = job_definition.get("image", {}).get(
334+
"environment", []
335+
)
336+
env_constants.extend(
337+
{
338+
item["name"]: item["value-from"]["constant"]["value"],
339+
}
340+
for item in environment
341+
if "constant" in item["value-from"]
342+
)
343+
344+
return env_constants
345+
346+
326347
def get_jobs_replaced(job_definition: Dict[str, Any]) -> Optional[List[str]]:
327348
"""Given a Job Definition this function returns the Jobs it replaces.
328349
The returned list is a list of jobs identified by collection and
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Tests for the decoder's get_environment_assets() function.
2+
from typing import Dict
3+
4+
import pytest
5+
6+
pytestmark = pytest.mark.unit
7+
8+
from decoder import decoder
9+
10+
11+
def test_get_environment_constants():
12+
# Arrange
13+
job_definition: Dict = {
14+
"image": {
15+
"environment": [
16+
{
17+
"name": "BLOB",
18+
"value-from": {"constant": {"value": "42"}},
19+
}
20+
],
21+
},
22+
}
23+
24+
# Act
25+
env_assets = decoder.get_environment_constants(job_definition)
26+
27+
# Assert
28+
assert len(env_assets) == 1
29+
assert env_assets[0]["BLOB"] == "42"

0 commit comments

Comments
 (0)