Skip to content

Commit 978161b

Browse files
committed
setup the application CI + revamp the Makefile (#8)
* revamp the makefile * add a gha workflow * add workflow to keep the ci alive * remove logs * add vs code related files * add ci back
1 parent 6f460e3 commit 978161b

File tree

10 files changed

+1140
-640
lines changed

10 files changed

+1140
-640
lines changed

.github/workflows/ci.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Setup infrastructure using CDK
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- 'README.md'
7+
branches:
8+
- main
9+
pull_request:
10+
branches:
11+
- main
12+
schedule:
13+
# “At 00:00 on Sunday.”
14+
- cron: "0 0 * * 0"
15+
workflow_dispatch:
16+
17+
jobs:
18+
cdk:
19+
name: Run Integration Tests
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
25+
- name: Setup Node.js
26+
uses: actions/setup-node@v3
27+
with:
28+
node-version: 22
29+
30+
- name: Start LocalStack
31+
uses: LocalStack/setup-localstack@main
32+
env:
33+
LOCALSTACK_AUTH_TOKEN: ${{ secrets.LOCALSTACK_AUTH_TOKEN }}
34+
with:
35+
use-pro: 'true'
36+
37+
- name: Install CDK
38+
run: |
39+
npm install -g aws-cdk-local aws-cdk
40+
cdklocal --version
41+
42+
- name: Install dependencies
43+
run: make install
44+
45+
- name: Deploy the CDK stack
46+
run: make deploy
47+
48+
- name: Run the tests
49+
run: make test
50+
51+
- name: Show LocalStack logs
52+
if: always()
53+
run: localstack logs
54+
55+
- name: Send a Slack notification
56+
if: failure() || github.event_name != 'pull_request'
57+
uses: ravsamhq/notify-slack-action@v2
58+
with:
59+
status: ${{ job.status }}
60+
token: ${{ secrets.GITHUB_TOKEN }}
61+
notification_title: "{workflow} has {status_message}"
62+
message_format: "{emoji} *{workflow}* {status_message} in <{repo_url}|{repo}>"
63+
footer: "Linked Repo <{repo_url}|{repo}> | <{run_url}|View Workflow run>"
64+
notify_when: "failure"
65+
env:
66+
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
67+
68+
- name: Generate a Diagnostic Report
69+
if: failure()
70+
run: |
71+
curl -s localhost:4566/_localstack/diagnose | gzip -cf > diagnose.json.gz
72+
73+
- name: Upload the Diagnostic Report
74+
if: failure()
75+
uses: actions/upload-artifact@v4
76+
with:
77+
name: diagnose.json.gz
78+
path: ./diagnose.json.gz

.github/workflows/keepalive.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Keep Alive
2+
on:
3+
schedule:
4+
- cron: "0 0 * * *"
5+
jobs:
6+
main-job:
7+
name: Main Job
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
workflow-keepalive:
12+
if: github.event_name == 'schedule'
13+
runs-on: ubuntu-latest
14+
permissions:
15+
actions: write
16+
steps:
17+
- uses: liskin/gh-workflow-keepalive@v1

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
.vscode
21
.idea
32
iac/awscdk/output.json
4-
.env-gdc-local
3+
logs.txt

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"address": "127.0.0.1",
6+
"localRoot": "${workspaceFolder}",
7+
"name": "Attach to Remote Node.js",
8+
"port": 9229,
9+
"remoteRoot": "/app",
10+
"request": "attach",
11+
"type": "node",
12+
"preLaunchTask": "Wait Remote Debugger Server"
13+
}
14+
]
15+
}

.vscode/tasks.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Wait Remote Debugger Server",
6+
"type": "shell",
7+
"command": "while [[ -z $(docker ps | grep :9229) ]]; do sleep 1; done; sleep 1;"
8+
}
9+
]
10+
}

Makefile

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ SHELL := /bin/bash
33
-include .env-gdc-local
44

55
CDIR = cd iac/awscdk
6+
TDIR = cd tests
67

78
export APP_NAME?=ecslb
89
export ENFORCE_IAM?=0
@@ -12,42 +13,70 @@ usage: ## Show this help in table format
1213
@echo "|------------------------|-------------------------------------------------------------------|"
1314
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/:.*##\s*/##/g' | awk -F'##' '{ printf "| %-22s | %-65s |\n", $$1, $$2 }'
1415

16+
check: ## Check if all required prerequisites are installed
17+
@command -v docker > /dev/null 2>&1 || { echo "Docker is not installed. Please install Docker and try again."; exit 1; }
18+
@command -v node > /dev/null 2>&1 || { echo "Node.js is not installed. Please install Node.js and try again."; exit 1; }
19+
@command -v aws > /dev/null 2>&1 || { echo "AWS CLI is not installed. Please install AWS CLI and try again."; exit 1; }
20+
@command -v localstack > /dev/null 2>&1 || { echo "LocalStack is not installed. Please install LocalStack and try again."; exit 1; }
21+
@command -v cdk > /dev/null 2>&1 || { echo "CDK is not installed. Please install CDK and try again."; exit 1; }
22+
@command -v cdklocal > /dev/null 2>&1 || { echo "cdklocal is not installed. Please install cdklocal and try again."; exit 1; }
23+
@echo "All required prerequisites are available."
24+
25+
install: ## Install NPM dependencies
26+
@${CDIR}; if [ ! -d "node_modules" ]; then \
27+
echo "Installing NPM dependencies..."; \
28+
npm install; \
29+
else \
30+
echo "NPM dependencies for CDK project already installed."; \
31+
fi
32+
@${TDIR}; if [ ! -d "node_modules" ]; then \
33+
echo "Installing NPM dependencies..."; \
34+
npm install; \
35+
else \
36+
echo "NPM dependencies for tests already installed."; \
37+
fi
38+
39+
deploy: ## Bootstrap and deploy the CDK app on LocalStack
40+
${CDIR}; cdklocal bootstrap; cdklocal deploy --outputs-file ./output.json --json --require-approval never
41+
42+
deploy-aws: ## Bootstrap and deploy the CDK app on AWS
43+
${CDIR}; cdk bootstrap && \
44+
cdk deploy --outputs-file ./output.json --json
45+
46+
destroy: ## Destroy the deployed CDK stack on LocalStack
47+
${CDIR}; cdklocal destroy
1548

16-
deploy: ## Bootstrap and deploy the CDK app to AWS
17-
${CDIR}; cdk bootstrap; cdk deploy --outputs-file ./output.json --json
18-
19-
destroy: ## Destroy the deployed CDK stack in AWS
49+
destroy-aws: ## Destroy the deployed CDK stack on AWS
2050
${CDIR}; cdk destroy
2151

22-
destroy-local: ## Destroy the deployed CDK stack locally
23-
${CDIR}; cdklocal destroy
24-
25-
deploy-local: ## Bootstrap and deploy the CDK app locally
26-
${CDIR}; cdklocal bootstrap && \
27-
cdklocal deploy --outputs-file ./output.json --json --require-approval never
52+
test: ## Run integration tests on LocalStack
53+
cd tests && LOCALSTACK=1 npm run test
2854

29-
test: ## Run integration tests
55+
test-aws: ## Run integration tests on AWS
3056
cd tests && npm run test
3157

32-
test-local: ## Run integration tests
33-
cd tests && LOCALSTACK=1 npm run test
34-
35-
curl-local:
58+
curl: ## Curl the LocalStack service load balancer
3659
curl $(shell cat iac/awscdk/output.json | jq '.RepoStack.localstackserviceslb')
3760

38-
curl-aws:
61+
curl-aws: ## Curl the AWS service load balancer
3962
curl $(shell cat iac/awscdk/output.json | jq '.RepoStack.serviceslb')
4063

41-
install: ## Install npm dependencies
42-
${CDIR}; npm install
43-
64+
start: ## Start LocalStack
65+
@echo "Starting LocalStack..."
66+
@LOCALSTACK_AUTH_TOKEN=$(LOCALSTACK_AUTH_TOKEN) localstack start -d
67+
@echo "LocalStack started successfully."
4468

45-
start-localstack:
46-
cd devops-tooling && docker compose -p $(APP_NAME) up
69+
stop: ## Stop LocalStack
70+
@echo "Stopping LocalStack..."
71+
@localstack stop
72+
@echo "LocalStack stopped successfully."
4773

48-
stop-localstack:
49-
cd devops-tooling && docker compose -p $(APP_NAME) down
74+
ready: ## Make sure the LocalStack container is up
75+
@echo Waiting on the LocalStack container...
76+
@localstack wait -t 30 && echo LocalStack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)
5077

78+
logs: ## Save the logs in a separate file
79+
@localstack logs > logs.txt
5180

5281
PKG_SUB_DIRS := $(dir $(shell find . -type d -name node_modules -prune -o -type d -name "venv*" -prune -o -type f -name package.json -print))
5382

@@ -56,4 +85,4 @@ update-deps: $(PKG_SUB_DIRS)
5685
pushd $$i && ncu -u && npm install && popd; \
5786
done
5887

59-
.PHONY: usage install test test-local deploy destroy deploy-local destroy-local bootstrap-local update-deps start-localstack stop-localstack curl-local curl-aws
88+
.PHONY: usage install check start deploy curl test destroy logs deploy-aws curl-aws test-aws destroy-aws update-deps

src/app/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const http = require('http')
22

33
const server = http.createServer((req, res) => {
44
res.setHeader('Content-Type', 'application/json')
5-
res.end(JSON.stringify({message: "Hi Localstack!"}))
5+
res.end(JSON.stringify({message: "Hi LocalStack!"}))
66
})
77

88
const PORT = 3000

tests/integration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('User API', function () {
2121

2222
// Assert the body
2323
expect(response.data).to.be.an('object')
24-
expect(response.data).to.have.property('message', "Hello, Welcome to Localstack!")
24+
expect(response.data).to.have.property('message', "Hi LocalStack!")
2525
expect(response.data.message).to.be.a('string')
2626
})
2727
})

0 commit comments

Comments
 (0)