@@ -243,6 +243,9 @@ def __init__(self, scope: core.App, id: str, props, **kwargs) -> None:
243243
244244 df_project = DeviceFarmProject (self , id , project_name = device_farm_project_name )
245245 df_pool = DeviceFarmDevicePool (self , f"{ id } DevicePool" , project_arn = core .Token .as_string (df_project .project_arn ), device_pool_name = "SingleDeviceIntegTestDevicePool" )
246+
247+ # Bucket to store build artifacts, logs, test results, etc.
248+ artifact_bucket = self .__create_artifact_bucket ("ArtifactBucket" , bucket_name = f"{ codebuild_project_name_prefix .lower ()} -builds-{ self .account } " )
246249
247250 PullRequestBuilder (self , "UnitTestRunner" , project_name = f"{ codebuild_project_name_prefix } -UnitTest" ,
248251 github_owner = owner ,
@@ -255,18 +258,35 @@ def __init__(self, scope: core.App, id: str, props, **kwargs) -> None:
255258 github_repo = repo ,
256259 base_branch = base_branch ,
257260 buildspec_path = "scripts/devicefarm-test-runner-buildspec.yml" ,
261+ primary_artifact = aws_codebuild .Artifacts .s3 (
262+ bucket = artifact_bucket ,
263+ encryption = True ,
264+ include_build_id = True ,
265+ package_zip = False ,
266+ path = "instrumented/apks"
267+ ),
268+ secondary_artifacts = [
269+ aws_codebuild .Artifacts .s3 (
270+ bucket = artifact_bucket ,
271+ identifier = "reports" ,
272+ encryption = True ,
273+ include_build_id = True ,
274+ package_zip = False ,
275+ path = "instrumented/reports"
276+ )],
258277 environment_variables = {
259278 'DEVICEFARM_PROJECT_ARN' : aws_codebuild .BuildEnvironmentVariable (value = df_project .get_arn ()),
260279 'DEVICEFARM_POOL_ARN' : aws_codebuild .BuildEnvironmentVariable (value = df_pool .device_pool_arn ),
261280 'CONFIG_SOURCE_BUCKET' : aws_codebuild .BuildEnvironmentVariable (value = config_source_bucket )
262281 })
263- self ._add_codebuild_project_runner_permissions (integtest_project .role )
264- self ._add_devicefarm_test_runner_permissions_to_role (integtest_project .role )
282+
283+ self .__add_codebuild_project_runner_permissions (integtest_project .role )
284+ self .__add_devicefarm_test_runner_permissions_to_role (integtest_project .role )
265285
266286 def get_codebuild_project_name (self ):
267287 return self .code_build_project .project_name
268288
269- def _add_devicefarm_test_runner_permissions_to_role (self , role : aws_iam .Role ):
289+ def __add_devicefarm_test_runner_permissions_to_role (self , role : aws_iam .Role ):
270290 df_runner_policy = aws_iam .ManagedPolicy (self ,
271291 "AmplifyAndroidDeviceFarmTestRunnerPolicy" ,
272292 managed_policy_name = f"AmplifyAndroidDeviceFarmTestRunnerPolicy" ,
@@ -277,23 +297,12 @@ def _add_devicefarm_test_runner_permissions_to_role(self, role: aws_iam.Role):
277297 )
278298 df_runner_policy .attach_to_role (role )
279299
280- def _add_devicefarm_test_stage (self , pipeline , device_farm_project_id , device_farm_pool_arn ):
281- test_actions = []
282- for module_name in self .MODULES_WITH_INSTRUMENTED_TESTS :
283- test_actions .append (self ._create_devicefarm_test_action (device_farm_project_id , device_farm_pool_arn , module_name ))
284-
285- testing_stage = {
286- "Name" : "Test" ,
287- "Actions" : test_actions
288- }
289- pipeline_node = pipeline .node .default_child
290- pipeline_node .add_property_override ("Stages.2" , testing_stage )
291-
292- def _create_artifact_bucket (self , bucket_name :str ):
293- artifact_bucket = aws_s3 .Bucket (self , "PipelineAssets" ,
300+ def __create_artifact_bucket (self , id , * , bucket_name :str ):
301+ artifact_bucket = aws_s3 .Bucket (self , id ,
294302 bucket_name = bucket_name ,
295303 encryption = aws_s3 .BucketEncryption .KMS_MANAGED ,
296304 removal_policy = core .RemovalPolicy .DESTROY )
305+
297306 artifact_bucket .add_to_resource_policy (permission = aws_iam .PolicyStatement (
298307 principals = [aws_iam .AnyPrincipal ()],
299308 effect = aws_iam .Effect .DENY ,
@@ -324,40 +333,7 @@ def _create_artifact_bucket(self, bucket_name:str):
324333 ))
325334 return artifact_bucket
326335
327- # Not calling this right now since we can't filter out PRs in CodePipeline.
328- def _create_pipeline (self ,
329- build_pipeline_name : str ,
330- github_source : aws_codepipeline_actions .GitHubSourceAction ,
331- codebuild_project : aws_codebuild .PipelineProject ,
332- config_file_source_bucket_name :str ,
333- df_project : DeviceFarmProject ,
334- device_farm_pool_arn :str ):
335- artifact_bucket = self ._create_artifact_bucket (f"pipeline-assets-{ build_pipeline_name .lower ()} -{ self .account } " )
336- self .code_build_project = self ._create_codebuild_project ("AmplifyAndroidCodeBuildProject" )
337- amplify_android_build_output = aws_codepipeline .Artifact ("AmplifyAndroidBuildOutput" )
338- pipeline = aws_codepipeline .Pipeline (self ,
339- f"{ build_pipeline_name } Pipeline" ,
340- pipeline_name = build_pipeline_name ,
341- artifact_bucket = artifact_bucket ,
342- stages = [
343- aws_codepipeline .StageProps (
344- stage_name = "Source" ,
345- actions = [ github_source ]
346- ),
347- aws_codepipeline .StageProps (
348- stage_name = "Build" ,
349- actions = [self ._create_build_and_assemble_action (input_artifact = github_source .action_properties .outputs [0 ],
350- output_artifact = amplify_android_build_output ,
351- pipeline_project = codebuild_project ,
352- config_source_bucket = config_file_source_bucket_name )
353- ]
354- )
355- ])
356- self ._add_devicefarm_test_runner_permissions_to_role (pipeline .role )
357- self ._add_devicefarm_test_stage (pipeline , df_project .get_project_id (), device_farm_pool_arn )
358- return pipeline
359-
360- def _create_codebuild_project (self , id : str ):
336+ def __create_codebuild_project (self , id : str ):
361337 pipeline_project = aws_codebuild .PipelineProject (self ,
362338 id ,
363339 environment = aws_codebuild .BuildEnvironment (build_image = aws_codebuild .LinuxBuildImage .AMAZON_LINUX_2_3 ,
@@ -375,7 +351,7 @@ def _create_codebuild_project(self, id: str):
375351 build_exec_policy .attach_to_role (pipeline_project .role )
376352 return pipeline_project
377353
378- def _add_codebuild_project_runner_permissions (self , role : aws_iam .Role ):
354+ def __add_codebuild_project_runner_permissions (self , role : aws_iam .Role ):
379355 build_exec_policy = aws_iam .ManagedPolicy (self ,
380356 "AmplifyAndroidBuildExecutorPolicy" ,
381357 managed_policy_name = f"AmplifyAndroidBuildExecutorPolicy" ,
@@ -385,53 +361,3 @@ def _add_codebuild_project_runner_permissions(self, role: aws_iam.Role):
385361 ]
386362 )
387363 build_exec_policy .attach_to_role (role )
388-
389-
390- def _create_build_and_assemble_action (self ,
391- input_artifact :aws_codepipeline .Artifact ,
392- output_artifact :aws_codepipeline .Artifact ,
393- pipeline_project :aws_codebuild .PipelineProject ,
394- config_source_bucket : str = None ):
395- if config_source_bucket is None :
396- return aws_codepipeline_actions .CodeBuildAction (
397- action_name = 'BuildAndAssemble' ,
398- input = input_artifact ,
399- project = pipeline_project ,
400- outputs = [output_artifact ]
401- )
402- else :
403- return aws_codepipeline_actions .CodeBuildAction (
404- action_name = 'BuildAndAssemble' ,
405- input = input_artifact ,
406- project = pipeline_project ,
407- environment_variables = {
408- 'CONFIG_SOURCE_BUCKET' : aws_codebuild .BuildEnvironmentVariable (value = config_source_bucket )
409- },
410- outputs = [output_artifact ]
411- )
412-
413- def _create_devicefarm_test_action (self , project_id : str , device_pool_arn : str , module_name : str ):
414- return {
415- "Name" :f"{ module_name } -InstrumentedTests" ,
416- "ActionTypeId" : {
417- "Category" : "Test" ,
418- "Owner" : "AWS" ,
419- "Provider" : "DeviceFarm" ,
420- "Version" : "1"
421- },
422- "RunOrder" : 1 ,
423- "Configuration" : {
424- "App" : f"{ module_name } -debug-androidTest.apk" ,
425- "Test" : f"{ module_name } -debug-androidTest.apk" ,
426- "AppType" : "Android" ,
427- "DevicePoolArn" : device_pool_arn ,
428- "ProjectId" : project_id ,
429- "TestType" : "INSTRUMENTATION"
430- },
431- "OutputArtifacts" : [],
432- "InputArtifacts" : [
433- {
434- "Name" : "AmplifyAndroidBuildOutput"
435- }
436- ]
437- }
0 commit comments