41
Deploying Shiny Apps to Heroku with Docker and GitHub Actions
By: Peter Solymos
I introduced the Heroku cloud platform for Shiny apps on the Hosting
Data Apps website. We saw how to use the command line tools to have a
git-based
workflow.
In this post, we take this one step further. You will learn how to set
up continuous integration/continuous delivery (CICD) with GitHub actions
to build and deploy the Shiny app to Heroku.
Data Apps website. We saw how to use the command line tools to have a
git-based
workflow.
In this post, we take this one step further. You will learn how to set
up continuous integration/continuous delivery (CICD) with GitHub actions
to build and deploy the Shiny app to Heroku.
In this post, we will concentrate on how to deploy a single app from the
root of the application. But the example repository has some
instructions for hosting and deploying multiple apps from the same
repository.
root of the application. But the example repository has some
instructions for hosting and deploying multiple apps from the same
repository.
The workflow described here works with public and private
repositories. It requires some setup in the Heroku dashboard and
GitHub settings. Once set up, you just keep pushing commits and the rest
is taken care of by the pipeline.
repositories. It requires some setup in the Heroku dashboard and
GitHub settings. Once set up, you just keep pushing commits and the rest
is taken care of by the pipeline.
Log into Heroku, in the dashboard, click on 'New' then select 'Create
new App'.
new App'.
Give a name (e.g.
app at https://shiny-example.herokuapp.com/) to the app and create the
app.
shiny-example
, if available, this will create theapp at https://shiny-example.herokuapp.com/) to the app and create the
app.
In your Heroku dashboard, go to your personal settings.

Find your API key, click on reveal and copy it, you'll need it later.
Go to the Settings tab of the GitHub repository, scroll down to Secrets
and add your
and add your
HEROKU_EMAIL
and HEROKU_API_KEY
as repository secrets:
See the
for options (see comments in the YAML file):
.github/workflows/deploy.yml
file in the example repositoryfor options (see comments in the YAML file):
shiny-example
) that was set up
in the Heroku dashboard previously,appdir
variable to e.g. app1
, this is the
directory the script will use to find the Shiny files relative to
the Dockerfile
in the root of this directory.Here is the YAML file for the GitHub action:
name: Build Shiny Docker Image and Deploy to Heroku
on:
push:
branches:
- main
jobs:
app1:
name: Build and deploy Shiny app
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Build and push Docker to Heroku
uses: akhileshns/heroku-[email protected]
with:
heroku_app_name: shiny-example
appdir: "."
heroku_api_key: ${{ secrets.HEROKU_API_KEY }}
heroku_email: ${{ secrets.HEROKU_EMAIL }}
usedocker: true
What do we have here? First, the app name. Then the
determines the events that trigger the workflow, here git push to the
steps. Job names must be unique in the YAML file to avoid parsing
errors.
on
sectiondetermines the events that trigger the workflow, here git push to the
main
branch. Next, you have the description of the jobs, including thesteps. Job names must be unique in the YAML file to avoid parsing
errors.
The steps in the
app1
job are:akhileshns/heroku-deploy
GitHub action.The Docker build/push-piece can be achieved using shell commands. The
following alternative is more verbose than the ready-made action. I
include the shell version here as a reference. It shows the power of
shell scripts in GitHub actions and how to use secrets and environment
variables:
following alternative is more verbose than the ready-made action. I
include the shell version here as a reference. It shows the power of
shell scripts in GitHub actions and how to use secrets and environment
variables:
name: Build Shiny Docker Image and Deploy to Heroku
on:
push:
branches:
- main
jobs:
app1:
name: Build and deploy Shiny app
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Build and push Docker to Heroku
env:
HEROKU_APP_NAME: shiny-example
DOCKERFILE_DIRECTORY: "."
HEROKU_EMAIL: ${{ secrets.HEROKU_EMAIL }}
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
DOCKERFILE_NAME: "Dockerfile"
DOCKER_OPTIONS: "--no-cache"
run: |
cd ${DOCKERFILE_DIRECTORY}
echo ${HEROKU_API_KEY} | docker login \
--username=${HEROKU_EMAIL} \
registry.heroku.com \
--password-stdin
docker build \
--file ${DOCKERFILE_NAME} \
${DOCKER_OPTIONS} \
--tag registry.heroku.com/${HEROKU_APP_NAME}/web .
heroku container:push web --app ${HEROKU_APP_NAME}
heroku container:release web --app ${HEROKU_APP_NAME}
Trigger the GitHub action by a new commit to the repo. When all goes
well, you should see logs like this:
well, you should see logs like this:

If you now go to the Heroku dashboard, on the application's page click
on the 'Activity' tab and you will see the note about the new
deployment.
on the 'Activity' tab and you will see the note about the new
deployment.
You created a continuous integration/continuous delivery (CICD) pipeline
for a dockerized Shiny application. The CICD pipeline uses GitHub
actions to build and deploy the Docker image to the versatile
application platform called Heroku.
for a dockerized Shiny application. The CICD pipeline uses GitHub
actions to build and deploy the Docker image to the versatile
application platform called Heroku.
41