Apply a schema from CI
You can keep the shape of your content types and fields (the schema) as a schema file (.hako.json) in the same repository as your site’s code. When you change the file, check the diff, then apply it to the space from CI. You can also use the schema file to bring a production space in line with content types you built in a test space.
Prerequisites
Section titled “Prerequisites”- You have installed
hakocms-cliand logged in (Install the CLI, Log in with the CLI) - In the space you apply to, you have a role that can change content types (owner or admin)
Get the schema file
Section titled “Get the schema file”Save the current space’s schema to a file.
hakocms-cli schema pull --space blog✓ Saved blog.hako.json (612 bytes)blog.hako.json contains all of the space’s content types and their fields. It does not contain entries or media.
{ "format": "hako-space", "version": 1, "source": { "space": "blog", "spaceName": "ブログ" }, "contentTypes": [ { "endpoint": "posts", "name": "記事", "kind": "collection", "schema": { "fields": [ { "fieldId": "title", "name": "タイトル", "required": true, "kind": "text", "unique": false }, { "fieldId": "body", "name": "本文", "required": false, "kind": "textArea" } ] } } ]}How to write fields is explained in Field types and value formats. Add this file to your repository.
Check the diff
Section titled “Check the diff”After you edit the file, check what will change before you apply it to the space. schema diff changes nothing.
hakocms-cli schema diff blog.hako.json --space blogFor each content type, the output marks additions with +, changes with ~, and deletions with -. It also shows the fields that change and the number of entries that will no longer match the new schema. For example, if you add “news” to the file and change fields of “posts”, you see the following. The first two lines show progress while the diff is being created.
apply o4itgj7ry38v: queuedapply o4itgj7ry38v: planned+ news (create)~ posts (update: ~fields.title, ~fields.body, 2 entries violate)Content types that are not in the file are shown with -. For example, after you apply the diff above and “news” exists, if you remove “posts” from the file, you see the following (progress lines omitted). “posts” is not deleted unless you apply with --prune (see “When you delete, and when entries do not match” below).
- posts (delete with 2 entries; needs --prune) unchanged: newsIf there is nothing to change, No changes is shown.
Apply from your computer
Section titled “Apply from your computer”hakocms-cli schema apply blog.hako.json --space blogThe diff is shown, and the CLI asks whether to apply it. Press y to apply.
✓ Applied the schema to blog- If there is nothing to change, the CLI does nothing, shows
No changes, and ends with exit code 0 - If there is a reason it cannot apply (for example, current entry values do not match the new schema), it changes nothing, shows the reason, and ends with exit code 1
When you delete, and when entries do not match
Section titled “When you delete, and when entries do not match”A normal schema apply makes only safe changes. The following changes require a flag.
| Change | Required flag | What happens |
|---|---|---|
| Delete content types that are not in the file | --prune |
Those content types are deleted together with their entries. This cannot be undone |
| Current entry values do not match the new schema (you made a field required, lowered the maximum length, and so on) | --allow-violations |
Entries that do not match stay as they are. The next time they are published, you are asked to fix them |
If a required flag is missing, the CLI changes nothing, shows the reason, and ends with exit code 1.
error: the schema cannot be applied:posts: 2 entries violate the new schema; confirm to apply anyway (HTTP 409)Without --prune, content types that are not in the file remain in the space. If deletions are the only difference and there is nothing else to change, nothing is changed: No changes and the names of the content types that were kept (Kept content types that are not in the file: …) are shown, and the CLI ends with exit code 0.
Apply with GitHub Actions
Section titled “Apply with GitHub Actions”This example applies changes to the production space when you change schema/blog.hako.json in the repository and merge it into main.
- In the admin, create an access token for CI. Set [名前](Name) to “GitHub Actions”, and set [できること](Allowed actions) to [すべて(あなたと同じ)](Everything (same as you)) (Log in with a token created in the admin).
- In your GitHub repository, go to [Settings]>[Secrets and variables]>[Actions], and add the token to secrets with the name
HAKOCMS_TOKEN. - Put the following workflow in
.github/workflows/hakocms-schema.yml. Replace<hostname>with the hostname of your environment.
name: Hako CMS schema
on: pull_request: paths: ["schema/**"] push: branches: [main] paths: ["schema/**"]
permissions: contents: read
jobs: schema: runs-on: ubuntu-latest env: HAKOCMS_HOST: https://<hostname>.hakocms.net HAKOCMS_TOKEN: ${{ secrets.HAKOCMS_TOKEN }} HAKOCMS_SPACE: blog # Pin the version to use (version in latest.json; change it here to upgrade) HAKOCMS_CLI_VERSION: "0.1.8" steps: - uses: actions/checkout@v7 - name: Install hakocms-cli run: | base=https://download.hakocms.net/hakocms-cli/v$HAKOCMS_CLI_VERSION name=hakocms-cli-v$HAKOCMS_CLI_VERSION-x86_64-unknown-linux-musl curl -fsSLO "$base/$name.tar.gz" curl -fsSLO "$base/SHA256SUMS" grep " $name.tar.gz\$" SHA256SUMS | sha256sum -c - tar xzf "$name.tar.gz" sudo install "$name/hakocms-cli" /usr/local/bin/ # On a PR, only show the diff (change nothing) - if: github.event_name == 'pull_request' run: hakocms-cli schema diff schema/blog.hako.json # Apply after merging into main - if: github.event_name == 'push' run: hakocms-cli schema apply schema/blog.hako.json --force- CI cannot ask whether to apply, so add
--force. Without it,needs --force when it cannot askis shown, and the CLI ends without changing anything - The diff for a PR appears in the workflow run log
- For changes that delete or that leave entries that do not match (
--prune,--allow-violations), we recommend that you do not leave them to CI, and instead apply them from your computer after checking the diff - Pin the version to use. If you install the latest version from
latest.jsonevery time, the behavior can change without you noticing when the CLI is updated. The latest version number is inversionathttps://download.hakocms.net/hakocms-cli/latest.json
Verify
Section titled “Verify”In the admin, open the content types of the space you applied to. The same fields as in the file are listed.
To check from your computer, run schema diff again. If No changes is shown, the file and the space match.
Generate entry types
Section titled “Generate entry types”(Optional) You can generate TypeScript types for entries from the schema. Use them in your site’s code to type Content API responses.
hakocms-cli schema types --space blog > src/hakocms-types.tsYou can also generate a GraphQL schema (--format graphql) and an OpenAPI description of the Content API (--format openapi).