Skip to content

Commit

Permalink
deploy on aws
Browse files Browse the repository at this point in the history
  • Loading branch information
khanzadimahdi committed Jan 5, 2025
1 parent 8282e84 commit 7c02a3d
Show file tree
Hide file tree
Showing 18 changed files with 201 additions and 1,038 deletions.
74 changes: 74 additions & 0 deletions .github/actions/docker-build/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: 'Docker Build Action'
description: 'Builds a Docker image using a specified Dockerfile and context'

inputs:
context:
description: 'The Docker build context (path to the directory containing the Dockerfile)'
required: true
default: '.'
dockerfile:
description: 'The path to the Dockerfile (relative to the context)'
required: true
default: 'Dockerfile'
target:
description: 'The build target'
required: false
image-name:
description: 'The name of the image to build'
required: true
push:
description: 'Determines if the built image should be pushed'
required: true
default: 'no'
container-registry:
description: 'container registry address (example: ghcr.io)'
required: false
container-registry-username:
description: 'container registry username'
required: false
container-registry-password:
description: 'container registry password'
required: false

runs:
using: 'composite'

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
if: ${{ inputs.push == 'true' }}
with:
logout: false
registry: ${{ inputs.container-registry }}
username: ${{ inputs.container-registry-username }}
password: ${{ inputs.container-registry-password }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Extract metadata (tags, labels) for Docker image
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ inputs.container-registry }}/${{ github.repository_owner }}/${{ inputs.image-name }}
tags: |
type=raw,value=latest,priority=200,enable={{is_default_branch}}
type=sha,enable=true,priority=100,prefix=,suffix=,format=short
- name: Build image and push (optional)
uses: docker/build-push-action@v6
with:
platforms: linux/amd64,linux/arm64
push: ${{ inputs.push == 'true' }}
context: ${{ inputs.context }}
file: ${{ inputs.dockerfile }}
target: ${{ inputs.target }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
97 changes: 74 additions & 23 deletions .github/workflows/backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ env:
REGISTRY: ghcr.io
IMAGE_NAME: backend

EC2_SSH_ADDRESS: ${{ secrets.EC2_SSH_ADDRESS }}
EC2_SSH_ENDPOINT: ${{ secrets.EC2_SSH_USER }}@${{ secrets.EC2_SSH_ADDRESS }}

jobs:
ci:
runs-on: ubuntu-latest
Expand All @@ -35,16 +38,15 @@ jobs:
run: |
go test ./... -v -race -cover
- name: Provide image name and version
run: |
IMAGE_ID=$(echo $REGISTRY/${{ github.repository_owner }}/$IMAGE_NAME | tr '[A-Z]' '[a-z]')
IMAGE_VERSION=${{ github.sha }}
echo "IMAGE_ID=$IMAGE_ID" >> "$GITHUB_ENV"
echo "IMAGE_VERSION=$IMAGE_VERSION" >> "$GITHUB_ENV"
- name: Build image
run: |
docker build . --file Dockerfile --target production --tag $IMAGE_ID:$IMAGE_VERSION --tag $IMAGE_ID:latest
uses: ./.github/actions/docker-build
with:
context: ./backend
dockerfile: ./backend/Dockerfile
image-name: ${{ env.IMAGE_NAME }}
target: production
push: false
container-registry: ${{ env.REGISTRY }}

cd:
runs-on: ubuntu-latest
Expand All @@ -62,21 +64,70 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Provide image name and version
- name: Build and push image
uses: ./.github/actions/docker-build
with:
context: ./backend
dockerfile: ./backend/Dockerfile
image-name: ${{ env.IMAGE_NAME }}
target: production
push: true
container-registry: ${{ env.REGISTRY }}
container-registry-username: ${{ github.actor }}
container-registry-password: ${{ secrets.GITHUB_TOKEN }}

- name: Deploy services
run: |
IMAGE_ID=$(echo $REGISTRY/${{ github.repository_owner }}/$IMAGE_NAME | tr '[A-Z]' '[a-z]')
IMAGE_VERSION=${{ github.sha }}
echo "IMAGE_ID=$IMAGE_ID" >> "$GITHUB_ENV"
echo "IMAGE_VERSION=$IMAGE_VERSION" >> "$GITHUB_ENV"
# Setup ssh key
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ~/ec2-key.pem
chmod 400 ~/ec2-key.pem
- name: Build image
run: |
docker build . --file Dockerfile --target production --tag $IMAGE_ID:$IMAGE_VERSION --tag $IMAGE_ID:latest
mkdir -p ~/.ssh
ssh-keyscan -H $EC2_SSH_ADDRESS >> ~/.ssh/known_hosts
- name: Log in to registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# Ensure remote directory exists
ssh -q -i ~/ec2-key.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $EC2_SSH_ENDPOINT > /dev/null 2>&1 << 'EOF'
sudo mkdir -p /tmp/deployment_backend
sudo chown ${{ secrets.EC2_SSH_USER }}:${{ secrets.EC2_SSH_USER }} /tmp/deployment_backend
- name: Push image
run: |
docker push $IMAGE_ID:$IMAGE_VERSION
docker push $IMAGE_ID:latest
EOF
# Copy files
scp -q -i ~/ec2-key.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r ./compose.app.yaml $EC2_SSH_ENDPOINT:/tmp/deployment_backend/ > /dev/null 2>&1
# Connect and deploy services
ssh -q -i ~/ec2-key.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $EC2_SSH_ENDPOINT > /dev/null 2>&1 << 'EOF'
export MONGO_USERNAME="${{ secrets.MONGO_USERNAME }}"
export MONGO_PASSWORD="${{ secrets.MONGO_PASSWORD }}"
export BACKEND_NATS_URL="${{ secrets.BACKEND_NATS_URL }}"
export BACKEND_PRIVATE_KEY="${{ secrets.BACKEND_PRIVATE_KEY }}"
export BACKEND_MONGO_HOST="mongodb"
export BACKEND_MONGO_PORT="27017"
export BACKEND_MONGO_SCHEME="mongodb"
export BACKEND_MONGO_DATABASE_NAME="${{ secrets.BACKEND_MONGO_DATABASE_NAME }}"
export BACKEND_MONGO_USERNAME="${{ secrets.MONGO_USERNAME }}"
export BACKEND_MONGO_PASSWORD="${{ secrets.MONGO_PASSWORD }}"
export BACKEND_MAIL_SMTP_PASSWORD="${{ secrets.BACKEND_MAIL_SMTP_PASSWORD }}"
export BACKEND_MAIL_SMTP_HOST="${{ secrets.BACKEND_MAIL_SMTP_HOST }}"
export BACKEND_MAIL_SMTP_FROM="${{ secrets.BACKEND_MAIL_SMTP_FROM }}"
export BACKEND_MAIL_SMTP_USERNAME="${{ secrets.BACKEND_MAIL_SMTP_USERNAME }}"
export BACKEND_MAIL_SMTP_PORT="${{ secrets.BACKEND_MAIL_SMTP_PORT }}"
export BACKEND_S3_ENDPOINT="${{ secrets.BACKEND_S3_ENDPOINT }}"
export BACKEND_S3_SECRET_KEY="${{ secrets.BACKEND_S3_SECRET_KEY }}"
export BACKEND_S3_ACCESS_KEY="${{ secrets.BACKEND_S3_ACCESS_KEY }}"
export BACKEND_S3_USE_SSL="${{ secrets.BACKEND_S3_USE_SSL }}"
export BACKEND_S3_BUCKET_NAME="${{ secrets.BACKEND_S3_BUCKET_NAME }}"
export APP_IMAGE="${{ secrets.APP_IMAGE }}"
# Run Docker Compose
cd /tmp/deployment_backend/
docker compose -f compose.app.yaml --project-name app up --pull always --detach
sudo rm -rf /tmp/deployment_backend
EOF
78 changes: 51 additions & 27 deletions .github/workflows/frontend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ env:
REGISTRY: ghcr.io
IMAGE_NAME: frontend

EC2_SSH_ADDRESS: ${{ secrets.EC2_SSH_ADDRESS }}
EC2_SSH_ENDPOINT: ${{ secrets.EC2_SSH_USER }}@${{ secrets.EC2_SSH_ADDRESS }}

jobs:
ci:
runs-on: ubuntu-latest
Expand All @@ -26,20 +29,15 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- uses: actions/setup-node@v3
with:
node-version: "18"

- name: Provide image name and version
run: |
IMAGE_ID=$(echo $REGISTRY/${{ github.repository_owner }}/$IMAGE_NAME | tr '[A-Z]' '[a-z]')
IMAGE_VERSION=${{ github.sha }}
echo "IMAGE_ID=$IMAGE_ID" >> "$GITHUB_ENV"
echo "IMAGE_VERSION=$IMAGE_VERSION" >> "$GITHUB_ENV"
- name: Build image
run: |
docker build . --file Dockerfile --target production --tag $IMAGE_ID:$IMAGE_VERSION --tag $IMAGE_ID:latest
uses: ./.github/actions/docker-build
with:
context: ./frontend
dockerfile: ./frontend/Dockerfile
image-name: ${{ env.IMAGE_NAME }}
target: production
push: false
container-registry: ${{ env.REGISTRY }}

cd:
runs-on: ubuntu-latest
Expand All @@ -57,21 +55,47 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Provide image name and version
- name: Build and push image
uses: ./.github/actions/docker-build
with:
context: ./frontend
dockerfile: ./frontend/Dockerfile
image-name: ${{ env.IMAGE_NAME }}
target: production
push: true
container-registry: ${{ env.REGISTRY }}
container-registry-username: ${{ github.actor }}
container-registry-password: ${{ secrets.GITHUB_TOKEN }}

- name: Deploy services
run: |
IMAGE_ID=$(echo $REGISTRY/${{ github.repository_owner }}/$IMAGE_NAME | tr '[A-Z]' '[a-z]')
IMAGE_VERSION=${{ github.sha }}
echo "IMAGE_ID=$IMAGE_ID" >> "$GITHUB_ENV"
echo "IMAGE_VERSION=$IMAGE_VERSION" >> "$GITHUB_ENV"
# Setup ssh key
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ~/ec2-key.pem
chmod 400 ~/ec2-key.pem
- name: Build image
run: |
docker build . --file Dockerfile --target production --tag $IMAGE_ID:$IMAGE_VERSION --tag $IMAGE_ID:latest
mkdir -p ~/.ssh
ssh-keyscan -H $EC2_SSH_ADDRESS >> ~/.ssh/known_hosts
- name: Log in to registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# Ensure remote directory exists
ssh -q -i ~/ec2-key.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $EC2_SSH_ENDPOINT > /dev/null 2>&1 << 'EOF'
sudo mkdir -p /tmp/deployment_frontend
sudo chown ${{ secrets.EC2_SSH_USER }}:${{ secrets.EC2_SSH_USER }} /tmp/deployment_frontend
EOF
- name: Push image
run: |
docker push $IMAGE_ID:$IMAGE_VERSION
docker push $IMAGE_ID:latest
# Copy files
scp -q -i ~/ec2-key.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r ./compose.frontend.yaml $EC2_SSH_ENDPOINT:/tmp/deployment_frontend/ > /dev/null 2>&1
# Connect and deploy services
ssh -q -i ~/ec2-key.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $EC2_SSH_ENDPOINT > /dev/null 2>&1 << 'EOF'
export FRONTEND_IMAGE="${{ secrets.FRONTEND_IMAGE }}"
export NEXT_PUBLIC_EXTERNAL_BACKEND_BASE_URL="${{ secrets.NEXT_PUBLIC_EXTERNAL_BACKEND_BASE_URL }}"
export INTERNAL_BACKEND_BASE_URL="${{ secrets.INTERNAL_BACKEND_BASE_URL }}"
export NEXT_PUBLIC_FILES_BASE_URL="${{ secrets.NEXT_PUBLIC_FILES_BASE_URL }}"
# Run Docker Compose
cd /tmp/deployment_frontend/
docker compose -f compose.frontend.yaml --project-name frontend up --pull always --detach
sudo rm -rf /tmp/deployment_frontend
EOF
Loading

0 comments on commit 7c02a3d

Please sign in to comment.