Gathering detailed insights and metrics for contentful-cli-migrations
Gathering detailed insights and metrics for contentful-cli-migrations
Gathering detailed insights and metrics for contentful-cli-migrations
Gathering detailed insights and metrics for contentful-cli-migrations
npm install contentful-cli-migrations
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2 Stars
38 Commits
1 Forks
2 Watching
1 Branches
1 Contributors
Updated on 27 Aug 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-92.9%
1
Compared to previous week
Last month
-43.9%
60
Compared to previous month
Last year
122.4%
1,112
Compared to previous year
9
Contentful CLI Migrations is a JavaScript open-source library for automating Contentful migrations. It provides a convenient way to manage and apply migrations to your Contentful space using the command line.
Note: This is NOT the official Contentful Migration tool, but it's based on it. That can be found on GitHub π or NpmJS π
β¨ Features Β· π‘ Installation Β· π Example Β· πΉ Usage Β· π¦ Write Migrations Β· π ToDo Β· πΎ Contributors Β· π© Acknowledgments Β· π Collection Β· π License
To use this helper library, you must have Node.js π and npm π installed.
To install it, simply run:
1npm install contentful-cli-migrations --save
Or, if using yarn π:
1yarn add contentful-cli-migrations
Similarly, if you are using Bun π, just run:
1bun add contentful-cli-release
node
>= 18.20.0npm
>= 10.5.0contentful-management
>= 11.31.7contentful-migration
^4.23.2Create a migrations/scripts/
directory in your project. This directory will contain your migration scripts.
Define your migration scripts inside the migrations/scripts/
directory. Each migration script should be as follows:
0001-migration.cjs
, 0002-migration.cjs
, and so on.-
). In this way the script can intercept
them.0001-
and not 1-
).0002
and 02
will be considered duplicated)..js
or .cjs
depending on the use of "type": "module"
in your package.json.In Contentful, you will need what we call a 'Counter' entry. It means that you will need to create an entry and assign one of its field to register the latest migration that has run successfully.
en-US
or en-GB
.Note: the entry can stay in 'draft' status, since it doesn't need to be published to be used.
Set up your .env
/.env.local
configuration file to specify the required environment and migration settings. The
configuration options include:
CMS_MANAGEMENT_TOKEN
: The Contentful CMA token for accessing your Contentful space.CMS_SPACE_ID
: The ID of your Contentful space.CMS_MIGRATIONS_DIR
: The path to the migrations directory in your project.CMS_MIGRATIONS_COUNTER_ID
: The ID of the entry used to store the migration counter.CMS_MIGRATIONS_COUNTER_FIELD
: The ID of the field in the counter entry that stores the actual migration counter.CMS_MIGRATIONS_COUNTER_LOCALE
: The locale used to retrieve the migration counter from the field.You can define these options in a configuration file (e.g., .env) or pass them as command-line arguments when running the migrations.
When you start your project with contentful you usually start with an empty master
environment. We have few advices
on how to keep the migrations as clean as possible for different environments:
en-US
) and eventually additional locales.master
environment as a new environment (ie: empty-DO-NOT-DELETE
).
This will ensure a clean environment where you could run a Contentful import or the migrations.--initialise
to
set up the counter content-type and entry for you.master
environment, take note of the entry-id, field-id and locale. When
duplicating master
for your dev
or staging
environments, these values will remain the same (so you won't have to
get confused with different values for different environments).Once everything is set up, running migrations is actually pretty simple
1npx contentful-cli-migrations --to "<environment-id>"
The --to
(or --environment-id
) is the only mandatory command line option. And it tells the environment to apply the
migrations 'to'.
Since the management token would give you access to all environments, there is no default environment value in the
.env
file, forcing you to specify the target environment for 'safety' reasons (surely you don't want to run them on
master
accidentally).
This script can be used from the command line and accepts various arguments for customization:
--to
or --environment-id
[MANDATORY]: The environment id to which migrations will be executed.--space-id
: The Contentful space id.--management-token
or --mt
: The Contentful Management Token.--migrations-dir
: To specify a custom directory for the migrations (default is sub-directory CMS_MIGRATIONS_DIR
or migrations/scripts
in your project root). The script will exit if this custom folder doesn't exist.--initialise
: This might be needed the first time to 'override' trying to read a Content-type and an Entry that
don't exist yet. If you already have your environment set up, you can just create a counter entry of one of your
existing Content-types, and skip this option.--counter-id
: The ID of the entry used to store the migration counter.--counter-field
: The ID of the field in the counter entry that stores the actual migration counter. Example: title
.--counter-locale
: The locale used to retrieve the migration counter from the field. Default: en-US
--force-yes
: The script runs a migration at a time, asking you to confirm it manually (Y/N). In a CD/CI pipeline,
you will need to set up --force-yes
so that all migrations will run without any request for interaction.There are some guidelines and some few tricks that can be implemented when writing a Contentful migration. For more info, look at the separate documentation on How to Write Migrations.
@fciacchi | @psyvic | @aalduz | @leslyto |
Feel free to open issues or pull requests in our GitHub Repository if you have suggestions or improvements to propose.
I would like to express my gratitude to the following parties:
Thank you to everyone involved!
We produce a bunch of interesting packages for Contentful. You might want to check them out:
This project is licensed under the MIT License
No vulnerabilities found.
No security vulnerabilities found.