How to run any ssh command on your webserver with github actions?
On host machine
1 |
ssh-keygen -t rsa -b 4096 -C "<YOUR_EMAIL>" |
In workflow.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
deploy: needs: publish name: Deploy to VPS runs-on: ubuntu-latest steps: - name: Install ssh keys run: | mkdir -p ~/.ssh echo "${{secrets.APP_SSH_PRIVATE_KEY}}" > ~/.ssh/id_rsa_${{env.SERVICE_NAME}} chmod 600 ~/.ssh/id_rsa_${{env.SERVICE_NAME}} cat >>~/.ssh/config <<EOF Host ${{env.SERVICE_NAME}} HostName ${{env.APP_SSH_HOST}} Port ${{env.APP_SSH_PORT}} User ${{env.APP_SSH_USER}} IdentityFile ~/.ssh/id_rsa_${{env.SERVICE_NAME}} StrictHostKeyChecking no EOF - name: Connect and pull run: | ssh ${{env.SERVICE_NAME}} -p ${{env.APP_SSH_PORT}} cd ${{env.APP_SSH_WORKDIR}} echo ${{secrets.PAT}} | docker login ghcr.io -u ${{github.actor}} --password-stdin docker compose pull ${{env.SERVICE_NAME}} docker compose up -d ${{env.SERVICE_NAME}} exit - name: Cleanup run: rm -rf ~/.ssh |