Skip to content

Commit

Permalink
feat: Add GitHub Actions workflow for automated EC2 deployment
Browse files Browse the repository at this point in the history
- Created deploy.yml workflow for deploying application to multiple EC2 instances
- Added deploy-readme.md with comprehensive documentation for deployment process
- Configured workflow to trigger on push or merge to feat/deploy-actions branch
- Implemented multi-instance deployment strategy with SSH-based deployment steps
  • Loading branch information
moarshy committed Feb 22, 2025
1 parent fa68a28 commit 68a9034
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
83 changes: 83 additions & 0 deletions .github/workflows/deploy-readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Application Deployment Workflow

This repository contains a GitHub Actions workflow for automated deployment to an Amazon EC2 instance. The workflow is triggered either by pushing to the `feat/deploy-actions` branch or when a pull request to this branch is merged.

## Workflow Overview

The deployment workflow automates the following steps:
1. Checks out the repository
2. Sets up SSH access to the EC2 instance
3. Deploys the application to EC2
4. Reports the deployment status

## Prerequisites

Before using this workflow, ensure you have the following setup:

1. An Amazon EC2 instance running Ubuntu
2. The following GitHub repository secrets configured:
- `EC2_SSH_KEY`: The private SSH key for connecting to the EC2 instance
- `EC2_HOST`: The hostname or IP address of your EC2 instance
- `DEPLOY_PATH`: The path on the EC2 instance where the application should be deployed

## Trigger Conditions

The workflow triggers under two conditions:
- On push to the `feat/deploy-actions` branch
- When a pull request to the `feat/deploy-actions` branch is merged

## Deployment Process

The deployment process follows these steps:

1. **Repository Checkout**: Fetches the latest code from the repository
2. **SSH Setup**:
- Creates SSH directory
- Installs the SSH private key
- Adds the EC2 host to known hosts
3. **Application Deployment**:
- Connects to EC2 via SSH
- Navigates to the deployment directory
- Stashes any local changes
- Fetches and resets to the latest code
- Attempts to reapply stashed changes
- Stops the existing service
- Launches the new version
4. **Status Reporting**: Reports whether the deployment was successful or failed

## Required Files

The workflow expects the following files to exist in your repository:
- `stop_service.sh`: Script to stop the currently running service
- `launch.sh`: Script to start the application

## Usage

No manual intervention is needed for deployment. The workflow will automatically run when:
- Code is pushed to the `feat/deploy-actions` branch
- A pull request to the `feat/deploy-actions` branch is merged

## Monitoring

You can monitor deployments in the GitHub Actions tab of your repository. Each deployment will show:
- Complete logs of the deployment process
- Final deployment status (✅ success or ❌ failure)

## Note

Currently, the workflow is configured to use the `feat/deploy-actions` branch. This will be updated to use the `main` branch once testing is completed.

## Troubleshooting

If deployment fails, check:
1. EC2 instance is running and accessible
2. SSH key is correctly configured in GitHub secrets
3. All required scripts (`stop_service.sh` and `launch.sh`) exist and are executable
4. Deployment path exists on the EC2 instance
5. GitHub Actions logs for specific error messages

## Security Considerations

- The SSH key is stored securely in GitHub secrets
- SSH key permissions are set to 600 (read/write for owner only)
- Host key verification is enabled for the EC2 instance
52 changes: 52 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Deploy Application

on:
push:
branches:
- feat/deploy-actions
pull_request:
types:
- closed
branches:
- feat/deploy-actions

jobs:
deploy:
if: github.event.pull_request.merged == true || github.event_name == 'push'
runs-on: ubuntu-latest
strategy:
matrix:
instance: [1, 2]

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

- name: Install SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/daimon.pem
chmod 600 ~/.ssh/daimon.pem
ssh-keyscan -H "${{ secrets.EC2_HOST_${{ matrix.instance }} }}" >> ~/.ssh/known_hosts
- name: Deploy to EC2 Instance ${{ matrix.instance }}
run: |
ssh -i ~/.ssh/daimon.pem ubuntu@${{ secrets.EC2_HOST_${{ matrix.instance }} }} << 'EOF'
cd ${{ secrets.DEPLOY_PATH }}
git stash
git fetch origin feat/deploy-actions # TODO: Change this to main once testing is done
git reset --hard origin/feat/deploy-actions # TODO: Change this to main once testing is done
git stash pop || true
bash stop_service.sh
bash launch.sh
echo "Deployment completed to Instance ${{ matrix.instance }} at $(date)"
EOF
- name: Deployment Status
if: always()
run: |
if [ ${{ job.status }} == 'success' ]; then
echo "✅ Deployment successful to Instance ${{ matrix.instance }}"
else
echo "❌ Deployment failed for Instance ${{ matrix.instance }}"
fi

0 comments on commit 68a9034

Please sign in to comment.