-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2d5249
commit 04aecf0
Showing
1 changed file
with
55 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,67 +10,76 @@ parameters: | |
enum: ['dev', 'prod'] | ||
default: 'dev' | ||
|
||
executors: | ||
node: | ||
docker: | ||
- image: cimg/node:16.16.0 | ||
orbs: | ||
aws-cli: circleci/[email protected] | ||
|
||
jobs: | ||
build: | ||
machine: true | ||
machine: | ||
image: ubuntu-2004:current | ||
working_directory: ~/map-sorter-api | ||
environment: | ||
DOCKER_IMAGE: high10hunter/map-sorter-api | ||
DOCKER_TAG: latest | ||
steps: | ||
- checkout | ||
- restore_cache: | ||
name: Restore NPM cache | ||
keys: | ||
- npm-cache-{{ checksum "package-lock.json" }} | ||
- npm-cache- | ||
- run: | ||
name: Update NPM version | ||
command: npm install -g npm@latest | ||
name: Login to DockerHub | ||
command: echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin | ||
|
||
- save_cache: | ||
name: Save NPM cache | ||
key: npm-cache-{{ checksum "package-lock.json" }} | ||
paths: | ||
- ~/.cache/npm | ||
- ./node_modules | ||
- run: | ||
name: Install dependencies | ||
command: npm ci | ||
- run: | ||
name: Run tests | ||
command: npm run test | ||
- run: | ||
name: Build | ||
command: npm run build | ||
name: Build and push image | ||
command: | | ||
echo "Building image" | ||
docker build -t $DOCKER_IMAGE:$DOCKER_TAG . | ||
echo "Pushing image" | ||
docker push $DOCKER_IMAGE:$DOCKER_TAG | ||
echo "::set-output name=image::$DOCKER_IMAGE:$DOCKER_TAG" | ||
deploy: | ||
executor: node | ||
machine: | ||
image: ubuntu-2004:current | ||
working_directory: ~/map-sorter-api | ||
environment: | ||
DOCKER_IMAGE: high10hunter/map-sorter-api | ||
DOCKER_TAG: latest | ||
steps: | ||
- aws-cli/setup: | ||
role-arn: 'arn:aws:iam::${AWS_ACCOUNT_ID}:role/circleci-oidc-provider-aws' | ||
aws-region: AWS_REGION | ||
- checkout | ||
- restore_cache: | ||
name: Restore NPM cache | ||
keys: | ||
- npm-cache-{{ checksum "package-lock.json" }} | ||
- npm-cache- | ||
|
||
- add_ssh_keys: | ||
fingerprints: | ||
- 'SHA256:+2S1UpsWHcjQu4ur9jpqF16GQa9NrND9qbFNU6pzB64' | ||
|
||
- run: | ||
name: Install dependencies | ||
command: npm ci | ||
- save_cache: | ||
name: Save NPM cache | ||
key: npm-cache-{{ checksum "package-lock.json" }} | ||
paths: | ||
- ~/.cache/npm | ||
- ./node_modules | ||
name: Create .env file and sync to dev server | ||
command: | | ||
cd tools | ||
aws ssm get-parameters-by-path --path "/dev" --with-decryption --recursive > mapsorter_env.json | ||
./convert_ssm_params_to_dotenv.sh mapsorter_env.json | ||
scp -oStrictHostKeyChecking=no -v -r .env $SSH_USER@$SSH_HOST:/home/gdsc/map-sorter-api/.env | ||
- run: | ||
name: 'Deploy to << pipeline.parameters.build_stage >>' | ||
name: SSH into dev server and deploy | ||
command: | | ||
curl -i -H "X-Secret-Signature: $WEBHOOK_SECRET" \ | ||
-H "Content-Type: application/json" \ | ||
-X POST $WEBHOOK_URL/redeploy | ||
echo "SSH into dev server" | ||
ssh -o StrictHostKeyChecking=no -v $SSH_USER@$SSH_HOST " | ||
su - gdsc -c ' | ||
cd map-sorter-api && | ||
echo \"$DOCKER_PASSWORD\" | docker login -u \"$DOCKER_USERNAME\" --password-stdin && | ||
docker pull $DOCKER_IMAGE:$DOCKER_TAG && | ||
docker container stop map-sorter-api || true && | ||
docker container rm map-sorter-api || true && | ||
docker run --log-opt max-size=10m --log-opt max-file=3 --log-opt compress=true \ | ||
--name map-sorter-api -d --env-file .env $DOCKER_IMAGE:$DOCKER_TAG && | ||
docker image prune --all -f | ||
rm .env | ||
' | ||
" | ||
workflows: | ||
version: 3 | ||
|
@@ -94,6 +103,7 @@ workflows: | |
jobs: | ||
- build | ||
- deploy: | ||
context: aws | ||
requires: | ||
- build | ||
filters: | ||
|
@@ -110,6 +120,7 @@ workflows: | |
jobs: | ||
- build | ||
- deploy: | ||
context: aws | ||
requires: | ||
- build | ||
filters: | ||
|