|
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 |
25 | 2 |
|
26 | 3 | on: |
27 | 4 | push: |
28 | | - branches: |
29 | | - - dev-release |
30 | | - |
31 | | -name: Deploy to Amazon ECS |
| 5 | + branches: [ dev-release ] |
32 | 6 |
|
33 | 7 | jobs: |
34 | | - deploy: |
35 | | - name: Deploy |
36 | | - runs-on: [self-hosted, linux, dev, prod] |
37 | | - |
| 8 | + build-and-deploy: |
| 9 | + runs-on: ubuntu-latest |
38 | 10 | 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- |
41 | 26 |
|
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 |
44 | 29 | 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 |
48 | 37 |
|
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 |
52 | 50 |
|
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 |
55 | 53 | 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) |
59 | 57 | 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 |
69 | 63 | 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" |
73 | 70 |
|
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 |
76 | 73 | 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 |
0 commit comments