GitLab CI/CD for Posit Connect

GitLab CI/CD for Posit Connect

Sept 23, 2023   ·  5 min read

Posit Connect is a service offered by Posit, formally named Rstudio, and is designed to make the delivery of data-driven applications (i.e., dashboards, notebooks, APIs, etc.) fast, approachable, and secure. This article shows how to publish applications to Posit Connect using GitLab CI/CD pipelines.

I've worked with Posit Connect (formerly Rstudio Connect) for the last four years, both as a user and an admin. It is a wonderful tool for data scientists to showcase their work to stakeholders and colleagues in a friendly and consumable way. 

One lesson that I've learned from this experience is that publishing content to Posit Connect using CI/CD offers several compelling advantages over manual, local laptop-based approaches. However, I wasn't able to find any useful documentation on how to do so using GitLab CI/CD pipelines and had to sort that out myself

If you are using Posit Connect and are interested in GitLab CI/CD then this article is for you!

What is CI/CD?

CI/CD stands for "Continuous Integration and Continuous Deployment/Delivery". There are many CI/CD tools out there, but the ones that I find most beneficial are those that are already available on your chosen Version Control System (VCS), like GitLab or GitHub. This article assumes that you are using GitLab and already have it set up for CI/CD (either with GitLab's free managed infrastructure or your own).

Using pipelines to publish content to Posit Connect offers several advantages over manual, local laptop-based approaches. 

Firstly, automation through CI/CD pipelines streamlines the content publishing process, reducing the risk of human errors that can often accompany manual deployments. This offers greater consistency and reliability in content delivery. 

Secondly, CI/CD pipelines provide a structured and version-controlled workflow, enabling teams to track changes, collaborate effectively, and roll back to previous states if needed. This enhances accountability and facilitates collaboration among team members, especially in a distributed or cross-functional setup. Additionally, CI/CD pipelines can trigger content updates automatically in response to changes in the source code or content repository, thus saving time and effort.

Lastly, by leveraging GitLab CI/CD pipelines, organizations can scale their content publishing operations more efficiently, reducing manual overhead, and ensuring that the process remains consistent and reliable as time goes on. 

Overall, GitLab CI/CD pipelines enhance productivity, accuracy, and scalability while mitigating potential risks associated with manual content publishing.

Publishing to Posit Connect using GitLab CI/CD

The following steps is how you can set up a GitLab repository to have a parent-child pipeline for each of your deployments to Posit Connect.

1. Use an API key from Posit Connect

You can use an existing API key or create a new one by logging into your Posit Connect account, clicking your profile name, and selecting "API Keys". This allows your code to securely publish to your Posit Connect server, as I'll demonstrate later.

2. Develop your content locally and then publish it to Posit Connect

If you don't have a working dashboard to publish yourself, you can use any one of the "Jump Start Examples" that Posit Connect offers to quickly get started, two of which are used later in this article for demonstration purposes.

3. Add a subfolder to your Git repository for your new content

After first having iterated and settled on your deployment to Posit Connect manually from your local machine, create a subfolder here and copy all of your content's files therein, including the auto-generated rsconnect-python/ directory from publishing your content manually. This directory contains useful metadata that will be used by the GitLab CI/CD pipeline.

4. Set up the necessary environment variables in your GitLab CI/CD settings

Now that you have a subfolder containing your content's necessary files, make sure to save any required environment variables in the CI/CD settings of the repo. Start by clicking on "Settings" and then underlying the "CI/CD" section.

From there, scroll down to the Variables section, expand, and then add each of your variables by providing the name of the variable (e.g., the "Key") and the value of the variable. You can set up the "Environment scope" under the name of your new subdirectory (the naming here is arbitrary, but naming it with your folder's name will more easily differentiate between other environments). Click "create wildcard" to set this up successfully and save by clicking "Add variable" at the bottom right of the pop up window.

NOTE: Some environment variables can be made available across all environments, like your Posit Connect server address.

Eventually your CI/CD Variables section will start to look like this:

5. Set up child pipeline for your content's ongoing CI/CD

Add a .gitlab-ci.yml file inside your new subdirectory to create a "child pipeline" in the repository's GitLab CI/CD pipeline. This will ensure that changes to your dashboard are independent and will only take effect when changes are made within your subdirectory. You can use the following code block as a template.

6. Add your new child pipeline to your repository's overall pipeline

Update the repo's top-level .gitlab-ci.yml file with a pipeline job specifying your newly created child pipeline from the previous step. You can use the following code block as a template.

7. Keep you environment variables updated in your GitLab CI/CD settings

To update the environment variables attached to your published Posit Connect content, you will need to (1) update the variables in the CI/CD settings of your repository and (2) kick off your child pipeline by either manually doing so from the GitLab UI or by pushing a change to the content's code.


And that's it! Thanks for reading :)

If you found any of my content helpful, please consider donating
using one of the following options   Anything is appreciated!