Python: Deploy with GitHub Actions
How to deploy your Python code using GitHub Actions
👋 Welcome to the Stackhero documentation!
Stackhero offers a ready-to-use Python cloud solution that provides a host of benefits, including:
- Deploy your application in seconds with a simple
git push.- Use your own domain name and benefit from the automatic configuration of HTTPS certificates for enhanced security.
- Enjoy peace of mind with automatic backups, one-click updates, and straightforward, transparent, and predictable pricing.
- Get optimal performance and robust security thanks to a private and dedicated VM.
Save time and simplify your life: it only takes 5 minutes to try Stackhero's Python cloud hosting solution!
GitHub Actions makes it easy to automate tasks, including deploying your Python code directly to production servers.
In this guide, you will see how to securely and reliably set up GitHub Actions to deploy your Python code to both staging and production environments.
To keep things organized, you will use two branches: staging and production. Whenever you push code to these branches, it will be automatically deployed to the corresponding Stackhero instance.
Having a staging instance is not mandatory. You can follow this guide and have just one production instance. However, to ensure smooth deployments and be fully confident when deploying to production, it is strongly recommended to maintain both a staging and a production instance. This practice is an industry standard and a sound approach that can help you avoid numerous issues.
Before you begin, make sure you have a GitHub account and a repository where your code is hosted.
Creating the Python services
Head over to your Stackhero dashboard and create two Stackhero services, one for staging and one for production. For clarity, you might want to name them "Production" and "Staging".
Do not have a Stackhero account yet? You can sign up in just two minutes for free and then create your Python cloud services with just a few clicks.
Example of production and staging services
Configure SSH keys
SSH keys allow GitHub Actions to securely connect to your Python service for code deployment. This step is crucial for protecting your Stackhero services.
On your computer, you can generate new SSH keys by running:
ssh-keygen -C "" -f /tmp/ssh_key -N ""
Set the public key
First, display the public key you just generated:
cat /tmp/ssh_key.pub
Next, in your Stackhero dashboard, select your "production" Python service and click the "Configure" button.
Get service settings
Follow these steps:
- Under
SSH public keys, click onAdd a public key. - For
Description, enterGitHub Action. - For
Key, paste the public key you just copied.
Add public key
Set the private key
Go to your GitHub project page, click on Settings, then Environments. Choose New environment.
Configuring GitHub environments
In the Name field, type "production" and confirm.
Setting the environment
Click on the No restriction button and select Selected branches and tags.
Setting environment restrictions
Then, click on Add deployment branch or tag rule, enter "production" in the Name pattern field, and click Add rule.
Setting environment branch
Setting environment branch
In the Environment secrets section, click Add secret.
Add secret
Now, display the private key you generated earlier:
cat /tmp/ssh_key
In the secret setup, use STACKHERO_SSH_PRIVATE_KEY as the Name and paste the private key as the Value.
Setting the SSH private key secret
Next, in the Environment variables section, click on Add variable.
Setting variables
Enter STACKHERO_ENDPOINT as the Name and paste your Python service endpoint as the Value. You will find this endpoint in your Stackhero dashboard.
Setting the endpoint variable
If you have customized your service's domain name, use the customized version instead of <XXXXXX>.stackhero-network.com.
Delete the generated keys
For security, it is a good idea to remove the SSH keys from your computer now, since you will not need them anymore:
rm /tmp/ssh_key /tmp/ssh_key.pub
Configure the GitHub Actions workflow
In your Git repository, create a directory called .github/workflows if it does not already exist. Then, add a file named deploy-to-stackhero.yml inside it.
# File: .github/workflows/deploy-to-stackhero.yml
name: Deploy to Stackhero
run-name: Deploy branch "${{ github.ref_name }}" to Stackhero
on:
push:
# List of branches that will trigger the deploy action following a git push.
# Do not forget to create an environment corresponding to the branch name in GitHub (in "Settings"/"Environments").
# Then add the corresponding secret "STACKHERO_SSH_PRIVATE_KEY" and variable "STACKHERO_ENDPOINT" in this environment.
branches: [ "production", "staging" ]
jobs:
Deploy:
environment: ${{ github.ref_name }}
runs-on: ubuntu-latest
steps:
- uses: stackhero-io/github-actions-deploy-to-stackhero@v1
with:
# The secret "STACKHERO_SSH_PRIVATE_KEY" and the variable "STACKHERO_ENDPOINT" should be defined in the corresponding branch environment on GitHub under "Settings"/"Environments".
ssh_private_key: ${{ secrets.STACKHERO_SSH_PRIVATE_KEY }}
endpoint: ${{ vars.STACKHERO_ENDPOINT }}
Once you have added the workflow file, you can commit your changes:
git add -A .
git commit -m "Add GitHub Actions to deploy to Stackhero"
You can create a production branch with:
git checkout -b production
And push your changes to GitHub:
git push --set-upstream origin production
This will push your code to the production branch on GitHub and trigger GitHub Actions, deploying your code to your Stackhero instance.
To check your deployment status, just visit your GitHub project page and click on Actions.
GitHub Actions that deployed to production
That is it. You now have automatic deployments to production using GitHub Actions.
Creating the staging environment
Setting up a staging environment is almost identical to configuring production. You can simply repeat the steps above, replacing production with staging wherever it appears.
After setting up the environment, you can create a staging branch like this:
git checkout -b staging
Then push your staging branch to GitHub:
git push --set-upstream origin staging
Your staging branch will now be automatically deployed to the Python instance you designated for staging.
Going further
It is a good idea to protect the production and staging branches to prevent direct pushes. With this setup, team members will need to create pull requests for the staging branch, which should be reviewed and merged by users with the right permissions. Once changes have been validated on the staging platform, the pull request can be merged into the production branch by someone authorized.
This workflow helps ensure that only approved changes reach production, while also improving reliability by testing new features on staging before they go live.