Skip to content

Commit

Permalink
Fix codepipeline build (#367)
Browse files Browse the repository at this point in the history
* Code Pipeline improvements

* More hacking

* More hacking

* Simplify

* Fix?

* Dependencies

* Better handling

* Handle stack creation

* Revert testing changes

* Changelog update
  • Loading branch information
matteofigus authored Jun 9, 2023
1 parent e9dfa7f commit 8bb9d99
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 20 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Change Log

## v0.60 (unreleased)
## v0.60

- [#367](https://github.com/awslabs/amazon-s3-find-and-forget/pull/367): Fix
deployment issue caused by deprecation of polling-based S3 sources in AWS
CodePipeline
- [#366](https://github.com/awslabs/amazon-s3-find-and-forget/pull/366): Upgrade
backend and frontend dependencies

Expand Down
22 changes: 18 additions & 4 deletions backend/lambdas/custom_resources/rerun_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,33 @@


@with_logging
@helper.create
@helper.delete
def create(event, context):
def delete(event, context):
return None


@with_logging
@helper.create
@helper.update
def update(event, context):
props = event["ResourceProperties"]
props_old = event["OldResourceProperties"]
if props_old["DeployWebUI"] == "false" and props["DeployWebUI"] == "true":
props_old = event.get("OldResourceProperties", {})

deploy_ui_changed = (
"DeployWebUI" in props_old
and props_old["DeployWebUI"] == "false"
and props["DeployWebUI"] == "true"
)

new_version = (
(not "Version" in props_old or props_old["Version"] != props["Version"])
if "Version" in props
else "Version" in props_old
)

if deploy_ui_changed or new_version:
pipe_client.start_pipeline_execution(name=props["PipelineName"])

return None


Expand Down
10 changes: 5 additions & 5 deletions templates/deployment_helper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ Resources:
Value: !Ref WebUIBucket
Name: !Sub ${ResourcePrefix}FrontendBuild
ServiceRole: !Ref CodeBuildFrontendServiceRole

CodePipelineServiceRole:
Type: AWS::IAM::Role
Properties:
Expand Down Expand Up @@ -293,6 +293,7 @@ Resources:
Configuration:
S3Bucket: !Ref CodeBuildArtefactBucket
S3ObjectKey: !Ref ArtefactName
PollForSourceChanges: false
OutputArtifacts:
- Name: S3F2
RunOrder: 1
Expand Down Expand Up @@ -435,7 +436,6 @@ Resources:
Resource:
- !Sub arn:${AWS::Partition}:codepipeline:${AWS::Region}:${AWS::AccountId}:${CodePipeline}


CleanupCodeBuildArtefactBucket:
Type: Custom::Setup
Properties:
Expand Down Expand Up @@ -473,7 +473,7 @@ Resources:

WaitForContainerBuild:
Type: Custom::Setup
DependsOn: CopyBuildArtefact
DependsOn: RerunPipeline
Properties:
ServiceToken: !GetAtt WaitForContainerBuildFunction.Arn
ArtefactName: !Ref ArtefactName
Expand All @@ -482,7 +482,6 @@ Resources:
LogLevel: !Ref LogLevel
Version: !Ref Version


RedeployApi:
Type: Custom::Setup
Properties:
Expand All @@ -492,11 +491,12 @@ Resources:
ApiStage: !Select [3, !Split ["/", !Ref ApiUrl]]
DeployCognito: !Ref DeployCognito


RerunPipeline:
Type: Custom::Setup
DependsOn: CopyBuildArtefact
Properties:
ServiceToken: !GetAtt RerunPipelineFunction.Arn
LogLevel: !Ref LogLevel
PipelineName: !Ref CodePipeline
DeployWebUI: !Ref DeployWebUI
Version: !Ref Version
4 changes: 2 additions & 2 deletions templates/template.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Amazon S3 Find and Forget (uksb-1q2j8beb0) (version:v0.59)
Description: Amazon S3 Find and Forget (uksb-1q2j8beb0) (version:v0.60)

Parameters:
AccessControlAllowOriginOverride:
Expand Down Expand Up @@ -206,7 +206,7 @@ Conditions:
Mappings:
Solution:
Constants:
Version: 'v0.59'
Version: 'v0.60'

Resources:
TempBucket:
Expand Down
96 changes: 88 additions & 8 deletions tests/unit/crs/test_cr_rerun_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,96 @@
import pytest
from mock import patch, MagicMock

from backend.lambdas.custom_resources.rerun_pipeline import create, update, handler
from backend.lambdas.custom_resources.rerun_pipeline import delete, update, handler

pytestmark = [pytest.mark.unit, pytest.mark.task]


@patch("backend.lambdas.custom_resources.rerun_pipeline.pipe_client")
def test_it_triggers_execution_if_required(mock_client):
def test_it_triggers_execution_if_web_ui_setting_changes(mock_client):
event = {
"ResourceProperties": {"DeployWebUI": "true", "PipelineName": "pipeline"},
"OldResourceProperties": {"DeployWebUI": "false"},
"ResourceProperties": {
"DeployWebUI": "true",
"PipelineName": "pipeline",
"Version": "v0.60",
},
"OldResourceProperties": {"DeployWebUI": "false", "Version": "v0.60"},
}

resp = update(event, MagicMock())

mock_client.start_pipeline_execution.assert_called_with(name="pipeline")

assert not resp


@patch("backend.lambdas.custom_resources.rerun_pipeline.pipe_client")
def test_it_triggers_execution_if_new_version(mock_client):
event = {
"ResourceProperties": {
"DeployWebUI": "true",
"PipelineName": "pipeline",
"Version": "v0.62",
},
"OldResourceProperties": {"DeployWebUI": "true", "Version": "v0.61"},
}

resp = update(event, MagicMock())

mock_client.start_pipeline_execution.assert_called_with(name="pipeline")

assert not resp


@patch("backend.lambdas.custom_resources.rerun_pipeline.pipe_client")
def test_it_triggers_execution_if_new_version_old_signature(mock_client):
event = {
"ResourceProperties": {
"DeployWebUI": "true",
"PipelineName": "pipeline",
"Version": "v0.60",
},
"OldResourceProperties": {
"DeployWebUI": "true"
}, # Prior to 0.59, the Version parameter wasn't sent to the CR payload
}

resp = update(event, MagicMock())

mock_client.start_pipeline_execution.assert_called_with(name="pipeline")

assert not resp


@patch("backend.lambdas.custom_resources.rerun_pipeline.pipe_client")
def test_it_triggers_execution_if_rollback_to_old_signature(mock_client):
event = {
"ResourceProperties": {
"DeployWebUI": "true",
"PipelineName": "pipeline",
# Prior to 0.59, the Version parameter wasn't sent to the CR payload
},
"OldResourceProperties": {
"DeployWebUI": "true",
"Version": "v0.60",
},
}

resp = update(event, MagicMock())

mock_client.start_pipeline_execution.assert_called_with(name="pipeline")

assert not resp


@patch("backend.lambdas.custom_resources.rerun_pipeline.pipe_client")
def test_it_triggers_execution_if_new_stack(mock_client):
event = {
"ResourceProperties": {
"DeployWebUI": "true",
"PipelineName": "pipeline",
"Version": "v0.60",
}
}

resp = update(event, MagicMock())
Expand All @@ -23,8 +103,8 @@ def test_it_triggers_execution_if_required(mock_client):
@patch("backend.lambdas.custom_resources.rerun_pipeline.pipe_client")
def test_it_does_not_trigger_execution_if_not_required(mock_client):
event = {
"ResourceProperties": {"DeployWebUI": "false"},
"OldResourceProperties": {"DeployWebUI": "false"},
"ResourceProperties": {"DeployWebUI": "false", "Version": "v0.60"},
"OldResourceProperties": {"DeployWebUI": "false", "Version": "v0.60"},
}

resp = update(event, MagicMock())
Expand All @@ -35,8 +115,8 @@ def test_it_does_not_trigger_execution_if_not_required(mock_client):


@patch("backend.lambdas.custom_resources.rerun_pipeline.pipe_client")
def test_it_does_nothing_on_create(mock_client):
resp = create({}, MagicMock())
def test_it_does_nothing_on_delete(mock_client):
resp = delete({}, MagicMock())

mock_client.assert_not_called()
assert not resp
Expand Down

0 comments on commit 8bb9d99

Please sign in to comment.