Skip to content

Commit

Permalink
addin python codepipeline docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
josjaf committed Oct 10, 2019
1 parent c8bf5ff commit 4828d95
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 0 deletions.
75 changes: 75 additions & 0 deletions python/codepipeline-docker-build/Base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from aws_cdk import (
aws_s3 as aws_s3,
aws_ecr,
aws_codebuild,
aws_ssm,
core,
)


class Base(core.Stack):
def __init__(self, app: core.App, id: str, props, **kwargs) -> None:
super().__init__(app, id, **kwargs)

# pipeline requires versioned bucket
bucket = aws_s3.Bucket(
self, "SourceBucket",
bucket_name=f"{props.namespace.lower()}-{core.Aws.ACCOUNT_ID}",
versioned=True,
removal_policy=core.RemovalPolicy.DESTROY)
# ssm parameter to get bucket name laster
bucket_param = aws_ssm.StringParameter(
self, "ParameterB",
parameter_name=f"{props.namespace}-bucket",
string_value=bucket.bucket_name,
description='cdk pipeline bucket'
)
# ecr repo to push docker container into
ecr = aws_ecr.Repository(
self, "ECR",
repository_name=f"{props.namespace}",
removal_policy=core.RemovalPolicy.DESTROY

)

# codebuild project meant to run in pipeline
cb_docker_build = aws_codebuild.PipelineProject(
self, "DockerBuild",
project_name=f"{props.namespace}-Docker-Build",
build_spec=aws_codebuild.BuildSpec.from_source_filename(
filename='pipeline_delivery/docker_build_buildspec.yml'),
environment=aws_codebuild.BuildEnvironment(
privileged=True,

),
# pass the ecr repo uri into the codebuild project so codebuild knows where to push
environment_variables={
'ecr': aws_codebuild.BuildEnvironmentVariable(
value=ecr.repository_uri),
'tag': aws_codebuild.BuildEnvironmentVariable(
value='cdk')

},

description='Pipeline for CodeBuild',
timeout=core.Duration.minutes(60),

)
# codebuild iam permissions to read write s3
bucket.grant_read_write(cb_docker_build)

# codebuild permissions to interact with ecr
ecr.grant_pull_push(cb_docker_build)


# update props to pass objects to another stack
props.bucket_name = bucket.bucket_name
props.bucket_arn = bucket.bucket_arn
props.bucket_obj = bucket
props.cb_docker_build = cb_docker_build
self.output_props = props
# pass objects to another stack
@property
def outputs(self):
props = self.output_props
return props
59 changes: 59 additions & 0 deletions python/codepipeline-docker-build/Pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from aws_cdk import (

aws_codepipeline,
aws_codepipeline_actions,
aws_ssm,
core,

)


class Pipeline(core.Stack):
def __init__(self, app: core.App, id: str, props, **kwargs) -> None:
super().__init__(app, id, **kwargs)
# define the s3 artifact
source_output = aws_codepipeline.Artifact(artifact_name='source')

# define the pipeline
pipeline = aws_codepipeline.Pipeline(
self, "Pipeline",
pipeline_name=f"{props.namespace}",
artifact_bucket=props.bucket_obj,
stages=[
aws_codepipeline.StageProps(
stage_name='Source',
actions=[
aws_codepipeline_actions.S3SourceAction(

bucket=props.bucket_obj,
bucket_key='source.zip',
action_name='S3Source',
run_order=1,
output=source_output,

),
]
),
aws_codepipeline.StageProps(

stage_name='Build',
actions=[aws_codepipeline_actions.CodeBuildAction(
action_name='DockerBuildImages',
# role=codepipeline_role,
input=source_output,
project=props.cb_docker_build,
run_order=1,

)
]
)
]

)
# pipeline param to get the
pipeline_param = aws_ssm.StringParameter(
self, "ParameterP",
parameter_name=f"{props.namespace}-pipeline",
string_value=pipeline.pipeline_name,
description='cdk pipeline bucket'
)
26 changes: 26 additions & 0 deletions python/codepipeline-docker-build/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from aws_cdk import (
core,
)

from Base import Base
from Pipeline import Pipeline


# using props to pass in objects between stacks
class Props():
def __init__(self):
self.namespace = 'cdk-example-pipeline'
self.region = 'us-east-1'


props = Props()
app = core.App()

# stack for ecr, bucket, codebuild
base = Base(app, f"{props.namespace}-base", props, )
props = base.outputs

# pipeline stack
pipeline = Pipeline(app, f"{props.namespace}-pipeline", props)
pipeline.add_dependency(base)
app.synth()
3 changes: 3 additions & 0 deletions python/codepipeline-docker-build/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"app": "python3 app.py"
}
2 changes: 2 additions & 0 deletions python/codepipeline-docker-build/pipeline_delivery/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM python:3.7.2-alpine
RUN pip install awscli
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 0.2

phases:
pre_build:
commands:
- echo logging into docker
- $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)
build:
commands:
- echo Entered the post_build phase...
- echo Build completed on `date`
- docker build -t ${tag}:latest pipeline_delivery/
- docker tag $tag:latest $ecr:$tag
- docker push $ecr

11 changes: 11 additions & 0 deletions python/codepipeline-docker-build/push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash


export account_id=$(aws sts get-caller-identity | jq -r .Account)
export source_bucket=$(aws ssm get-parameter --name 'cdk-example-pipeline-bucket' | jq -r .Parameter.Value)
export pipeline_name=$(aws ssm get-parameter --name 'cdk-example-pipeline-pipeline' | jq -r .Parameter.Value)
export REGION='us-east-1'

zip -r source.zip .
aws s3 cp source.zip s3://${source_bucket}/source.zip
aws codepipeline start-pipeline-execution --name ${pipeline_name}
35 changes: 35 additions & 0 deletions python/codepipeline-docker-build/release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import boto3
import sys
from pathlib import Path
import shutil


def main():
zip = shutil.make_archive("base", root_dir='repo_kid/cdk', format='zip')
data = open(zip, 'rb')
body_data = data.read()
key = 'source.zip'

session = boto3.session.Session(profile_name=None)
shared_id = session.client("sts").get_caller_identity()['Account']
bucket = f"newport-alameda-{shared_id}"
namespace = 'newport-alameda'
s3 = session.client('s3')

s3_args = dict(Bucket=bucket, Key=key, Body=body_data)
response = s3.put_object(**s3_args)

p = Path().resolve().parent

cp = session.client('codepipeline')

pipeline_name = f'{namespace}'
response = cp.start_pipeline_execution(
name=pipeline_name,
)
print(response)
return


if __name__ == '__main__':
main()
49 changes: 49 additions & 0 deletions python/codepipeline-docker-build/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
attrs==19.2.0
aws-cdk.assets==1.12.0
aws-cdk.aws-apigateway==1.12.0
aws-cdk.aws-applicationautoscaling==1.12.0
aws-cdk.aws-autoscaling==1.12.0
aws-cdk.aws-autoscaling-common==1.12.0
aws-cdk.aws-autoscaling-hooktargets==1.12.0
aws-cdk.aws-certificatemanager==1.12.0
aws-cdk.aws-cloudformation==1.12.0
aws-cdk.aws-cloudfront==1.12.0
aws-cdk.aws-cloudwatch==1.12.0
aws-cdk.aws-codebuild==1.12.0
aws-cdk.aws-codecommit==1.12.0
aws-cdk.aws-codedeploy==1.12.0
aws-cdk.aws-codepipeline==1.12.0
aws-cdk.aws-codepipeline-actions==1.12.0
aws-cdk.aws-ec2==1.12.0
aws-cdk.aws-ecr==1.12.0
aws-cdk.aws-ecr-assets==1.12.0
aws-cdk.aws-ecs==1.12.0
aws-cdk.aws-elasticloadbalancing==1.12.0
aws-cdk.aws-elasticloadbalancingv2==1.12.0
aws-cdk.aws-events==1.12.0
aws-cdk.aws-events-targets==1.12.0
aws-cdk.aws-iam==1.12.0
aws-cdk.aws-kms==1.12.0
aws-cdk.aws-lambda==1.12.0
aws-cdk.aws-logs==1.12.0
aws-cdk.aws-route53==1.12.0
aws-cdk.aws-route53-targets==1.12.0
aws-cdk.aws-s3==1.12.0
aws-cdk.aws-s3-assets==1.12.0
aws-cdk.aws-secretsmanager==1.12.0
aws-cdk.aws-servicediscovery==1.12.0
aws-cdk.aws-sns==1.12.0
aws-cdk.aws-sns-subscriptions==1.12.0
aws-cdk.aws-sqs==1.12.0
aws-cdk.aws-ssm==1.12.0
aws-cdk.aws-stepfunctions==1.12.0
aws-cdk.core==1.12.0
aws-cdk.cx-api==1.12.0
aws-cdk.region-info==1.12.0
cattrs==0.9.0
importlib-resources==1.0.2
jsii==0.18.0
publication==0.0.3
python-dateutil==2.8.0
six==1.12.0
typing-extensions==3.7.4

0 comments on commit 4828d95

Please sign in to comment.