How To Deploy A Git Repository To A Server Using GitHub Actions
Do you want to learn how to deploy a git repository to your own server using GitHub Actions? In this post, I will lead you through creating it step-by-step. This includes the steps on your server, the steps on GitHub, and the action itself. Therefore we will go through the different sections:
The idea and the goal
The goal of this action is to push changes to your repository and to pull them onto your server automatically. This can be useful for deploying a website or a web app that automatically updates when the user visits/reloads your page.
To accomplish this, the idea of the action is to first connect to the server via ssh as a user with enough rights for the next steps. Then we want to change the directory to the directory containing the repository, change to the main branch, and pull the changes. Afterward, we can exit the shell again.
VPS Hosting Course
Learn everything you need to know about servers and hosting your own applications!GitHub Action to deploy a git repository on a server
Now that we described the action’s goal and idea, we can start to set everything up. Follow the following steps:
On your server:
- Create a new user or use your own
- Check that user has access to the directory containing the repository (if the repository does not yet exist on the server, clone it into the directory you want)
- Create an SSH key:
ssh-keygen -t rsa -b 4096
- Add the public key to the authorized_keys file:
cat <path/to/public/key> >> ~/.ssh/authorized_keys
- Copy content of the key file:
cat <path/to/private/key
> - You need to paste it in the next step.
On the GitHub website:
- Go to “Settings > Secrets > Actions”
- Create the following secrets:
SSH_PRIVATE_KEY
: content of the private key fileSSH_USER
: user to access the serverSSH_HOST
: hostname/ip-address of your serverWORK_DIR
: path to the directory containing the repositoryMAIN_BRANCH
: name of the main branch (mostly main)
- Go to the “Actions” tab in your GitHub repository
- Click on “set up a workflow yourself”
- Paste the following content:
on:
push:
branches:
- main
workflow_dispatch:
jobs:
run_pull:
name: run pull
runs-on: ubuntu-latest
steps:
- name: install ssh keys
# check this thread to understand why its needed:
# https://stackoverflow.com/a/70447517
run: |
install -m 600 -D /dev/null ~/.ssh/id_rsa
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts
- name: connect and pull
run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "cd ${{ secrets.WORK_DIR }} && git checkout ${{ secrets.MAIN_BRANCH }} && git pull && exit"
- name: cleanup
run: rm -rf ~/.ssh
The different blocks do the following:
Need help or want to share feedback? Join my discord community!
on
: execute the action on push to the main branch or start it manuallyinstall ssh keys
: setup ssh keys and known hostsconnect and pull
: connect to the server via ssh, go to the correct directory and branch and then pull changescleanup
: remove the ssh keys and known hosts again
Conclusion
In this post, we create a GitHub Action to deploy changes in a git repository to a server easily. I hope this little guide was helpful to you. In case you have any questions, feel free to ask in the comments or send me a mail at mail@programonaut.com (also check out the helpful links sections before).
In case you rather want to use Docker for your application, check out this post here, where we deploy a Docker image of our application to our own server!
If this guide is helpful to you and you like what I do, please support me with a coffee!
If you liked this post, consider subscribing to my newsletter to get monthly updates!
[convertkit form=2303042]
Helpful Links
With these links, I solved problems that occurred along the way: