🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Troubleshooting logging in to the CLI tool using a token in a CI/CD environment.

22 replies
Last updated: Jul 28, 2020
Hello. We are looking to use an automated system for deploying GraphQL schemas and studio, but we have some problems logging in to the CLI tool. Is it possible to use a token for logging in, or is it another method?
Jul 28, 2020, 7:02 AM
Hi User, you can create a deploy token for this purpose. To do so, go to your project on manage.sanity.io and then Settings > API > Tokens > Add new token - make sure to choose “Deploy token” under rights 🙂
Jul 28, 2020, 7:36 AM
Hi again
user M
, thank you for the reply. Where would we use this token when we need to log in?
Jul 28, 2020, 7:38 AM
Are you able to set it up as an environment variable in your pipeline? What are you using for automation?
Jul 28, 2020, 7:45 AM
We are trying to use Octopus. We added the token to the environmental variables but we can't figure out how to use the token to log us in to the CLI tool, so we can use commands like
sanity deploy
,
sanity graphql deploy
automatically.
Jul 28, 2020, 7:53 AM
Try running the commands as follows:
SANITY_AUTH_TOKEN=<token> sanity <command>
(Using the relevant env var for your token)
Jul 28, 2020, 8:00 AM
Thank you very much
user M
! 😃
Jul 28, 2020, 8:04 AM
user M
It does not seem to work, unfortunately. When we try to provide the SANITY_AUTH_TOKEN before the sanity command it doesn't recognize it as a command, and when we provide it after the command it gives an error saying we have to use
sanity login
.Is there an argument option to provide token, say
sanity graphql list --token SANITY_AUTH_TOKEN=<token>
?
Jul 28, 2020, 8:41 AM
As an alternative, does the following approach produce any different results for you?
sanity <command> --with-user-token
Jul 28, 2020, 8:47 AM
user M
this error message is returned, I've tried a few different variations of the command you sent. I am currently just trying the commands in my personal terminal logged out to replicate the CI environment.
Jul 28, 2020, 9:32 AM
Could you try creating a Read+Write token and running the commands with that one locally to test?
Jul 28, 2020, 9:43 AM
By for example running
sanity graphql list --with-user-token <token>
?If so, it returned the same error message as above.
Jul 28, 2020, 9:46 AM
And this one with the R+W token?
SANITY_AUTH_TOKEN=<token> sanity <command>
Jul 28, 2020, 9:48 AM
Could you try creating a Read+Write token and running the commands with that one locally to test?
Jul 28, 2020, 9:43 AM
The term 'SANITY_AUTH_TOKEN=<token> is not recognized as the name of a cmdlet, function, script file, or operable program.
When I ran
SANITY_AUTH_TOKEN=<token> sanity <command>
with the R+W token as the &lt;token&gt;.
Jul 28, 2020, 9:50 AM
That’s usually an issue with the path variable. I guess you’re running PowerShell?
Jul 28, 2020, 9:53 AM
Okey, do you suggest that I should configure the path variable to sanity? Do you have any documentation to set it up correctly?
Jul 28, 2020, 9:57 AM
user M
would love to know if you guys have any idea how to setup this in our CD environment with Powershell.I've tried a few variations in Powershell, like using
$
in front of the
$SANITY_AUTH_TOKEN
. Would also be nice to see some more documentation on this topic in the future. 🙂Cheers
🤓
Jul 28, 2020, 11:23 AM
Absolutely, User, we should give some attention to documenting CI/CD flows in general. It’s too confusing right now and there a bunch of different options each with specific challenges, mostly around authentication though.
Unfortunately I’ve never used PowerShell myself, so I can only assist from the sidelines here. I’ve seen it being a path variable issue, at least when that error message you shared above occurs. However, it usually throws the error for any
sanity <command>
in that case, not specifically for the
SANITY_AUTH_TOKEN=
portion. I’d be happy to check with the team to see if anyone’s more familiar with PS.
Jul 28, 2020, 11:27 AM
That would be lovely
user M
, thanks.Note that I'm not seeing the
not recognized
error message when not using the
SANITY_AUTH_TOKEN
in front of the command.
Jul 28, 2020, 11:30 AM
user M
I figured it out. In Powershell you have to define the environment variable like so:
$env:SANITY_AUTH_TOKEN=<token> ; sanity <command>
and notice the
;
to separate the command from setting the variable. I was inspired by this Stack Overflow answer: https://stackoverflow.com/a/1422082 .Thanks for all the help
🤓
Jul 28, 2020, 12:09 PM
This is awesome,
user M
! I’ll note this in bold for when we write up those docs on CI/CD. Thanks for sharing the solution - great catch 🎉
Jul 28, 2020, 12:15 PM
This is awesome,
user M
! I’ll note this in bold for when we write up those docs on CI/CD. Thanks for sharing the solution - great catch 🎉
Jul 28, 2020, 12:15 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?