From 01fffd0e2b82ff28685ceae001939b40fe8cbb82 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Tue, 29 Sep 2020 14:57:49 +0000 Subject: [PATCH 1/3] Adding copilot pipeline configuration --- copilot/.workspace | 1 + copilot/buildspec.yml | 94 +++++++++++++++++++++++++++ copilot/ecsdemo-frontend/manifest.yml | 43 ++++++++++++ copilot/pipeline.yml | 30 +++++++++ 4 files changed, 168 insertions(+) create mode 100644 copilot/.workspace create mode 100644 copilot/buildspec.yml create mode 100644 copilot/ecsdemo-frontend/manifest.yml create mode 100644 copilot/pipeline.yml diff --git a/copilot/.workspace b/copilot/.workspace new file mode 100644 index 0000000..8c5827e --- /dev/null +++ b/copilot/.workspace @@ -0,0 +1 @@ +application: ecsworkshop diff --git a/copilot/buildspec.yml b/copilot/buildspec.yml new file mode 100644 index 0000000..fef3624 --- /dev/null +++ b/copilot/buildspec.yml @@ -0,0 +1,94 @@ +# Buildspec runs in the build stage of your pipeline. +version: 0.2 +phases: + install: + runtime-versions: + docker: 18 + ruby: 2.6 + commands: + - echo "cd into $CODEBUILD_SRC_DIR" + - cd $CODEBUILD_SRC_DIR + # Download the copilot linux binary. + - wget https://ecs-cli-v2-release.s3.amazonaws.com/copilot-linux-v0.4.0 + - mv ./copilot-linux-v0.4.0 ./copilot-linux + - chmod +x ./copilot-linux + build: + commands: + - echo "Run your tests" + # - make test + post_build: + commands: + - ls -l + - export COLOR="false" + # Find all the local services in the workspace. + - svcs=$(./copilot-linux svc ls --local --json | jq '.services[].name' | sed 's/"//g') + # Find all the environments. + - envs=$(./copilot-linux env ls --json | jq '.environments[].name' | sed 's/"//g') + # Generate the cloudformation templates. + # The tag is the build ID but we replaced the colon ':' with a dash '-'. + - tag=$(sed 's/:/-/g' <<<"$CODEBUILD_BUILD_ID") + - > + for env in $envs; do + for svc in $svcs; do + ./copilot-linux svc package -n $svc -e $env --output-dir './infrastructure' --tag $tag; + done; + done; + - ls -lah ./infrastructure + # If addons exists, upload addons templates to each S3 bucket and write template URL to template config files. + - | + for svc in $svcs; do + ADDONSFILE=./infrastructure/$svc.addons.stack.yml + if [ -f "$ADDONSFILE" ]; then + tmp=$(mktemp) + timestamp=$(date +%s) + aws s3 cp "$ADDONSFILE" "s3://stackset-ecsworkshop-inf-pipelinebuiltartifactbuc-lpffwlbqieel/manual/$timestamp/$svc.addons.stack.yml"; + jq --arg a "https://stackset-ecsworkshop-inf-pipelinebuiltartifactbuc-lpffwlbqieel.s3-ap-southeast-1.amazonaws.com/manual/$timestamp/$svc.addons.stack.yml" '.Parameters.AddonsTemplateURL = $a' ./infrastructure/$svc-test.params.json > "$tmp" && mv "$tmp" ./infrastructure/$svc-test.params.json + fi + done; + # Build images + # - For each manifest file: + # - Read the path to the Dockerfile by translating the YAML file into JSON. + # - Run docker build. + # - For each environment: + # - Retrieve the ECR repository. + # - Login and push the image. + - > + for svc in $svcs; do + manifest=$(cat $CODEBUILD_SRC_DIR/copilot/$svc/manifest.yml | ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(ARGF))') + base_dockerfile=$(echo $manifest | jq '.image.build') + build_dockerfile=$(echo $manifest| jq 'if .image.build?.dockerfile? then .image.build.dockerfile else "" end' | sed 's/"//g') + build_context=$(echo $manifest| jq 'if .image.build?.context? then .image.build.context else "" end' | sed 's/"//g') + dockerfile_args=$(echo $manifest | jq 'if .image.build?.args? then .image.build.args else "" end | to_entries?') + df_rel_path=$( echo $base_dockerfile | sed 's/"//g') + if [ -n "$build_dockerfile" ]; then + df_rel_path=$build_dockerfile + fi + df_path=$df_rel_path + df_dir_path=$(dirname "$df_path") + if [ -n "$build_context" ]; then + df_dir_path=$build_context + fi + build_args= + if [ -n "$dockerfile_args" ]; then + for arg in $(echo $dockerfile_args | jq -r '.[] | "\(.key)=\(.value)"'); do + build_args="$build_args--build-arg $arg " + done + fi + echo "Service: $svc" + echo "Relative Dockerfile path: $df_rel_path" + echo "Docker build context: $df_dir_path" + echo "Docker build args: $build_args" + echo "Running command: docker build -t $svc:$tag $build_args-f $df_path $df_dir_path"; + docker build -t $svc:$tag $build_args-f $df_path $df_dir_path; + image_id=$(docker images -q $svc:$tag); + for env in $envs; do + repo=$(cat $CODEBUILD_SRC_DIR/infrastructure/$svc-$env.params.json | jq '.Parameters.ContainerImage' | sed 's/"//g'); + region=$(echo $repo | cut -d'.' -f4); + $(aws ecr get-login --no-include-email --region $region); + docker tag $image_id $repo; + docker push $repo; + done; + done; +artifacts: + files: + - "infrastructure/*" \ No newline at end of file diff --git a/copilot/ecsdemo-frontend/manifest.yml b/copilot/ecsdemo-frontend/manifest.yml new file mode 100644 index 0000000..9bcf185 --- /dev/null +++ b/copilot/ecsdemo-frontend/manifest.yml @@ -0,0 +1,43 @@ +# The manifest for the "ecsdemo-frontend" service. +# Read the full specification for the "Load Balanced Web Service" type at: +# https://github.com/aws/copilot-cli/wiki/Manifests#load-balanced-web-svc + +# Your service name will be used in naming your resources like log groups, ECS services, etc. +name: ecsdemo-frontend +# The "architecture" of the service you're running. +type: Load Balanced Web Service + +image: + # Docker build arguments. You can specify additional overrides here. Supported: dockerfile, context, args + build: Dockerfile + # Port exposed through your container to route traffic to it. + port: 3000 + +http: + # Requests to this path will be forwarded to your service. + # To match all requests you can use the "/" path. + path: '/' + # You can specify a custom health check path. The default is "/" + # healthcheck: '/' + # You can enable sticky sessions. + # stickiness: true + +# Number of CPU units for the task. +cpu: 256 +# Amount of memory in MiB used by the task. +memory: 512 +# Number of tasks that should be running in your service. +count: 3 + +# Optional fields for more advanced use-cases. +# +#variables: # Pass environment variables as key value pairs. +# LOG_LEVEL: info +# +#secrets: # Pass secrets from AWS Systems Manager (SSM) Parameter Store. +# GITHUB_TOKEN: GITHUB_TOKEN # The key is the name of the environment variable, the value is the name of the SSM parameter. + +# You can override any of the values defined above by environment. +#environments: +# test: +# count: 2 # Number of tasks to run for the "test" environment. diff --git a/copilot/pipeline.yml b/copilot/pipeline.yml new file mode 100644 index 0000000..d10455e --- /dev/null +++ b/copilot/pipeline.yml @@ -0,0 +1,30 @@ +# This YAML file defines the relationship and deployment ordering of your environments. + +# The name of the pipeline +name: pipeline-ecsworkshop-bdn9385-ecsdemo-frontend + +# The version of the schema used in this template +version: 1 + +# This section defines the source artifacts. +source: + # The name of the provider that is used to store the source artifacts. + provider: GitHub + # Additional properties that further specifies the exact location + # the artifacts should be sourced from. For example, the GitHub provider + # has the following properties: repository, branch. + properties: + access_token_secret: github-token-ecsworkshop-ecsdemo-frontend + branch: master + repository: https://github.com/bdn9385/ecsdemo-frontend + +# The deployment section defines the order the pipeline will deploy +# to your environments. +stages: + - # The name of the environment to deploy to. + name: test + # Optional: flag for manual approval action before deployment. + # requires_approval: true + # Optional: use test commands to validate this stage of your build. + # test_commands: [echo 'running tests', make test] + From 45eae0a7ebfa9dd11ed2f5c1e0ec8d0696586b17 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Tue, 29 Sep 2020 15:30:27 +0000 Subject: [PATCH 2/3] Adding an IAM policy addon --- code_hash.txt | 2 +- .../ecsdemo-frontend/addons/task-role.yaml | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 copilot/ecsdemo-frontend/addons/task-role.yaml diff --git a/code_hash.txt b/code_hash.txt index a7c041f..a3648ba 100644 --- a/code_hash.txt +++ b/code_hash.txt @@ -1 +1 @@ -NOHASH +01fffd0 diff --git a/copilot/ecsdemo-frontend/addons/task-role.yaml b/copilot/ecsdemo-frontend/addons/task-role.yaml new file mode 100644 index 0000000..35a1cb1 --- /dev/null +++ b/copilot/ecsdemo-frontend/addons/task-role.yaml @@ -0,0 +1,30 @@ +# You can use any of these parameters to create conditions or mappings in your template. +Parameters: + App: + Type: String + Description: Your application's name. + Env: + Type: String + Description: The environment name your service, job, or workflow is being deployed to. + Name: + Type: String + Description: The name of the service, job, or workflow being deployed. + +Resources: + SubnetsAccessPolicy: + Type: AWS::IAM::ManagedPolicy + Properties: + PolicyDocument: + Version: 2012-10-17 + Statement: + - Sid: EC2Actions + Effect: Allow + Action: + - ec2:DescribeSubnets + Resource: "*" + +Outputs: + # You also need to output the IAM ManagedPolicy so that Copilot can inject it to your ECS task role. + SubnetsAccessPolicyArn: + Description: "The ARN of the Policy to attach to the task role." + Value: !Ref SubnetsAccessPolicy From f16b47ad7b43333690802827b4d8b4c876609c99 Mon Sep 17 00:00:00 2001 From: bdn9385 Date: Wed, 24 Feb 2021 15:57:27 +0000 Subject: [PATCH 3/3] Fully automate dev setup with Gitpod This commit implements a fully-automated development setup using Gitpod.io, an online IDE for GitLab, GitHub, and Bitbucket that enables Dev-Environments-As-Code. This makes it easy for anyone to get a ready-to-code workspace for any branch, issue or pull request almost instantly with a single click. --- .gitpod.Dockerfile | 7 +++++++ .gitpod.yml | 5 +++++ README.md | 2 ++ 3 files changed, 14 insertions(+) create mode 100644 .gitpod.Dockerfile create mode 100644 .gitpod.yml diff --git a/.gitpod.Dockerfile b/.gitpod.Dockerfile new file mode 100644 index 0000000..b7a628d --- /dev/null +++ b/.gitpod.Dockerfile @@ -0,0 +1,7 @@ +FROM gitpod/workspace-mysql + +# Install custom tools, runtimes, etc. +# For example "bastet", a command-line tetris clone: +# RUN brew install bastet +# +# More information: https://www.gitpod.io/docs/config-docker/ diff --git a/.gitpod.yml b/.gitpod.yml new file mode 100644 index 0000000..a0d2eec --- /dev/null +++ b/.gitpod.yml @@ -0,0 +1,5 @@ +image: + file: .gitpod.Dockerfile + +tasks: + - init: bundle install diff --git a/README.md b/README.md index eba9ef6..678e607 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/brentley/ecsdemo-frontend) + ![Build Status](https://codebuild.us-east-2.amazonaws.com/badges?uuid=eyJlbmNyeXB0ZWREYXRhIjoiMnZsWms5clp6NEwvRnJXYUsyWjBmcnBiUWVRaFVsRlpENmg3MWU0M2oxVFpEdDdtSDRVRXJJZm1NNXdGQWIrWVU5UTFHd1RZUTdnU29SV0JyeVNHU1R3PSIsIml2UGFyYW1ldGVyU3BlYyI6InVpTTNLMlRtUEV6ZzJCZ2oiLCJtYXRlcmlhbFNldFNlcmlhbCI6MX0%3D&branch=master) # Amazon Containers Workshop