node-gradle / gradle-node-plugin

Gradle plugin for integrating NodeJS in your build. :rocket:
Apache License 2.0
599 stars 117 forks source link

How to set _authToken via pnpmCommand? #303

Open antpaw opened 6 months ago

antpaw commented 6 months ago

I'm looking for a way to do something like this. Does anyone know how I could set the token? I do not want to set it via project's .npmrc. Thank you!

  pnpmCommand.set(listOf("config set https://npm.pkg.github.com/:_authToken $authToken"))
deepy commented 6 months ago

Based on the constraints I'd recommend using an environment variable https://pnpm.io/npmrc#url_authtoken https://github.com/pnpm/pnpm/issues/3141#issuecomment-1279842014

antpaw commented 6 months ago

Thanks you for your reply!

i've tried two things, setting it the env with gradle.

val buildFrontend by tasks.registering(PnpmTask::class) {
    val version = project.property("version")
    environment.set(
        mapOf(
            "VITE_MY_VERSION" to version.toString(),
            "npm_config_//npm.pkg.github.com/:_authtoken" to authToken.toString()
        )
    )
    pnpmCommand.set(listOf("build:web"))
    dependsOn(tasks.pnpmInstall)

it still errors out on install with 401

> Task :frontend:pnpmInstall
Progress: resolved 0, reused 1, downloaded 0, added 0
 ERR_PNPM_FETCH_401  GET https://npm.pkg.github.com/some-pkg: Unauthorized - 401

and setting it directly in bash

$ export npm_config_//npm.pkg.github.com/:_authtoken=myToken            
export: not valid in this context: npm_config_//npm.pkg.github.com/:_authtoken

this errors out with not valid because of all the special chars like . and / right?

It looks like that the env example is only working for github actions? I'm running this on jenkins