Ruby: Deploy with GitHub Actions

How to deploy your Ruby code using GitHub Actions

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Ruby 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 Ruby cloud hosting solution!

GitHub Actions makes it easy to automate tasks such as deploying your Ruby code to your production servers. No manual steps are required.

In this guide, you will learn how to securely and reliably configure GitHub Actions to automatically deploy your Ruby code to both staging and production environments. This method ensures consistent deployments and reduces the risk of errors.

To get started, you will use two branches in your GitHub repository: staging and production. Each time you push code to one of these branches, your changes will be automatically deployed to the corresponding Stackhero instance.

Having a staging instance is optional. While you can follow this guide with only a production instance, it is strongly recommended to set up both staging and production environments. This is considered an industry best practice and helps catch issues before they reach your production users.

Before you begin, make sure you have a GitHub account and a repository containing your Ruby code.

Go to your Stackhero dashboard to create two Ruby services, one for staging and another for production. For clarity, it is a good idea to rename these services to "Production" and "Staging".

Don't have a Stackhero account yet? You can sign up for free in just a few minutes, and then create your Ruby cloud services with just a few clicks.

Example of production and staging servicesExample of production and staging services

To allow GitHub Actions to deploy your code, you need to set up SSH keys. This ensures that only authorized actions can connect to your Stackhero services, keeping your deployments secure.

You can generate a new SSH key pair on your computer with:

ssh-keygen -C "" -f /tmp/ssh_key -N ""

Next, retrieve the public key you just created:

cat /tmp/ssh_key.pub

In the Stackhero dashboard, select your "production" Ruby service and click the "Configure" button. Get service settingsGet service settings

Then:

  1. Under SSH public keys, click Add a public key.
  2. For Description, enter GitHub Action.
  3. For Key, paste in the public key content from your computer.

Get service settingsGet service settings

Now, go to the GitHub website and open your project.

Go to Settings > Environments, then click New environment. Configuring GitHub environmentsConfiguring GitHub environments

Name your environment "production" and save it. Setting the environmentSetting the environment

Click the No restriction button and select Selected branches and tags. Setting environment restrictionsSetting environment restrictions

Click Add deployment branch or tag rule, enter "production" as the pattern, and add the rule. Setting environment branchSetting environment branch Setting environment branchSetting environment branch

To securely store your SSH private key, go to Environment secrets and click Add secret. Add secretAdd secret

Retrieve your private key from your computer:

cat /tmp/ssh_key

For Name, enter STACKHERO_SSH_PRIVATE_KEY. For Value, paste your private key content. Setting the SSH private key secretSetting the SSH private key secret

Next, add your Ruby service's endpoint as an environment variable. Click on Add variable in Environment variables. Setting variablesSetting variables

For Name, enter STACKHERO_ENDPOINT. For Value, paste the endpoint for your Ruby service, which you can find in your Stackhero dashboard. Setting the endpoint variableSetting the endpoint variable

If you have set a custom domain name for your service, make sure to use that custom domain instead of <XXXXXX>.stackhero-network.com.

Once your keys are set up in Stackhero and GitHub, you can delete them from your computer for security.

rm /tmp/ssh_key /tmp/ssh_key.pub

On your local machine, in your Git repository, create a folder called .github/workflows. Inside this folder, create a file named deploy-to-stackhero.yml.

# 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.
    # Don't 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 }}

After creating the workflow file, you can commit your changes.

git add -A .
git commit -m "Add GitHub Actions to deploy to Stackhero"

Next, create a production branch.

git checkout -b production

Finally, push your changes to GitHub.

git push --set-upstream origin production

This last git push will upload your code to the production branch on GitHub. GitHub Actions will then automatically start and deploy your code to your Stackhero instance.

To see your workflow in action, go to your project's GitHub page and click Actions. GitHub Actions that deployed to productionGitHub Actions that deployed to production

Congratulations, you have just set up continuous deployment to production with GitHub Actions.

Setting up the staging environment follows exactly the same process as for production. Simply repeat the steps above, replacing production with staging wherever necessary.

Once your staging environment is configured, you can create a staging branch.

git checkout -b staging

Push your staging branch to GitHub.

git push --set-upstream origin staging

GitHub Actions will now automatically deploy your staging branch to your dedicated Stackhero Ruby instance for staging.

To further secure your deployments, it is recommended to protect your production and staging branches. This means direct pushes are restricted, and changes must go through a pull request. Team members with the appropriate permissions can review and approve pull requests to staging, and once everything is validated, merge the changes into production in the same way.

By following this workflow, you increase security (only authorized users can deploy to staging and production) and reliability (all new features are tested in staging before reaching production). This helps ensure smooth deployments and a stable production environment.