fix: Add sudo to git stash in deployment workflow #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
if [ "${{ matrix.instance }}" == "1" ]; then | |
ssh-keyscan -H "${{ secrets.EC2_HOST_1 }}" >> ~/.ssh/known_hosts | |
else | |
ssh-keyscan -H "${{ secrets.EC2_HOST_2 }}" >> ~/.ssh/known_hosts | |
fi | |
- name: Deploy to EC2 Instance ${{ matrix.instance }} | |
env: | |
EC2_HOST: ${{ matrix.instance == 1 && secrets.EC2_HOST_1 || secrets.EC2_HOST_2 }} | |
DEPLOY_PATH: ${{ secrets.DEPLOY_PATH }} | |
run: | | |
ssh -i ~/.ssh/daimon.pem ubuntu@$EC2_HOST << EOF | |
cd $DEPLOY_PATH | |
sudo git stash | |
git fetch origin feat/deploy-actions | |
git reset --hard origin/feat/deploy-actions | |
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 |