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 like deploying your Ruby code to your production servers. No manual steps are required.

In this guide, you will discover how to securely and reliably set up GitHub Actions to deploy your Ruby code automatically to both staging and production environments. This approach ensures that your deployments are consistent and reduces the risk of errors.

To get started, you will work with two branches in your GitHub repository: staging and production. Whenever you push code to 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 just a production instance, it is highly 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 with your Ruby code.

Head over 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".

Do not have a Stackhero account yet? You can sign up for free in just a couple of 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 will need to set up SSH keys. This ensures only authorized actions can connect to your Stackhero services, keeping your deployments secure.

You can generate a new set of SSH keys on your computer using:

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, head 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 choose 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 store your SSH private key securely, 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, be 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 directory called .github/workflows. Inside this directory, 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.
    # 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 }}

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 automatically start and deploy your code to your Stackhero instance.

To see your workflow in action, visit 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 the same process as production. Just repeat the steps above, swapping out production for staging wherever needed.

Once you have set up your staging environment, 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 designated Stackhero Ruby instance for staging.

To help keep your deployments safe, it is a good idea 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 right permissions can review and approve pull requests to staging, and once everything looks good, changes can be merged into production 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 making it to production). This helps ensure your deployments are smooth and your production environment remains stable.