Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try 162
Browse files Browse the repository at this point in the history
AzizMukhtorjonov committed Jun 23, 2024
1 parent 27d34a3 commit 1e2827c
Showing 1 changed file with 46 additions and 32 deletions.
78 changes: 46 additions & 32 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
name: Build and Deploy Docker Image on Server
name: Build and deploy on server

on:
push

env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SERVER_USER: ${{ secrets.SERVER_USER }}
SERVER_HOST: ${{ secrets.SERVER_HOST }}
KNOWN_HOSTS: ${{ secrets.KNOWN_HOSTS }}
DIRECTORY: "${{ vars.DIRECTORY }}/${{ github.event.repository.name }}"

jobs:
build-and-deploy:
environment: Test
checkout-code:
runs-on: ubuntu-latest

steps:
@@ -30,42 +25,61 @@ jobs:
run: |
echo "${{ secrets.ENVS }}" > .env
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: source-code
path: .

setup-ssh:
runs-on: ubuntu-latest
needs: checkout-code

steps:
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: source-code
path: .

- name: Set up SSH
uses: webfactory/ssh-agent@v0.5.3
with:
ssh-private-key: ${{ env.SSH_PRIVATE_KEY }}
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}

- name: Add server to known hosts
run: |
mkdir -p ~/.ssh
echo "$KNOWN_HOSTS" > ~/.ssh/known_hosts
echo "${{ secrets.KNOWN_HOSTS }}" > ~/.ssh/known_hosts
deploy-on-server:
runs-on: ubuntu-latest
needs: setup-ssh

steps:
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: source-code
path: .

- name: Deploy on Server
env:
SERVER_USER: ${{ secrets.SERVER_USER }}
SERVER_HOST: ${{ secrets.SERVER_HOST }}
DIRECTORY: ${{ env.DIRECTORY }}

run: |
echo $DIRECTORY
ssh -t $SERVER_USER@$SERVER_HOST << EOF
# Prepare the directory
if [ -d "$DIRECTORY" ]; then
rm -rf $DIRECTORY
fi
mkdir -p $DIRECTORY
rm -rf $DIRECTORY && mkdir -p $DIRECTORY
EOF
scp -r * $SERVER_USER@$SERVER_HOST:$DIRECTORY
scp .env $SERVER_USER@$SERVER_HOST:$DIRECTORY/.env
ssh -t $SERVER_USER@$SERVER_HOST << EOF
echo "Starting deployment process..."
cd $DIRECTORY
# Clone the repository with submodules
git clone -b cicd https://github.com/${{ github.repository }} .
eval "$(ssh-agent)"
echo "${{ env.SSH_PRIVATE_KEY }}" > id_rsa
chmod 777 id_rsa
ssh-add id_rsa
git submodule init
git submodule update
# Create .env file
echo "${{ secrets.ENVS }}" > .env
# Run deploy script
sh deploy.sh
echo "Deployment successful, cleaning up directory..."
rm -rf $DIRECTORY
EOF

0 comments on commit 1e2827c

Please sign in to comment.