This guide will walk you through setting up an automated deployment process for your application using GitHub Actions. When you push changes to the master branch of your repository, this workflow will automatically deploy them to your server.
The process is managed by a GitHub Actions workflow defined in the .github/workflows/deploy.yml file. Here's a quick rundown of what it does:
- On Push to Master: The workflow is triggered every time you push new code to the
masterbranch. - Checkout Code: It checks out the latest version of your code in the GitHub Actions runner environment.
- SSH and Deploy: It connects to your server using SSH (with a username and password) and runs a series of commands to:
- Navigate to your project's directory.
- Pull the latest code from your GitHub repository.
- Install or update any necessary Python packages from your
requirements.txtfile. - Restart your application to apply the new changes. We use
screenin this example for simplicity.
Follow these steps to get your automated deployment up and running.
Before setting up the GitHub Action, make sure your server is ready for deployments:
- Git: Ensure Git is installed and that your project is cloned onto the server from your GitHub repository.
- Python and Virtual Environment: Your application should be able to run in a Python virtual environment to keep dependencies isolated. Create one if you haven't already:
python3 -m venv venv- Application Runner (Optional but Recommended): It's a good practice to run your application in a way that it continues running even after you disconnect from the SSH session. In our example, we use
screen, a simple terminal multiplexer. You can install it on Debian/Ubuntu with:
sudo apt-get update
sudo apt-get install screenTo allow GitHub Actions to securely connect to your server, you need to store your SSH credentials as secrets in your GitHub repository. Do not hardcode your password or any sensitive information directly in the .yml file.
- In your GitHub repository, go to Settings > Secrets and variables > Actions.
- Click on New repository secret for each of the following:
SSH_HOST: The IP address or domain name of your server.- Example:
123.45.67.89
- Example:
SSH_USER: The username you use to log in to your server.- Example:
rootorubuntu
- Example:
SSH_PASSWORD: The password for that user.
- In your repository, create a directory named
.github, and inside it, create another directory namedworkflows. - Inside the
.github/workflowsdirectory, create a new file nameddeploy.yml. - Copy and paste the content of the simplified
deploy.ymlprovided in the previous step into this new file.
Open the deploy.yml file and make the following changes to fit your project:
cd /path/to/your/app: Change/path/to/your/appto the actual path where your project is located on your server.uvicorn app.main:app ...: If you're not using FastAPI with Uvicorn, or if your main application file is named differently, update this command to how you start your application.
Commit these changes and push them to your master branch.
git add .github/workflows/deploy.yml
git commit -m "Add CI/CD deployment workflow"
git push origin masterYou can now go to the Actions tab in your GitHub repository to see your deployment workflow in action! From now on, every time you push to the master branch, your changes will be automatically deployed.