Skip to content

Commit

Permalink
Merge branch 'fs-117-ci-cd'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred Snyder committed Apr 8, 2023
2 parents 7d196c4 + 809fe02 commit 1f15a47
Show file tree
Hide file tree
Showing 61 changed files with 3,841 additions and 2,440 deletions.
3 changes: 3 additions & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VITE_ENDPOINT=http://100.111.214.8:8000/new_todo

# this ENV will load with npm run dev
3 changes: 3 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VITE_ENDPOINT=https://jchv4b5lpu5gea3lkowr5g2bei0tpnsl.lambda-url.eu-central-1.on.aws/new_todo

# this ENV will load with npm run build
33 changes: 33 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Deploy kbsb-demodash
run-name: ${{ github.actor }} is deploying.

on:
push:
branches:
- main

jobs:
deploy-frontend:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "make a node environment"
- run: echo "install pnpm"
- run: echo "build frontend"
- run: echo "copy dist folder to Scaleway"
- run: echo "purge Cloudflare cache"
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: |
echo "🍏 This job's status is ${{ job.status }}."
node-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: node --version
- run: npm --version
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dist/
downloads/
eggs/
.eggs/
lib/
# lib/
lib64/
parts/
sdist/
Expand Down
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# run development server
# accept traffic from all of the available interfaces (0.0.0.0)
dev:
pnpm run dev

# export/build for production
build:
pnpm run build

# preview/test the production export on 0.0.0.0:8000
preview:
cd dist; python -m http.server
20 changes: 10 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>kbsb-demodash</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
5 changes: 5 additions & 0 deletions lambdas/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# the shebang is ignored, but nice for editors

use nix
# source shell.sh
1 change: 1 addition & 0 deletions lambdas/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.9.16
3 changes: 3 additions & 0 deletions lambdas/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Readme

Lambda functions for the React front end widgets.
4 changes: 4 additions & 0 deletions lambdas/ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extend-select = ["C4", "SIM", "TCH"]
ignore = []
show-fixes = true
target-version = "py39"
9 changes: 9 additions & 0 deletions lambdas/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
name = "NIX";
buildInputs = [
pkgs.python39
pkgs.python39Packages.pip
];
}
1 change: 1 addition & 0 deletions lambdas/todos/.aws/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export AWS_PROFILE="YOUR_PROFILE_NAME"
9 changes: 9 additions & 0 deletions lambdas/todos/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
**/__pycache__/
.aws/
.venv/
events/
.env
.env.example
.envrc
Makefile
README.md
3 changes: 3 additions & 0 deletions lambdas/todos/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
AIRTABLE_API_KEY=api_key
AIRTABLE_BASE_ID=base_id
AIRTABLE_TABLE_NAME=table_name
15 changes: 15 additions & 0 deletions lambdas/todos/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# also source the .envrc of parent directory
source_up

#! The order is important
#! Sourcing the parent env after layout discards the python env

# set name for virtual environment
export VIRTUAL_ENV=.venv
# use the python env
layout python3

# source venv alias
alias svenv="source .venv/bin/activate"

# https://github.com/direnv/direnv/wiki/Python
15 changes: 15 additions & 0 deletions lambdas/todos/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM public.ecr.aws/lambda/python:3.9

# Copy function code
COPY requirements.txt ${LAMBDA_TASK_ROOT}
# COPY app.py ${LAMBDA_TASK_ROOT}
COPY . ${LAMBDA_TASK_ROOT}

# install dependencies
RUN pip install -r requirements.txt
# you don't need a venv in a container

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
# make sure that the Lambda container use the Mangum handler
# and that your local test environment uses the app:app FastAPI function
CMD [ "app.handler" ]
69 changes: 69 additions & 0 deletions lambdas/todos/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# include environment variables in every sub-shell
include .aws/.env
# all targets refer to tasks (instead of files)
.PHONY: *

ecr = 011809624525.dkr.ecr.eu-central-1.amazonaws.com
region = eu-central-1
image_name = kbsb-demodash-todos
docker_repo = kbsb-demodash
function_name = todos

######## FASTAPI local development

url_local = http://100.111.214.8:8000
url_docker = http://localhost:9000/2015-03-31/functions/function/invocations
url_lambda = https://jchv4b5lpu5gea3lkowr5g2bei0tpnsl.lambda-url.eu-central-1.on.aws

# app:app refers to app.py app function
api:
uvicorn --host 0.0.0.0 app:app --reload

pytest-local:
export TEST_URL="$(url_local)" && pytest -s -k test_endpoints
# `pytest -s` includes stdout

pytest-docker:
export TEST_URL="$(url_docker)"; export TEST_DOCKER=1 && pytest -s -k test_endpoints

pytest-prod:
export TEST_URL="$(url_lambda)" && pytest -s -k test_endpoints

######## AWS Authentication

login:
aws sso login

auth:
aws ecr get-login-password --region "$(region)" | docker login --username AWS --password-stdin "$(ecr)"

######## DOCKER tasks

build:
docker build -t "$(image_name)" .

run:
docker run --env-file .env -e LOG_LEVEL=DEBUG -p 9000:8080 "$(image_name)"

shell:
docker run -it --entrypoint /bin/bash -p 9000:8080 "$(image_name)"

tag:
docker tag "$(image_name)" "$(ecr)"/"$(docker_repo)"

push:
docker push "$(ecr)"/"$(docker_repo)"

lambda:
aws lambda update-function-code \
--region "$(region)" \
--function-name "$(function_name)" \
--image-uri "$(ecr)"/"$(docker_repo)":latest

invoke:
aws lambda invoke --function-name "$(function_name)" output.json

update: build tag push lambda

logs:
docker logs "$(image_name)"
34 changes: 34 additions & 0 deletions lambdas/todos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Readme

Post todos to Airtable.

Requirements:
- Python v3.9

> 📌 Make sure to set the AWS_PROFILE environment variable if you want to use the Makefile Docker targets!
```sh
pip install pytest
pip freeze > requirements.txt
# install dependencies from requirements
pip freeze -r requirements.txt

# update requirements
pip freeze
# run pytest tests
pytest
```

```sh
# setup Python environment
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

## FastAPI documentation page

```
http://100.111.214.8:8000/docs
https://jchv4b5lpu5gea3lkowr5g2bei0tpnsl.lambda-url.eu-central-1.on.aws/docs
```
Empty file added lambdas/todos/__init__.py
Empty file.
30 changes: 30 additions & 0 deletions lambdas/todos/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from fastapi import FastAPI
from mangum import Mangum
from routes import new_todo, list_todos
from fastapi.middleware.cors import CORSMiddleware

origins = ["*"]

#! set a variable so that the production origin is only demodash.kbsb.app

app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

app.include_router(new_todo.router)
app.include_router(list_todos.router)


@app.get("/")
def root():
return {
"body": {"message": "Hello todo!"},
}


handler = Mangum(app)
15 changes: 15 additions & 0 deletions lambdas/todos/events/docker-emulator-root.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# send API Gateway payload to the Lambda function

curl -XGET "http://localhost:9000/2015-03-31/functions/function/invocations" -d '
{
"version": "2.0",
"requestContext": {
"http": {
"method": "GET",
"path": "/",
"protocol": "HTTP/1.1",
"sourceIp": "192.0.2.1"
}
}
}
'
20 changes: 20 additions & 0 deletions lambdas/todos/events/docker-emulator.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# send API Gateway payload to the Lambda function

# the Lambda emulator expects escaped JSON strings as body
# this is not a problem when running locally with FastAPI
# or when running in production

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '
{
"version": "2.0",
"requestContext": {
"http": {
"method": "POST",
"path": "/new_todo",
"protocol": "HTTP/1.1",
"sourceIp": "192.0.2.1"
}
},
"body": "{\"description\": \"Lorem ipsum\",\"label\": \"normal\",\"done\": true}"
}
'
25 changes: 25 additions & 0 deletions lambdas/todos/events/endpoint-todos.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# production
# @URL = https://jchv4b5lpu5gea3lkowr5g2bei0tpnsl.lambda-url.eu-central-1.on.aws

# local dev server
@URL = http://100.111.214.8:8000

# https://github.com/Huachao/vscode-restclient/blob/master/README.md#variables

### endpoint: /

GET {{URL}}/ HTTP/1.1.
content-type: application/json

{}

### endpoint: /new_todo

POST {{URL}}/new_todo HTTP/1.1.
content-type: application/json

{
"description": "Lorem ipsum",
"label": "normal",
"done": true
}
Empty file added lambdas/todos/lib/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions lambdas/todos/lib/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os
from dotenv import load_dotenv

# load environment variables
load_dotenv()

test = "test222222"

api_key = os.getenv("AIRTABLE_API_KEY")
base_id = os.getenv("AIRTABLE_BASE_ID")
table_name = os.getenv("AIRTABLE_TABLE_NAME")

0 comments on commit 1f15a47

Please sign in to comment.