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 your production servers.
In this guide, you will learn how to securely and reliably configure GitHub Actions to deploy your Python code to both staging and production environments.
To keep things organised, you will use two branches: staging and production. Each time 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 with just a single production instance. However, to ensure smooth deployments and to deploy to production with confidence, it is strongly recommended to maintain both a staging and a production instance. This practice is an industry standard and a reliable approach that helps you avoid many issues.
Before you begin, make sure you have a GitHub account and a repository hosting your code.
Creating the Python services
Go to your Stackhero dashboard and create two Stackhero services, one for staging and one for production. For clarity, you may wish to name them "Production" and "Staging".
Don't have a Stackhero account yet? You can sign up for free in just two minutes and then create your Python cloud services in 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 essential for protecting your Stackhero services.
On your computer, you can generate new SSH keys by running:
ssh-keygen -C "" -f /tmp/ssh_key -N ""
Add the public key
First, display the public key you have 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 have just copied.
Add public key
Add 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 customised your service's domain name, use the customised version instead of <XXXXXX>.stackhero-network.com.
Delete the generated keys
For security reasons, it is advisable to delete the SSH keys from your computer now, as 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, which will deploy your code to your Stackhero instance.
To check your deployment status, simply go to your GitHub project page and click on Actions.
GitHub Actions that deployed to production
That's 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 just need to repeat the steps above, replacing production with staging wherever it appears.
After configuring the environment, you can create a staging branch as follows:
git checkout -b staging
Then push your staging branch to GitHub:
git push --set-upstream origin staging
Your staging branch will then be automatically deployed to the Python instance you have dedicated to staging.
Going further
It is recommended to protect the production and staging branches to prevent direct pushes. With this setup, team members will need to create pull requests to the staging branch, which should be reviewed and merged by users with the appropriate permissions. Once changes have been validated on the staging platform, the pull request can be merged into the production branch by an authorised person.
This workflow ensures that only approved changes reach production, while also improving reliability by testing new features on staging before they go live.