Skip to content

Commit 7bdd1c1

Browse files
feat: check if iamge has been pushed in the last 7 days
1 parent e3bf0ad commit 7bdd1c1

File tree

6 files changed

+77
-20
lines changed

6 files changed

+77
-20
lines changed

.github/workflows/docker-build-publish.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
uses: actions/checkout@v3
2222

2323
- name: Log in to the Container registry
24-
uses: docker/login-action@40891eba8c2bcd1309b07ba8b11232f313e86779
24+
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
2525
with:
2626
registry: ${{ env.REGISTRY }}
2727
username: ${{ github.actor }}
@@ -34,7 +34,7 @@ jobs:
3434
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
3535

3636
- name: Build and push Docker image
37-
uses: docker/build-push-action@eafaea8d0f5853934deece2ffa67af59d936562b
37+
uses: docker/build-push-action@817ed59f97d2974e2c1d92f170242f52fd3feae4
3838
with:
3939
context: .
4040
push: true

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"python.formatting.provider": "black"
2+
"python.formatting.provider": "none",
3+
"[python]": {
4+
"editor.defaultFormatter": "ms-python.black-formatter"
5+
}
36
}

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ test: lint
1111
run:
1212
poetry run ./main.py
1313

14+
push: test
15+
git push
16+
1417
dry-run:
1518
DRY_RUN=true poetry run ./main.py
1619

main.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,31 @@ def get_ecr_images(
195195
return images
196196

197197

198+
def is_image_pushed_recently(image: dict) -> bool:
199+
"""
200+
:param image dict: image details
201+
Checks to see if the image has been pushed in the last 7 days
202+
:return bool: if the image has been pushed in the last 7 days True will be returned
203+
"""
204+
logging.debug("Checking if the image has been pushed recently")
205+
logger.debug(f"{image=}")
206+
if "imagePushedAt" in image:
207+
last_pull_time = image["imagePushedAt"]
208+
localized_now_ts = UTC.localize(datetime.now() - timedelta(7))
209+
logger.debug(last_pull_time)
210+
logger.debug(localized_now_ts)
211+
if last_pull_time > localized_now_ts:
212+
logger.debug("The last pulltime was more than 7 days ago")
213+
return True
214+
else:
215+
return False
216+
else:
217+
logger.info(
218+
f"There is no imagePushedAt because the {image['image_uri']} has something terribly wrong with it"
219+
)
220+
return False
221+
222+
198223
def is_image_pulled_recently(image: dict) -> bool:
199224
"""
200225
:param image dict: image details
@@ -283,7 +308,8 @@ def is_image_deletable(image: dict, k8s_images: list) -> bool:
283308
# TODO: Make it handle multiple tags
284309

285310
if (
286-
is_image_referenced(image, k8s_images)
311+
is_image_pushed_recently(image)
312+
or is_image_referenced(image, k8s_images)
287313
or is_image_pulled_recently(image)
288314
or is_image_tagged_keep(image)
289315
):

poetry.lock

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test_main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,31 @@ def test_is_image_pulled_recently(image, result):
145145
assert (main.is_image_pulled_recently(image)) == result
146146

147147

148+
@pytest.mark.parametrize(
149+
"image,result",
150+
[
151+
(
152+
{
153+
"imagePushedAt": UTC.localize(datetime.now() - timedelta(2)),
154+
},
155+
True,
156+
),
157+
(
158+
{
159+
"imagePushedAt": UTC.localize(datetime.now() - timedelta(10)),
160+
},
161+
False,
162+
),
163+
(
164+
{"image_uri": "test"},
165+
False,
166+
),
167+
],
168+
)
169+
def test_is_image_pushed_recently(image, result):
170+
assert (main.is_image_pushed_recently(image)) == result
171+
172+
148173
@pytest.mark.parametrize(
149174
"image,result",
150175
[

0 commit comments

Comments
 (0)