github-action-sanity icon indicating copy to clipboard operation
github-action-sanity copied to clipboard

Error: sanity.cli.ts does not contain a project identifier

Open gislitg opened this issue 1 year ago • 2 comments

I am getting the error: "Error: sanity.cli.ts does not contain a project identifier ("api.projectId"), which is required for the Sanity CLI to communicate with the Sanity API"

To Reproduce

Steps to reproduce the behavior:

  1. Use the export/backup example code but use the latest version 0.7-alpha
  2. Used a read+write token since I was also going to use the sanity dataset import but never got to that point
  3. Received error

Expected behavior

The workflow should succeed

Screenshots

Screenshot 2023-12-15 at 09 03 57

Which versions of Sanity are you using? v3.21.3

gislitg avatar Dec 15 '23 09:12 gislitg

I think this is an issue with the latest sanity cli because when you use a js file instead of ts then it does work

WahidN avatar Mar 25 '24 11:03 WahidN

In my case (and probably most of you too if you've followed Sanity's setup for NextJS), the issue is that I read these variables from the environment.

import { defineCliConfig } from "sanity/cli";

const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID;
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET;

export default defineCliConfig({
  api: { projectId, dataset },
});

Providing those in the env of the workflow step fixed this specific issue.

- name: Export Dataset
  uses: sanity-io/[email protected]
  with:
    args: dataset export [DATASET] [UPLOAD_FILE_PATH]
  env:
    SANITY_AUTH_TOKEN: [TOKEN]
    NEXT_PUBLIC_SANITY_PROJECT_ID: [PROJECT_ID]
    NEXT_PUBLIC_SANITY_DATASET: [DATASET]

Alternatively, you could also have a step that deletes the file and rewrites it with the values populated:

- run: |
    rm sanity.cli.ts && echo 'export default { api: { projectId: "PROJECT_ID", dataset: "DATASET" } }' > sanity.cli.ts

However, I then got a new error, which has been reported here: https://github.com/sanity-io/github-action-sanity/issues/23 . The workaround suggested by this comment fixed the new error. This was my final configuration to make it work with the latest CLI (3.43.0):

- name: Export dataset
  run: npx sanity dataset export ${{ inputs.dataset || 'production' }} ${{ steps.upload_name.outputs.UPLOAD_NAME }}
  env:
    SANITY_AUTH_TOKEN: ${{ secrets.SANITY_TOKEN }}
    NEXT_PUBLIC_SANITY_PROJECT_ID: ${{ secrets.SANITY_PROJECT_ID }}
    NEXT_PUBLIC_SANITY_DATASET: ${{ inputs.dataset || 'production' }}

maxijonson avatar May 26 '24 04:05 maxijonson