Skip to content

Commit

Permalink
try 165
Browse files Browse the repository at this point in the history
  • Loading branch information
AzizMukhtorjonov committed Jun 23, 2024
1 parent 5216659 commit d165778
Showing 1 changed file with 31 additions and 59 deletions.
90 changes: 31 additions & 59 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
name: Build and deploy on server
name: Build and Deploy Docker Image 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:
checkout-code:
build-and-deploy:
environment: Test
runs-on: ubuntu-latest

Expand All @@ -17,73 +21,41 @@ jobs:
with:
submodules: 'recursive'

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "latest"

- name: Create .env file
run: |
echo "${{ secrets.ENVS }}" > .env
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: source-code
path: .

setup-ssh:
environment: Test
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/[email protected]
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
ssh-private-key: ${{ env.SSH_PRIVATE_KEY }}

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

steps:
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: source-code
path: .
echo "${{ env.KNOWN_HOSTS }}" > ~/.ssh/known_hosts
- name: Deploy on Server
env:
SERVER_USER: ${{ secrets.SERVER_USER }}
SERVER_HOST: ${{ secrets.SERVER_HOST }}
DIRECTORY: ${{ env.DIRECTORY }}

run: |
echo $DIRECTORY
ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_HOST << EOF
rm -rf $DIRECTORY && mkdir -p $DIRECTORY
EOF
scp -o StrictHostKeyChecking=no -r * $SERVER_USER@$SERVER_HOST:$DIRECTORY
scp -o StrictHostKeyChecking=no .env $SERVER_USER@$SERVER_HOST:$DIRECTORY/.env
ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_HOST << EOF
echo "Starting deployment process..."
ssh -t $SERVER_USER@$SERVER_HOST << EOF
ssh-agent
ssh-add -L
# Prepare the directory
if [ -d "$DIRECTORY" ]; then
rm -rf $DIRECTORY
fi
mkdir -p $DIRECTORY
cd $DIRECTORY
# Clone the repository with submodules
git clone -b cicd https://github.com/${{ github.repository }} .
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 d165778

Please sign in to comment.