Skip to content

Workflow file for this run

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:
build-and-deploy:
environment: Test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
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: Set up SSH
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ env.SSH_PRIVATE_KEY }}
- name: Add server to known hosts
run: |
mkdir -p ~/.ssh
echo "$KNOWN_HOSTS" > ~/.ssh/known_hosts
- name: Deploy on Server
run: |
ssh -t $SERVER_USER@$SERVER_HOST << EOF
# Prepare the directory
if [ -d "$DIRECTORY" ]; then
rm -rf $DIRECTORY
fi
mkdir -p $DIRECTORY
cd $DIRECTORY
eval `$(ssh-agent -s)`
echo $SSH_AUTH_SOCK
echo $SSH_AGENT_PID
ssh-add /home/github/.ssh/keys/cloning/id_rsa
echo "$SSH_PRIVATE_KEY" > ~/.ssh/keys/cloning/github_key
chmod 600 ~/.ssh/keys/cloning/github_key
echo "IdentityFile ~/.ssh/keys/cloning/github_key" >> ~/.ssh/config
# Clone the repository with submodules
git clone --recurse-submodules -b cicd https://github.com/${{ github.repository }} .
# Create .env file
echo "${{ secrets.ENVS }}" > .env
# Run deploy script
sh deploy.sh
EOF