Managing state in CI/CD
This feature is considered in a beta stage. It is still being tested and may change. For more information, see the API lifecycle stages documentation.
This guide explains how to refresh state for both Local Filesystem and Versioned State Storage strategies while deploying your Dagster project.
Before you begin
Ensure your project is installed in the current environment (e.g. GitHub Actions, Docker, etc.) where you'll run state refresh commands.
- uv
- pip
uv pip install -e .
pip install -e .
Refreshing state for Local Filesystem components
If your components use the LOCAL_FILESYSTEM state management type, you typically refresh state during your CI/CD pipeline as part of building your Docker image.
Basic refresh command
Run this command to refresh state for all Local Filesystem components:
dg utils refresh-defs-state --management-type LOCAL_FILESYSTEM
This command:
- Discovers and loads all state-backed components in your project
- Filters to only those using
LOCAL_FILESYSTEMmanagement type - Fetches fresh state from each external system (Tableau, Fivetran, etc.)
- Writes the state to the
.local_defs_statedirectory in your project - Reports success or failure for each component
Example: Dockerfile with state refresh
Here's a Dockerfile that refreshes state during the image build process:
FROM python:3.11-slim
WORKDIR /app
# Copy project files
COPY . /app/
# Install dependencies
RUN pip install -e .
# Refresh component state during build
RUN dg utils refresh-defs-state --management-type LOCAL_FILESYSTEM
This approach:
- Installs your project and its dependencies
- Runs
dg utils refresh-defs-stateto fetch fresh state from external APIs - Stores the state in the
.local_defs_statedirectory within the image
This causes the state to become part of your deployment artifact.
The .local_defs_state directory is automatically excluded from version control via an auto-generated .gitignore file. However, it should be included in your Docker image as part of your deployment artifact.
Refreshing state for Versioned State Storage components
If your components use the VERSIONED_STATE_STORAGE state management type, the refresh workflow differs between OSS and Dagster+ deployments.
Dagster+ deployments
For Dagster+ deployments, use the specialized dg plus deploy command during your GitHub or GitLab build:
dg plus deploy refresh-defs-state
This should be executed after the init step.
This command should not be executed inside a Dockerfile, as it relies on having access to the configuration available within the deploy context.
This command automatically:
- Uses your Dagster+ credentials to access the cloud instance
- Refreshes state for all
VERSIONED_STATE_STORAGEcomponents - Uploads state to Dagster+'s managed state storage
- Updates version metadata in the Dagster+ instance
For more information on deployment commands, see CI/CD guide.
Example: Dagster+ Github Action step
Here's an example of a Github Action step that refreshes state for Versioned State Storage components:
- name: Refresh defs state
if: steps.prerun.outputs.result != 'skip'
run: |
python -m pip install uv
cd path/to/project
uv run dg plus deploy refresh-defs-state
shell: bash
It first installs uv, then executes the dg plus deploy refresh-defs-state command (uv run will automatically install your project into a new virtual environment). Once this completes, new state for all of your VERSIONED_STATE_STORAGE components will be uploaded to Dagster+'s managed state storage, and your build metadata will be updated with pointers to the new state.
OSS deployments
For OSS deployments, you first need to configure a state storage backend in your Dagster instance (see Configuring versioned state storage for more information).
Refreshing state
For OSS deployments with Versioned State Storage configured, run:
dg utils refresh-defs-state --management-type VERSIONED_STATE_STORAGE
This command:
- Discovers all state-backed components using
VERSIONED_STATE_STORAGE - Fetches fresh metadata from each external system
- Generates a new UUID version identifier for each state
- Uploads state to your configured state storage backend (S3, GCS, etc.)
- Updates the instance database with the new version identifiers
The specifics of how and where you run this command will depend on your deployment strategy, but you must have access to your production DagsterInstance in order for state to be written to the correct spot.
In a k8s+helm deployment, this would mean mouting your instance ConfigMap into your pod. For more information, see Customizing your Kubernetes deployment.
Checking state status
After refreshing state, you can verify the update in the Dagster UI:
- Navigate to the Deployment tab
- Select your code location
- View the Defs state section
You'll see:
- All registered defs state keys
- The last updated timestamp for each key
- The current version identifier for each key
Handling state refresh failures
If your state refresh command fails (for example, due to API errors or network issues), the CLI exits with a non-zero status code. This typically causes your build process to fail, preventing deployment with stale or missing state.
Common causes and solutions:
- Invalid credentials: Verify API keys and secrets are correct and not expired
- Network connectivity: Ensure your CI/CD environment can reach external APIs
- Rate limiting: Space out refreshes or request higher rate limits from providers
- Service outages: Monitor external service status pages
- Transient errors: Add retry logic with exponential backoff to your deployment scripts
Next steps
- Review how to configure state-backed components
- Learn about the Components system in general