Contentful logo

Contentful Community

Programmatically use of contentful-migration-cli

#1

Hi,

I’m testing the new contentful-migration-cli package.

It’s possibile to use it programmatically like the contentful-extension-cli?

Something like this:

const migration = require('contentful-migration-cli')
const client = migration.createClient({
    accessToken: process.env.CONTENTFUL_MANAGEMENT_ACCESS_TOKEN
})
...

The cli approach is less flexible and more difficult to integrate with our Build/Delivery system.

Thank you,
Vincenzo

0 Likes

#2

Hey @vmilone,

Welcome to Contentful Community! It’s unfortunately not available at the moment, but that’s planned.
However, you can workaround this. In a build process you can often execute more than one shell command - we do respect the exit codes, so you could catch a failed migration (e.g. in most CIs nothing further gets executed if the prior process exited with something different than 0)
Also you can always spawn a shell within Node, which then does the migration and wrap that in a promise - combine it with https://github.com/sindresorhus/dargs and you basically can simulate the behaviour of extension-cli's programmatic usage.

Let me know if that help :slight_smile:

0 Likes

#3

Hi @andrey,

thank you for the answer.

I like dargs, I’ll give it a shot.

Vincenzo

1 Like

#4

@vmilone Awesome! Just let us know if you need some help with this.

0 Likes

#5

Hi @andrey

I re-open this discussion because I’m trying to use contentful-migration-cli inside our build system.
We use Jenkins to generate artifacts. When Jenkins launch
contentful-migration --space-id vdg1mwfhyz60 migrations/pictures/banner_full.js
the cli asks “Do you want to apply the migration?”.

No bodies can answer Yes: it’a an automatic task, so the build fails. It’s possible to force the migration without the input prompt?

V.

0 Likes

#6

HI there,
Unfortunately there is no argument to auto respond for now, but you can tell jenkins to answer yes when receiving that question from the migration-cli using shell script.

Something like this

expect -c "
spawn contentful-migration --space-id $SPACE_ID $MIGRATION_FILE
set timeout -1
expect \"Do you want to apply the migration (Y/n)\"
send \"y\r\"
expect eof
"
2 Likes

#7

Hi @vmilone,

Khaled was faster :rocket:

0 Likes

#8

It should also be possible to pipe Unix yes into the process (but I haven’t tried this)

1 Like

#9

Hi,

I got inspiration from Stephan’s suggestion and I wrote:
echo yes | contentful-migration --space-id vdg1mwfhyz60 migrations/pictures/slider_gallery.js

It works!

Thank you.

1 Like

#10

Hi @vmilone,

I am also looking solution automate the migration using Jenkins. Could you please help us how you have done build setup in Jenkins job?

Thanks,
Venkatesh. A

0 Likes