Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

Fully automate dev setup with Gitpod #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitpod.Dockerfile
Original file line number Diff line number Diff line change
@@ -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/
5 changes: 5 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
image:
file: .gitpod.Dockerfile

tasks:
- init: bundle install
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion code_hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
NOHASH
01fffd0
1 change: 1 addition & 0 deletions copilot/.workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application: ecsworkshop
94 changes: 94 additions & 0 deletions copilot/buildspec.yml
Original file line number Diff line number Diff line change
@@ -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/*"
30 changes: 30 additions & 0 deletions copilot/ecsdemo-frontend/addons/task-role.yaml
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions copilot/ecsdemo-frontend/manifest.yml
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 30 additions & 0 deletions copilot/pipeline.yml
Original file line number Diff line number Diff line change
@@ -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]