-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom python script block to set automatic stage rollback
- Loading branch information
1 parent
0d19cfa
commit 4084d0f
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
codebase-pipelines/custom_pipeline_update/test_update_pipeline.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import pytest | ||
import unittest | ||
from unittest.mock import MagicMock, patch | ||
|
||
from update_pipeline import update_pipeline_stage_failure | ||
|
||
|
||
class TestUpdatePipeline(unittest.TestCase): | ||
|
||
def test_update_pipeline_stage_failure_sets_rollback(self): | ||
with patch("boto3.client") as mock_boto_client: | ||
mock_client = MagicMock() | ||
mock_boto_client.return_value = mock_client | ||
|
||
mock_client.get_pipeline.return_value = { | ||
"pipeline": { | ||
"name": "test-pipeline", | ||
"stages": [ | ||
{ | ||
"name": "Deploy", | ||
} | ||
] | ||
} | ||
} | ||
|
||
update_pipeline_stage_failure(["test-pipeline"]) | ||
|
||
call_args = mock_client.update_pipeline.call_args[1] | ||
assert call_args["pipeline"]["stages"][0]["onFailure"]["result"] == "ROLLBACK" | ||
|
||
def test_update_pipeline_stage_failure_does_not_set_rollback(self): | ||
with patch("boto3.client") as mock_boto_client: | ||
mock_client = MagicMock() | ||
mock_boto_client.return_value = mock_client | ||
|
||
mock_client.get_pipeline.return_value = { | ||
"pipeline": { | ||
"name": "test-pipeline", | ||
"stages": [ | ||
{ | ||
"name": "Source", | ||
} | ||
] | ||
} | ||
} | ||
|
||
with pytest.raises(ValueError) as error_msg: | ||
update_pipeline_stage_failure(["test-pipeline"]) | ||
|
||
assert "Stage Deploy not found in pipeline test-pipeline" in str(error_msg.value) |
33 changes: 33 additions & 0 deletions
33
codebase-pipelines/custom_pipeline_update/update_pipeline.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import os | ||
import json | ||
import boto3 | ||
from typing import List | ||
|
||
|
||
def update_pipeline_stage_failure(pipelines: List[str]): | ||
for pipeline_name in pipelines: | ||
print(f"Updating pipeline {pipeline_name}") | ||
|
||
client = boto3.client("codepipeline") | ||
|
||
response = client.get_pipeline(name=pipeline_name) | ||
pipeline = response["pipeline"] | ||
|
||
stage_found = False | ||
for stage in pipeline["stages"]: | ||
if "Deploy" in stage["name"]: | ||
stage["onFailure"] = { | ||
"result": "ROLLBACK" | ||
} | ||
stage_found = True | ||
|
||
if not stage_found: | ||
raise ValueError(f"Stage Deploy not found in pipeline {pipeline_name}") | ||
|
||
client.update_pipeline(pipeline=pipeline) | ||
print(f"Updated Deploy stage onFailure property for {pipeline_name}") | ||
|
||
|
||
if __name__ == "__main__": | ||
pipelines = json.loads(os.environ['PIPELINES']) | ||
update_pipeline_stage_failure(pipelines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters