You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-09-16 09:26:22 +02:00
feat (npmExecuteScripts) create seperate npmrc file for publish to private repo (#3422)
* creating new npm rc file * publishing to registry staging * exposing base64 version of env variables * changing encoding param * fixing unit test for the new path * debugging env var * remove debug message * update docu * changing new npmrc file name * adding new npmrc to ignore * adding new npmrc to ignore Co-authored-by: anilkeshav27 <you@example.com>
This commit is contained in:
@@ -115,7 +115,7 @@ The `vaultCredentialPath` parameter is the endpoint of your credential path in V
|
|||||||
2. `<vaultBasePath>/<vaultPipelineName>/<vaultCredentialPath>`
|
2. `<vaultBasePath>/<vaultPipelineName>/<vaultCredentialPath>`
|
||||||
3. `<vaultBasePath>/GROUP-SECRETS/<vaultCredentialPath>`
|
3. `<vaultBasePath>/GROUP-SECRETS/<vaultCredentialPath>`
|
||||||
|
|
||||||
The `vaultCredentialKeys`parameter is a list of credential IDs. The secret value of the credential will be exposed as an environment variable prefixed by "PIPER_VAULTCREDENTIAL_" and transformed to a valid variable name. For a credential ID named `myAppId` the forwarded environment variable to the step will be `PIPER_VAULTCREDENTIAL_MYAPPID` containing the secret. Hyphens will be replaced by underscores and other non-alphanumeric characters will be removed.
|
The `vaultCredentialKeys`parameter is a list of credential IDs. The secret value of the credential will be exposed as an environment variable prefixed by "PIPER_VAULTCREDENTIAL_" and transformed to a valid variable name. For a credential ID named `myAppId` the forwarded environment variable to the step will be `PIPER_VAULTCREDENTIAL_MYAPPID` containing the secret. The Base64 encoded secret value will be exposed as environment variable to the step as `PIPER_VAULTCREDENTIAL_MYAPPID_BASE64`. Hyphens will be replaced by underscores and other non-alphanumeric characters will be removed.
|
||||||
|
|
||||||
!!! hint "Using a custom prefix for test credentials"
|
!!! hint "Using a custom prefix for test credentials"
|
||||||
By default the prefix for test credentials is `PIPER_VAULTCREDENTIAL_`.
|
By default the prefix for test credentials is `PIPER_VAULTCREDENTIAL_`.
|
||||||
|
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/SAP/jenkins-library/pkg/config/interpolation"
|
"github.com/SAP/jenkins-library/pkg/config/interpolation"
|
||||||
"github.com/SAP/jenkins-library/pkg/log"
|
"github.com/SAP/jenkins-library/pkg/log"
|
||||||
|
CredentialUtils "github.com/SAP/jenkins-library/pkg/piperutils"
|
||||||
"github.com/SAP/jenkins-library/pkg/vault"
|
"github.com/SAP/jenkins-library/pkg/vault"
|
||||||
"github.com/hashicorp/vault/api"
|
"github.com/hashicorp/vault/api"
|
||||||
)
|
)
|
||||||
@@ -280,6 +281,9 @@ func populateCredentialsAsEnvs(config *StepConfig, secret map[string]string, key
|
|||||||
envVariable := vaultCredentialEnvPrefix + convertEnvVar(secretKey)
|
envVariable := vaultCredentialEnvPrefix + convertEnvVar(secretKey)
|
||||||
log.Entry().Debugf("Exposing general purpose credential '%v' as '%v'", key, envVariable)
|
log.Entry().Debugf("Exposing general purpose credential '%v' as '%v'", key, envVariable)
|
||||||
os.Setenv(envVariable, secretValue)
|
os.Setenv(envVariable, secretValue)
|
||||||
|
envVariable = vaultCredentialEnvPrefix + convertEnvVar(secretKey) + "_BASE64"
|
||||||
|
log.Entry().Debugf("Exposing general purpose base64 encoded credential '%v' as '%v'", key, envVariable)
|
||||||
|
os.Setenv(envVariable, CredentialUtils.EncodeString(secretValue))
|
||||||
matched = true
|
matched = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -295,6 +299,9 @@ func populateCredentialsAsEnvs(config *StepConfig, secret map[string]string, key
|
|||||||
envVariable := vaultCredentialEnvPrefixDefault + convertEnvVar(secretKey)
|
envVariable := vaultCredentialEnvPrefixDefault + convertEnvVar(secretKey)
|
||||||
log.Entry().Debugf("Exposing general purpose credential '%v' as '%v'", key, envVariable)
|
log.Entry().Debugf("Exposing general purpose credential '%v' as '%v'", key, envVariable)
|
||||||
os.Setenv(envVariable, secretValue)
|
os.Setenv(envVariable, secretValue)
|
||||||
|
envVariable = vaultCredentialEnvPrefixDefault + convertEnvVar(secretKey) + "_BASE64"
|
||||||
|
log.Entry().Debugf("Exposing general purpose base64 encoded credential '%v' as '%v'", key, envVariable)
|
||||||
|
os.Setenv(envVariable, CredentialUtils.EncodeString(secretValue))
|
||||||
matched = true
|
matched = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
configFilename = ".npmrc"
|
configFilename = ".piperNpmrc"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package npm
|
package npm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -20,9 +21,9 @@ func TestNewNPMRC(t *testing.T) {
|
|||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{name: "current dir", args: args{""}, want: configFilename},
|
{name: "current dir", args: args{""}, want: configFilename},
|
||||||
{name: "sub dir", args: args{mock.Anything}, want: mock.Anything + "/.npmrc"},
|
{name: "sub dir", args: args{mock.Anything}, want: filepath.Join(mock.Anything, ".piperNpmrc")},
|
||||||
{name: "file path in current dir", args: args{".npmrc"}, want: ".npmrc"},
|
{name: "file path in current dir", args: args{".piperNpmrc"}, want: ".piperNpmrc"},
|
||||||
{name: "file path in sub dir", args: args{mock.Anything + "/.npmrc"}, want: mock.Anything + "/.npmrc"},
|
{name: "file path in sub dir", args: args{filepath.Join(mock.Anything, ".piperNpmrc")}, want: filepath.Join(mock.Anything, ".piperNpmrc")},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
@@ -50,13 +50,18 @@ func (exec *Execute) publish(packageJSON, registry, username, password string) e
|
|||||||
npmignore.Add("**/piper")
|
npmignore.Add("**/piper")
|
||||||
log.Entry().Debug("adding **/sap-piper")
|
log.Entry().Debug("adding **/sap-piper")
|
||||||
npmignore.Add("**/sap-piper")
|
npmignore.Add("**/sap-piper")
|
||||||
// update .npmrc
|
|
||||||
|
npmrc := NewNPMRC(filepath.Dir(packageJSON))
|
||||||
|
|
||||||
|
log.Entry().Debugf("adding piper npmrc file %v", npmrc.filepath)
|
||||||
|
npmignore.Add(npmrc.filepath)
|
||||||
|
|
||||||
if err := npmignore.Write(); err != nil {
|
if err := npmignore.Write(); err != nil {
|
||||||
return errors.Wrapf(err, "failed to update %s file", npmignore.filepath)
|
return errors.Wrapf(err, "failed to update %s file", npmignore.filepath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update .piperNpmrc
|
||||||
if len(registry) > 0 {
|
if len(registry) > 0 {
|
||||||
npmrc := NewNPMRC(filepath.Dir(packageJSON))
|
|
||||||
// check existing .npmrc file
|
// check existing .npmrc file
|
||||||
if exists, err := FileUtils.FileExists(npmrc.filepath); exists {
|
if exists, err := FileUtils.FileExists(npmrc.filepath); exists {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -67,7 +72,7 @@ func (exec *Execute) publish(packageJSON, registry, username, password string) e
|
|||||||
return errors.Wrapf(err, "failed to read existing %s file", npmrc.filepath)
|
return errors.Wrapf(err, "failed to read existing %s file", npmrc.filepath)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Entry().Debug("creating .npmrc file")
|
log.Entry().Debugf("creating new npmrc file at %s", npmrc.filepath)
|
||||||
}
|
}
|
||||||
// set registry
|
// set registry
|
||||||
log.Entry().Debugf("adding registry %s", registry)
|
log.Entry().Debugf("adding registry %s", registry)
|
||||||
@@ -86,7 +91,7 @@ func (exec *Execute) publish(packageJSON, registry, username, password string) e
|
|||||||
log.Entry().Debug("no registry provided")
|
log.Entry().Debug("no registry provided")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := execRunner.RunExecutable("npm", "publish", filepath.Dir(packageJSON))
|
err := execRunner.RunExecutable("npm", "publish", "--userconfig", npmrc.filepath, "--registry", registry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user