Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #83 from oneleif/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
0xLeif authored Mar 11, 2020
2 parents 75634a5 + e5413d4 commit be7ab49
Show file tree
Hide file tree
Showing 59 changed files with 1,111 additions and 894 deletions.
93 changes: 84 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,105 @@
version: 2
version: 2.1

orbs:
aws-code-deploy: circleci/aws-code-deploy@1.0.0
aws-cli: circleci/aws-cli@0.1.20

commands:
deploy-to-aws:
parameters:
environment:
type: string
default: staging
steps:
- checkout
- setup_remote_docker
- run:
name: Build docker release image
command: |
docker build -t oneleif-api:latest -f web.Dockerfile --build-arg env=<< parameters.environment >> .
docker tag oneleif-api:latest oneleif-api:$CIRCLE_SHA1
- run:
name: Create revision bundle
command: |
mkdir -p $BUNDLE_DIR
docker save oneleif-api | gzip > $BUNDLE_DIR/oneleif-api.$CIRCLE_SHA1.tar.gz
cp -r Deploy/* $BUNDLE_DIR/
- aws-cli/setup:
aws-region: AWS_REGION
- aws-code-deploy/push-bundle:
application-name: $CODE_DEPLOY_APP_NAME
bundle-bucket: $CODE_DEPLOY_BUNDLE_BUCKET
bundle-key: << parameters.environment >>/$CIRCLE_SHA1
bundle-source: $BUNDLE_DIR
- aws-code-deploy/deploy-bundle:
application-name: $CODE_DEPLOY_APP_NAME
bundle-bucket: $CODE_DEPLOY_BUNDLE_BUCKET
bundle-key: << parameters.environment >>/$CIRCLE_SHA1
deployment-group: << parameters.environment >>

jobs:
linux:
test:
docker:
- image: swift:4.1
- image: circleci/postgres:latest
environment:
POSTGRES_USER: oneleif
POSTGRES_PASSWORD: root
POSTGRES_DB: oneleif-testing
POSTGRES_INITDB_ARGS: --auth-local password --auth-host password
steps:
- checkout
- run:
- run:
name: Compile code
command: swift build
- run:
- run:
name: Run unit tests
command: swift test

linux-release:
test-release:
docker:
- image: swift:4.1
steps:
- checkout
- run:
- run:
name: Compile code with optimizations
command: swift build -c release

build-and-deploy-staging:
docker:
- image: circleci/golang:latest
steps:
- deploy-to-aws:
environment: staging

build-and-deploy-production:
docker:
- image: circleci/golang:latest
steps:
- deploy-to-aws:
environment: production

workflows:
version: 2
tests:
build-and-deploy:
jobs:
- linux
- linux-release
- test
- test-release
- build-and-deploy-staging:
requires:
- test
- test-release
filters:
branches:
only:
- develop
context: oneleif-aws
- build-and-deploy-production:
requires:
- test
- test-release
filters:
branches:
only:
- master
context: oneleif-aws
15 changes: 15 additions & 0 deletions Deploy/appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# For help completing this file, see the "AppSpec File Reference" in the
# "CodeDeploy User Guide" at
# https://docs.aws.amazon.com/codedeploy/latest/userguide/app-spec-ref.html
version: 0.0
os: linux

hooks:
AfterInstall:
- location: hooks/after.sh
timeout: 300
runas: root
ValidateService:
- location: hooks/validate.sh
timeout: 300
runas: root
32 changes: 32 additions & 0 deletions Deploy/hooks/after.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

CONTAINER_NAME=oneleif-api-$DEPLOYMENT_GROUP_NAME
IMAGE_FILE=$(find .. -name "oneleif-api.*.tar.gz" | head -n 1)
NETWORK_NAME=olwebsite_default
RESTART_POLICY=on-failure:10
IMAGE_NAME=oneleif-api
ENV_FILE_PATH=/home/ubuntu/oneleif-env/$DEPLOYMENT_GROUP_NAME.env

case $DEPLOYMENT_GROUP_NAME in
"production")
PORT=8080
;;
"staging")
PORT=8081
;;
*)
PORT=8888
;;
esac

echo "Deploying oneleif-api ($DEPLOYMENT_GROUP_NAME) on port $PORT..."

# clean up
docker stop $CONTAINER_NAME || true
docker rm $CONTAINER_NAME || true

# load revision image
docker load -i $IMAGE_FILE

# start new version
docker run --env-file $ENV_FILE_PATH --name $CONTAINER_NAME --network $NETWORK_NAME --publish $PORT:80 --restart $RESTART_POLICY --detach $IMAGE_NAME
28 changes: 28 additions & 0 deletions Deploy/hooks/validate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

case $DEPLOYMENT_GROUP_NAME in
"production")
PORT=8080
;;
"staging")
PORT=8081
;;
*)
PORT=8888
;;
esac

# wait for application start on $PORT
while ! bash -c "echo >/dev/tcp/localhost/$PORT"; do sleep 1; done

HEALTH_CHECK_URL=localhost:$PORT/docs/index.html

STATUS_CODE=$(curl --write-out %{http_code} --silent --output /dev/null $HEALTH_CHECK_URL)

if [[ "$STATUS_CODE" -ne 200 ]] ; then
echo "Status code is not 200 ($STATUS_CODE), deployment failed"
exit 1
else
echo "Request successful: $STATUS_CODE"
exit 0
fi
4 changes: 2 additions & 2 deletions Docs/curlAPI.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ curl -X "POST" "https://oneleif-olwebsite.vapor.cloud/api/register" \
-H 'Content-Type: application/json; charset=utf-8' \
-H 'Cookie: vapor-session=QPh4F3PqH+CJKPrJ340FJQ==' \
-d $'{
"username": "Leif",
"email": "leif@oneleif.com",
"password": "somePassword!123"
}'

Expand All @@ -12,7 +12,7 @@ curl -X "POST" "https://oneleif-olwebsite.vapor.cloud/api/login" \
-H 'Content-Type: application/json; charset=utf-8' \
-H 'Cookie: vapor-session=QPh4F3PqH+CJKPrJ340FJQ==' \
-d $'{
"username": "Leif",
"email": "leif@oneleif.com",
"password": "somePassword!123"
}'

Expand Down
6 changes: 3 additions & 3 deletions Docs/postmanAPI.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"body": {
"mode": "raw",
"disabled": false,
"raw": "{\"username\":\"Leif\",\"password\":\"somePassword!123\"}"
"raw": "{\"email\":\"leif@oneleif.com\",\"password\":\"somePassword!123\"}"
},
"auth": null
},
Expand Down Expand Up @@ -80,7 +80,7 @@
"body": {
"mode": "raw",
"disabled": false,
"raw": "{\"username\":\"Leif\",\"password\":\"somePassword!123\"}"
"raw": "{\"email\":\"leif@oneleif.com\",\"password\":\"somePassword!123\"}"
},
"auth": null
},
Expand Down Expand Up @@ -530,4 +530,4 @@
"response": []
}
]
}
}
61 changes: 35 additions & 26 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"repositoryURL": "https://github.com/vapor/core.git",
"state": {
"branch": null,
"revision": "c64f63cb21631010952f7abfef719d376ab6a441",
"version": "3.9.1"
"revision": "10d33362a47fab03a067e78fb0791341d9c634fa",
"version": "3.9.3"
}
},
{
Expand All @@ -51,8 +51,17 @@
"repositoryURL": "https://github.com/vapor/fluent.git",
"state": {
"branch": null,
"revision": "b915c321c6f9e83743ee5efa35a30895e1b02e51",
"version": "3.2.0"
"revision": "783819d8838d15e1a05b459aa0fd1bde1e37ac26",
"version": "3.2.1"
}
},
{
"package": "FluentPostgreSQL",
"repositoryURL": "https://github.com/vapor/fluent-postgresql.git",
"state": {
"branch": null,
"revision": "8e3eb9d24d54ac58c8d04c194ad6b24f0b1b667e",
"version": "1.0.0"
}
},
{
Expand All @@ -69,53 +78,53 @@
"repositoryURL": "https://github.com/vapor/http.git",
"state": {
"branch": null,
"revision": "3808ed0401379b6e9f4a053f03090ea9d658caa9",
"version": "3.2.1"
"revision": "fba1329cd430e2f9a3f995b317b04f268d8b2978",
"version": "3.3.2"
}
},
{
"package": "Imperial",
"repositoryURL": "https://github.com/vapor-community/Imperial.git",
"state": {
"branch": null,
"revision": "b60f0e7b853290239441410f881ac2aad2f4f136",
"version": "0.13.0"
"revision": "06b932e4a9d8f94be4b82e0cabf64c6f08144663",
"version": "0.15.0"
}
},
{
"package": "JWT",
"repositoryURL": "https://github.com/vapor/jwt.git",
"state": {
"branch": null,
"revision": "2e225c722bf26407c1c4bd11d341e48759f46095",
"version": "3.0.0"
"revision": "4265f12b3f2585b8f5792ba6078c2cda0e4043a1",
"version": "3.1.1"
}
},
{
"package": "Leaf",
"repositoryURL": "https://github.com/vapor/leaf.git",
"package": "Multipart",
"repositoryURL": "https://github.com/vapor/multipart.git",
"state": {
"branch": null,
"revision": "d35f54cbac723e673f9bd5078361eea74049c8d7",
"version": "3.0.2"
"revision": "f063180d0b84832accd33194e06ed3c41f8609ac",
"version": "3.1.1"
}
},
{
"package": "Multipart",
"repositoryURL": "https://github.com/vapor/multipart.git",
"package": "PostgreSQL",
"repositoryURL": "https://github.com/vapor/postgresql.git",
"state": {
"branch": null,
"revision": "f919a01c4d10a281d6236a21b0b1d1759a72b8eb",
"version": "3.0.4"
"revision": "69ccf32e9164b207f9a773edac5433fe68ae3e5b",
"version": "1.5.0"
}
},
{
"package": "Routing",
"repositoryURL": "https://github.com/vapor/routing.git",
"state": {
"branch": null,
"revision": "626190ddd2bd9f967743b60ba6adaf90bbd2651c",
"version": "3.0.2"
"revision": "d76f339c9716785e5079af9d7075d28ff7da3d92",
"version": "3.1.0"
}
},
{
Expand Down Expand Up @@ -150,8 +159,8 @@
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "ba7970fe396e8198b84c6c1b44b38a1d4e2eb6bd",
"version": "1.14.1"
"revision": "8da5c5a4e6c5084c296b9f39dc54f00be146e0fa",
"version": "1.14.2"
}
},
{
Expand Down Expand Up @@ -195,8 +204,8 @@
"repositoryURL": "https://github.com/vapor/url-encoded-form.git",
"state": {
"branch": null,
"revision": "82d8d63bdb76b6dd8febe916c639ab8608dbbaed",
"version": "1.0.6"
"revision": "20f68fbe7fac006d4d0617ea4edcba033227359e",
"version": "1.1.0"
}
},
{
Expand All @@ -213,8 +222,8 @@
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "c86ada59b31c69f08a6abd4f776537cba48d5df6",
"version": "3.3.0"
"revision": "642f3d4d1f0eafad651c85524d0d1c698b55399f",
"version": "3.3.3"
}
},
{
Expand Down
7 changes: 3 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ let package = Package(
dependencies: [
// 💧 A server-side Swift web framework.
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
.package(url: "https://github.com/vapor/fluent-sqlite.git", from: "3.0.0"),
.package(url: "https://github.com/vapor/fluent-postgresql.git", from: "1.0.0"),
.package(url: "https://github.com/vapor/auth.git", from: "2.0.0"),
.package(url: "https://github.com/vapor-community/Imperial.git", from: "0.7.1"),
.package(url: "https://github.com/vapor/crypto.git", from: "3.0.0"),
.package(url: "https://github.com/vapor/leaf.git", from: "3.0.0")
],
targets: [
.target(name: "App", dependencies: ["FluentSQLite", "Vapor", "Leaf", "Authentication", "Imperial", "Crypto"]),
.target(name: "App", dependencies: ["FluentPostgreSQL", "Vapor", "Authentication", "Imperial", "Crypto"]),
.target(name: "Run", dependencies: ["App"]),
.testTarget(name: "AppTests", dependencies: ["App"])
.testTarget(name: "AppTests", dependencies: ["App"]),
]
)

Empty file removed Public/.gitkeep
Empty file.
Loading

0 comments on commit be7ab49

Please sign in to comment.