Skip to content

Contentful Schema Migrate #35

Contentful Schema Migrate

Contentful Schema Migrate #35

name: Contentful Schema Migrate
on:
workflow_dispatch:
inputs:
target_environment:
required: true
type: string
env:
MANAGEMENT_TOKEN: ${{ secrets.CONTENTFUL_MANAGEMENT_TOKEN }}
DELIVERY_KEY: ${{ secrets.TF_VAR_CPD_DELIVERY_KEY }}
SPACE_ID: ${{ secrets.TF_VAR_CPD_SPACE_ID }}
ENVIRONMENT: ${{ inputs.target_environment }}
jobs:
setup:
runs-on: ubuntu-latest
outputs:
staging-environment: ${{ steps.staging-env.outputs.staging-environment }}
required-migrations: ${{ steps.required-migrations.outputs.required-migrations }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install npm packages used by util scripts
working-directory: ./Contentful-Schema
run: npm ci
- name: Get target environment current migration version
id: current-migration-version
working-directory: ./Contentful-Schema/utils
run: node get-environment-version.js
- name: Determine timestamp for new staging environment
run: echo "TIMESTAMP=$(date +%Y-%m-%d-%H-%M-%S)" >> $GITHUB_ENV
- name: Set var for name of new environment
id: staging-env
run: echo "staging-environment=$(echo ${{ inputs.target_environment }}-${{ env.TIMESTAMP }})" >> $GITHUB_OUTPUT
- name: Determine required migrations for environment
id: required-migrations
working-directory: ./Contentful-Schema/utils
run: node get-required-migrations.js --currentVersion ${{ steps.current-migration-version.outputs.migration-version }}
clone:
if: ${{ join(needs.setup.outputs.required-migrations, '') != '[]' }}
needs: [setup]
runs-on: ubuntu-latest
steps:
- name: Install Contentful CLI
run: npm install -g contentful-cli
- name: Login to Contentful with management token
run: contentful login --management-token "${{ env.MANAGEMENT_TOKEN }}"
- name: Clone target environment to staging environment
run: contentful space environment create --name ${{ needs.setup.outputs.staging-environment }} --environment-id ${{ needs.setup.outputs.staging-environment }} --source ${{ inputs.target_environment }}
migrate:
if: ${{ join(needs.setup.outputs.required-migrations, '') != '[]' }}
needs: [setup,clone]
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix:
value: ${{fromJSON(needs.setup.outputs.required-migrations)}}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v4
with:
node-version: 20
# - name: Install npm packages used by migration script
# working-directory: ./Contentful-Schema
# run: npm ci
#
# - name: Install Contentful CLI
# run: npm install -g contentful-cli
- name: Display migration to run ${{ matrix.value }}
run: echo "${{ matrix.value }}"
#
# - name: Run migration script ${{ matrix.value }}
# run: contentful space migration --space-id "${{ vars.SPACE_ID }}" --environment-id ${{ needs.setup.outputs.staging-environment }} --management-token "${{ secrets.CONTENTFUL_CLI_TOKEN }}" ./schema/migrations/${{ matrix.value }} --yes
#
# - name: Update environment's migration version
# run: node ./schema/utils/set-environment-version.js --token ${{ secrets.MANAGEMENT_API_KEY }} --space ${{ vars.SPACE_ID }} --environment ${{ needs.setup.outputs.staging-environment }} --migrationFilename ${{ matrix.value }}