Skip to content

Commit 45e5187

Browse files
committed
feat: add new dev workflow
1 parent 025256b commit 45e5187

3 files changed

Lines changed: 114 additions & 65 deletions

File tree

.github/workflows/dev-release.yml

Lines changed: 79 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,94 @@
1-
# This workflow will build and push a new container image to Amazon ECR,
2-
# and then will deploy a new task definition to Amazon ECS, when a release is created
3-
#
4-
# To use this workflow, you will need to complete the following set-up steps:
5-
#
6-
# 1. Create an ECR repository to store your images.
7-
# For example: `aws ecr create-repository --repository-name my-ecr-repo --region us-east-2`.
8-
# Replace the value of `ECR_REPOSITORY` in the workflow below with your repository's name.
9-
# Replace the value of `aws-region` in the workflow below with your repository's region.
10-
#
11-
# 2. Create an ECS task definition, an ECS cluster, and an ECS service.
12-
# For example, follow the Getting Started guide on the ECS console:
13-
# https://us-east-2.console.aws.amazon.com/ecs/home?region=us-east-2#/firstRun
14-
# Replace the values for `service` and `cluster` in the workflow below with your service and cluster names.
15-
#
16-
# 3. Store your ECS task definition as a JSON file in your repository.
17-
# The format should follow the output of `aws ecs register-task-definition --generate-cli-skeleton`.
18-
# Replace the value of `task-definition` in the workflow below with your JSON file's name.
19-
# Replace the value of `container-name` in the workflow below with the name of the container
20-
# in the `containerDefinitions` section of the task definition.
21-
#
22-
# 4. Store an IAM user access key in GitHub Actions secrets named `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.
23-
# See the documentation for each action used below for the recommended IAM policies for this IAM user,
24-
# and best practices on handling the access key credentials.
1+
name: Deploy App
252

263
on:
274
push:
28-
branches:
29-
- dev-release
30-
31-
name: Deploy to Amazon ECS
5+
branches: [ dev-release ]
326

337
jobs:
34-
deploy:
35-
name: Deploy
36-
runs-on: [self-hosted, linux, dev, prod]
37-
8+
build-and-deploy:
9+
runs-on: ubuntu-latest
3810
steps:
39-
- name: Checkout
40-
uses: actions/checkout@v2
11+
- name: Checkout code
12+
uses: actions/checkout@v4
13+
14+
# Configure Docker Buildx to allow efficient caching
15+
- name: Set up Docker Buildx
16+
uses: docker/setup-buildx-action@v3
17+
18+
# Cache Docker layers to speed up future builds
19+
- name: Cache Docker layers
20+
uses: actions/cache@v3
21+
with:
22+
path: /tmp/.buildx-cache
23+
key: ${{ runner.os }}-buildx-${{ github.sha }}
24+
restore-keys: |
25+
${{ runner.os }}-buildx-
4126
42-
- name: Configure AWS credentials
43-
uses: aws-actions/configure-aws-credentials@v1
27+
- name: Build and Export Image
28+
uses: docker/build-push-action@v5
4429
with:
45-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
46-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
47-
aws-region: eu-central-1
30+
context: .
31+
file: Dockerfile
32+
platforms: linux/amd64
33+
tags: superdapp-python-api-dev:latest
34+
outputs: type=docker,dest=/tmp/image.tar
35+
cache-from: type=local,src=/tmp/.buildx-cache
36+
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
4837

49-
- name: Login to Amazon ECR
50-
id: login-ecr
51-
uses: aws-actions/amazon-ecr-login@v1
38+
# Compress the image for faster upload and move to root folder
39+
- name: Gzip Image and Move
40+
run: |
41+
gzip -9 /tmp/image.tar
42+
mv /tmp/image.tar.gz ./
43+
44+
# Install AWS CLI (For local act runner)
45+
- name: Install AWS CLI (For local act runner)
46+
run: |
47+
if ! command -v aws &> /dev/null; then
48+
sudo apt-get update && sudo apt-get install -y awscli
49+
fi
5250
53-
- name: Build, tag, and push image to Amazon ECR
54-
id: build-image
51+
# Download the environment variables from Hetzner Object Storage
52+
- name: Download .env from Hetzner S3
5553
env:
56-
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
57-
ECR_REPOSITORY: superdapp-python-dev
58-
IMAGE_TAG: latest
54+
AWS_ACCESS_KEY_ID: ${{ secrets.HETZNER_ACCESS_KEY }}
55+
AWS_SECRET_ACCESS_KEY: ${{ secrets.HETZNER_SECRET_KEY }}
56+
AWS_REGION: eu-central-1 # Change to your preferred Hetzner region (e.g., fsn1, nbg1)
5957
run: |
60-
# Build a docker container and
61-
# push it to ECR so that it can
62-
# be deployed to ECS.
63-
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
64-
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
65-
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
66-
- name: Fill in the new image ID in the Amazon ECS task definition
67-
id: task-def
68-
uses: aws-actions/amazon-ecs-render-task-definition@v1
58+
aws s3 cp s3://superdapp-env-files/superdapp-python-dev.env .env \
59+
--endpoint-url https://hel1.your-objectstorage.com
60+
61+
- name: Transfer Files to VPS
62+
uses: appleboy/scp-action@v0.1.7
6963
with:
70-
task-definition: ecs-dev-release-task-definition.json
71-
container-name: superdapp-python-dev
72-
image: ${{ steps.build-image.outputs.image }}
64+
host: ${{ secrets.DEV_HOST }}
65+
username: ${{ secrets.USER }}
66+
key: ${{ secrets.SSH_DEV_PRIVATE_KEY }}
67+
# Send the image, the updated docker-compose.dev.yml AND the downloaded .env file
68+
source: "image.tar.gz,docker-compose.dev.yml,.env"
69+
target: "~/superdapp-python-api-dev"
7370

74-
- name: Deploy Amazon ECS task definition
75-
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
71+
- name: Remote Deploy
72+
uses: appleboy/ssh-action@v1.0.3
7673
with:
77-
task-definition: ${{ steps.task-def.outputs.task-definition }}
78-
service: superdapp-python-dev
79-
cluster: dev
80-
wait-for-service-stability: true
74+
host: ${{ secrets.DEV_HOST }}
75+
username: ${{ secrets.USER }}
76+
key: ${{ secrets.SSH_DEV_PRIVATE_KEY }}
77+
script: |
78+
cd ~/superdapp-python-api-dev
79+
80+
# Load the new image
81+
gunzip -c image.tar.gz | docker load
82+
83+
# Restart the service leveraging the custom file name
84+
docker compose -f docker-compose.dev.yml up -d
85+
86+
# Cleanup: Remove the tar file and old/dangling images
87+
rm image.tar.gz
88+
docker image prune -f
89+
90+
# Buildx cache management (cleanup required for GitHub Actions)
91+
- name: Move cache
92+
run: |
93+
rm -rf /tmp/.buildx-cache
94+
mv /tmp/.buildx-cache-new /tmp/.buildx-cache

docker-compose.dev.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
services:
2+
api:
3+
image: superdapp-python-api-dev:latest
4+
container_name: python-api-dev
5+
restart: unless-stopped
6+
env_file:
7+
- .env
8+
networks:
9+
- traefik-net
10+
labels:
11+
- "traefik.enable=true"
12+
- "traefik.http.routers.python-api.rule=Host(`python-api.superdapp.dev`)"
13+
- "traefik.http.routers.python-api.entrypoints=websecure"
14+
- "traefik.http.routers.python-api.tls=true"
15+
- "traefik.http.routers.python-api.tls.certresolver=letsencrypt"
16+
- "traefik.http.services.python-api.loadbalancer.server.port=80"
17+
- "traefik.docker.network=traefik-net"
18+
deploy:
19+
resources:
20+
limits:
21+
cpus: '0.50'
22+
memory: 2048M
23+
reservations:
24+
cpus: '0.25'
25+
memory: 512M
26+
27+
networks:
28+
traefik-net:
29+
external: true
30+
name: traefik-net

main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
]
3333

3434
app = FastAPI()
35+
36+
@app.get("/")
37+
async def root():
38+
return {"status": "ok", "message": "SuperDapp Python API is running"}
39+
3540
# Initialize logging
3641
LOGFILE_PATH = os.path.join(os.path.dirname(
3742
os.path.abspath(__file__)), 'app.log')

0 commit comments

Comments
 (0)