Workflow (works with public and privare repositories):
- start with this template or modify your app taking this as an example
- run
renv::init()
orrenv::snapshot()
to capture dependencies in therenv.lock
file - work on your app
- add secrets to your GitHub repo settings and follow steps below
The /app
folder contains the shiny app. The demo app was taken from here:
https://github.com/analythium/shinyproxy-demo
Note: Shiny apps time out after 55 seconds:
After the initial response, each byte sent from the server restarts a rolling 55 second window. A similar 55 second window is restarted for every byte sent from the client.
If no data is received from the dyno within the 55 second window the connection is terminated and an H15 error is logged.
Similarly, if no data is received from the client within the 55 second window, the connection is terminated and an H28 error is logged.
A workaround was posted on SO to print a dot to the console every passage of 10 seconds.
A counter set at 50 seconds interval is added to /app/server.R
:
## prevent timeout
autoInvalidate <- reactiveTimer(intervalMs = 50*1000)
observe({
autoInvalidate()
cat(".")
})
The following deployment options are explained here:
Log into Heroku, in the dashboard, click on 'New' then select 'Create new App'.
Give a name (e.g. shiny-cicd
, if available, this will create the app at https://shiny-cicd.herokuapp.com/) to the app and create the app.
Got to the Settings tab of the repo, scrolld down to Secrets and add the following new repository secrets:
HEROKU_EMAIL
: your Heroku emailHEROKU_APP_NAME
: you application name from aboveHEROKU_API_KEY
: your Heroku api key, you can find it under your personal settings, click on reveal and copy
See the .github/workflows/deploy.yml
file for additional options
(dockerfile_name
, docker_options
, dockerfile_directory
)
- Install Heroku CLI
- R & Shiny
- Docker Engine installed & a Docker Hub account
See docs: https://devcenter.heroku.com/articles/build-docker-images-heroku-yml
Create a heroku.yml
file in your application's root directory. The following example heroku.yml specifies the Docker image to build for the app’s web process:
build:
docker:
web: Dockerfile
Heroku uses the CMD
specified in the Dockerfile.
The Dockerfile defines system requirements, R package dependencies.
FROM rocker/r-base:latest
...
ENV PORT=8080
CMD ["R", "-e", "shiny::runApp('/home/app', host = '0.0.0.0', port=as.numeric(Sys.getenv('PORT')))"]
The port settings and the CMD
part is different from ShinyProxy apps,
port number is passed as an env var by the Heroku container runtime.
See also here: https://github.com/virtualstaticvoid/heroku-docker-r
Commit your changes if any (only repos with new commits will deploy once the app is already on Heroku):
git commit -m "Changed"
Create the Heroku application with the container stack
heroku create --stack=container
Now you should see the app url.
Or configure an existing application to use the container stack.
heroku stack:set container
Deploy your application to Heroku, replacing <branch>
with your branch,
e.g. main
:
git push heroku <branch>
Now you see the build logs on the Heroku remote.
Now open the app in your browser:
heroku open
https://morning-springs-64281.herokuapp.com/
To build the image from the Dockerfile, run:
sudo docker build -t analythium/heroku-demo .
Test:
docker run -p 4000:8080 analythium/heroku-demo
then visit 127.0.0.1:4000
.
(c) Copyright Analythium Solutions Inc., 2021 (MIT).