Workflow file for this run
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: 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 | |
whoami | |
id | |
cd ~/.ssh | |
pwd | |
cd ~/.ssh/keys | |
pwd | |
cd ~/.ssh/keys/cloning | |
ll | |
ssh-add id_rsa | |
pwd | |
echo $SHELL | |
eval `$(ssh-agent -s)` | |
echo $SSH_AUTH_SOCK | |
echo $SSH_AGENT_PID | |
ssh-add /home/github/.ssh/keys/cloning/id_rsa | |
# 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 |