Home

Awesome

Contentful Migration Automation

An action for automating contentful migrations.

To learn about making changes to a content model and entries on a Contentful Space using the Contentful CLI check out our tutorial on Scripting Migrations. You can read our conceptual guide on how to utilize Contentful Environments inside your continuous delivery pipeline.

Usage

This action runs migrations on your contentful space. Please add your migration scripts to a directory called migrations (configurable) and name them [version].js where [version] is the current version of your content-model. Versions can be integers or Semantic versions (semver)
Example: 1.js, 2.js,2.0.1-alpha.js,2.0.1-rc.js,2.0.1-rc.1.js,2.0.1-rc.2.js, 2.0.1.js ...

We recommend to not mix integers with semver but as long as they are unique it will work. 2 is the same version as 2.0and 2.0.0, therefore it can cause errors if you have several versions that are considered the same.

Screenshot of Contentful Version Tracking Entry

You can choose the initial version. For simplicity, we recommend to start with 1. Please create 1.js inside your migrations folder and include the following code:

module.exports = function () {};

For every new version we can now increase the version (2.js, 3.js, ...). Per default, this action looks for a directory labeled migrations but it's configurable via the arg migrations_dir.

Next we can adjust our workflow file to use this action. You have to include your space_id and management_api_key from your Contentful space.

There are several options to allow customizing this action.

Environment names

You can define the master_pattern and feature_pattern. Master is used as alias target-environments on contentful. Feature is used during development as a sandbox environments on contentful.

These helpers are available:

Examples

Automations

DANGER. Please make sure you know what you're doing when setting these to true.

delete_feature: Will delete the feature once it has been merged. While this is considered safe, you might want to keep the sandbox environment.

set_alias: Will set the alias to the new master environment once the feature has been merged. You might want to manually set the alias from the GUI.

Versioning

Please read the usage info above. The content-type and the field-id are configurable.

LOG_LEVEL

If you want to see more logs you can set your LOG_LEVEL to verbose. (See example workflow below)

Arguments

NameTypeRequiredDefaultDescription
space_idstringYesundefinedThe id of the contentful space
management_api_keystringYesundefinedThe management-api key for contentful
delete_featurebooleanNofalseDeletes sandbox environment if the head branch is merged
set_aliasbooleanNofalseAliases master the new master environment
master_patternstringNomaster-[YYYY]-[MM]-[DD]-[hh][mm]The pattern that should be used for the new master environment on contentful
feature_patternstringNoGH-[branch]The pattern that should be used for the new feature environments on contentful
version_content_typestringNoversionTrackingThe content-type that tracks the version
version_fieldstringNoversionThe field-id that carries the version number
migrations_dirstringNomigrationsThe directory to look for migrations

Workflow

Please look at the demo file.

- name: Contentful Migration
  id: migrate
  uses: contentful-userland/contentful-migration-automation@v1
  with:
    # delete_feature: true
    # set_alias: true
    # master_pattern: "main-[YY]-[MM]-[DD]-[hh]-[mm]"
    # feature_pattern: "sandbox-[branch]"
    # version_field: versionCounter
    # version_content_type: environmentVersion
    # migrations_dir: contentful/migrations
    space_id: ${{ secrets.SPACE_ID }}
    management_api_key: ${{ secrets.MANAGEMENT_API_KEY }}
  # env:
    # LOG_LEVEL: verbose

Contributors

Thanks to our community members who have contributed code to this action. A full list of community contributors to the action are listed below, in alphabetical order:

License

Copyright (c) 2021 Contentful GmbH. Code released under the MIT license.. See LICENSE for further details.