add extra steps on adding known hosts #12
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 on Server | |
on: | |
push: | |
branches: | |
- main | |
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 SSH | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ env.SSH_PRIVATE_KEY }} | |
- name: Add server to known hosts | |
run: | | |
mkdir -p ~/.ssh | |
touch ~/.ssh/known_hosts | |
echo "${{ secrets.SERVER_HOST }} ssh-rsa ${{ secrets.KNOWN_HOSTS }}" >> ~/.ssh/known_hosts | |
chmod 600 ~/.ssh/known_hosts | |
chmod 700 ~/.ssh | |
- name: Deploy on Server | |
run: | | |
ssh -t $SERVER_USER@$SERVER_HOST << EOF | |
if [ -d "$DIRECTORY" ]; then | |
rm -rf $DIRECTORY | |
fi | |
mkdir -p $DIRECTORY | |
cd $DIRECTORY | |
git clone https://github.com/${{ github.repository }} . | |
git submodule init | |
git submodule update | |
echo "${{ secrets.ENVS }}" > .env | |
sh deploy.sh | |
rm -rf $DIRECTORY | |
EOF |