saitho / semantic-release-backmerge

:twisted_rightwards_arrows: semantic-release plugin to back-merge a release into a develop branch
https://saitho.github.io/semantic-release-backmerge/
58 stars 11 forks source link

Git Authentication issues on Jenkins job #12

Open Lacront opened 3 years ago

Lacront commented 3 years ago

Hey, I sadly can't get this plugin to work in my Jenkins environment due to Git authentication errors when the plugin attempts to execute git fetch.
The weird thing is that semantic-release itself has no issues with git. It can push version commits and set git tags perfectly fine.

Here is my plugin config:

"release": {
    "branches": [
      "master",
      {
        "name": "develop",
        "prerelease": "dev"
      }
    ],
    "plugins": [
      "@semantic-release/commit-analyzer",
      "@semantic-release/release-notes-generator",
      [
        "@semantic-release/npm",
        {
          "npmPublish": false
        }
      ],
      [
        "@semantic-release/git",
        {
          "message": "chore: [RELEASE] Upgrade to ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
        }
      ],
      [
        "@saithodev/semantic-release-backmerge",
        {
          "branchName": "develop",
          "backmergeStrategy": "merge",
          "message": "chore: [RELEASE] Preparations for next release ${nextRelease.version} [skip ci]"
        }
      ]
    ]
  }

Here is my build step:

stage('Create Version') {
  when {
    expression {
      return !noActualChanges
    }
  }
  steps {
    withCredentials([usernameColonPassword(credentialsId: JENKINS_GIT_CREDENTIALS_ID, variable: 'GIT_CREDENTIALS')]) {
      nodejs(JENKINS_NODE_JS_INSTALLATION_LABEL) {
        sh 'npx semantic-release'
      }
    }
    withCredentials([usernamePassword(credentialsId: JENKINS_GIT_CREDENTIALS_ID, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
      sh("git push https://${GIT_USERNAME}:${GIT_PASSWORD}@${GIT_REPO} ${GIT_BRANCH} --tags")
    }
  }
}

And here the error that is thrown:

[5:52:50 PM] [semantic-release] › ✔  Completed step "prepare" of plugin "@semantic-release/npm"
[5:52:50 PM] [semantic-release] › ℹ  Start step "prepare" of plugin "@semantic-release/git"
[5:52:50 PM] [semantic-release] [@semantic-release/git] › ℹ  Found 2 file(s) to commit
[5:52:51 PM] [semantic-release] [@semantic-release/git] › ℹ  Prepared Git release: v1.0.81
[5:52:51 PM] [semantic-release] › ✔  Completed step "prepare" of plugin "@semantic-release/git"
[5:52:51 PM] [semantic-release] › ℹ  Start step "generateNotes" of plugin "@semantic-release/release-notes-generator"
[5:52:51 PM] [semantic-release] › ✔  Completed step "generateNotes" of plugin "@semantic-release/release-notes-generator"
[5:52:53 PM] [semantic-release] › ✔  Created tag v1.0.81
[5:52:53 PM] [semantic-release] › ℹ  Start step "publish" of plugin "@semantic-release/npm"
[5:52:53 PM] [semantic-release] [@semantic-release/npm] › ℹ  Skip publishing to npm registry as npmPublish is false
[5:52:53 PM] [semantic-release] › ✔  Completed step "publish" of plugin "@semantic-release/npm"
[5:52:53 PM] [semantic-release] › ℹ  Start step "success" of plugin "@saithodev/semantic-release-backmerge"
[5:52:53 PM] [semantic-release] [@saithodev/semantic-release-backmerge] › ℹ  Release succeeded. Performing back-merge into branch "develop".
[5:52:53 PM] [semantic-release] › ✖  Failed step "success" of plugin "@saithodev/semantic-release-backmerge"
[5:52:53 PM] [semantic-release] › ✖  An error occurred while running semantic-release: Error: Command failed with exit code 128: git fetch
fatal: Authentication failed for 'https://tasktrack..../bitbucket/scm/ccf/microfrontend-...-next.git/'
    at makeError (/home/jenkins/workspace/microfrontend-...-next CD/node_modules/@saithodev/semantic-release-backmerge/node_modules/execa/lib/error.js:59:11)
    at handlePromise (/home/jenkins/workspace/microfrontend-...-next CD/node_modules/@saithodev/semantic-release-backmerge/node_modules/execa/index.js:114:26)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  shortMessage: 'Command failed with exit code 128: git fetch',
  command: 'git fetch',
  exitCode: 128,
  signal: undefined,
  signalDescription: undefined,
  stdout: '',
  stderr: "fatal: Authentication failed for 'https://tasktrack..../bitbucket/scm/ccf/microfrontend-...-next.git/'",
  failed: true,
  timedOut: false,
  isCanceled: false,
  killed: false,
  pluginName: '@saithodev/semantic-release-backmerge'
}
AggregateError: 
    Error: Command failed with exit code 128: git fetch
    fatal: Authentication failed for 'https://tasktrack..../bitbucket/scm/ccf/microfrontend-...-next.git/'
        at makeError (/home/jenkins/workspace/microfrontend-...-next CD/node_modules/@saithodev/semantic-release-backmerge/node_modules/execa/lib/error.js:59:11)
        at handlePromise (/home/jenkins/workspace/microfrontend-...-next CD/node_modules/@saithodev/semantic-release-backmerge/node_modules/execa/index.js:114:26)
    at /home/jenkins/workspace/microfrontend-...-next CD/node_modules/semantic-release/lib/plugins/pipeline.js:54:11
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Object.pluginsConf.<computed> [as success] (/home/jenkins/workspace/microfrontend-...-next CD/node_modules/semantic-release/lib/plugins/index.js:80:11)
    at async run (/home/jenkins/workspace/microfrontend-...-next CD/node_modules/semantic-release/index.js:201:3)
    at async module.exports (/home/jenkins/workspace/microfrontend-...-next CD/node_modules/semantic-release/index.js:260:22)
    at async module.exports (/home/jenkins/workspace/microfrontend-...-next CD/node_modules/semantic-release/cli.js:55:5)

Is the plugin handling git authentication differently than semantic-release?
Thank you!

Lacront commented 3 years ago

For those with the same issue. Here is a workaround for your Jenkinsfile:

        withCredentials([usernamePassword(credentialsId: JENKINS_GIT_CREDENTIALS_ID, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
          sh("git config credential.username ${GIT_USERNAME}")
          sh("git config credential.helper '!f() { echo password=$GIT_PASSWORD; }; f'")
        }
        withCredentials([usernameColonPassword(credentialsId: JENKINS_GIT_CREDENTIALS_ID, variable: 'GIT_CREDENTIALS')]) {
          nodejs(JENKINS_NODE_JS_INSTALLATION_LABEL) {
            sh("npx semantic-release")
          }
        }
saitho commented 3 years ago

Hi @Lacront,

Is the plugin handling git authentication differently than semantic-release?

The Git command execution is just the same as semantic-release. However I noticed that you're using GIT_CREDENTIALS which I didn't know before. Comparing the Git executions, I noticed that semantic-release uses the repository URL when fetching which I don't. I'll look into that.

saitho commented 3 years ago

@Lacront

Can you try v1.4.1 to see if that fixed your issue?

saitho commented 3 years ago

All right, that does not work and breaks the whole execution. I'll need to setup a Jenkins to propertly debug that. Going by your workaround, the GIT_USERNAME and GIT_PASSWORD are not set, even though the Git URL should be created with GIT_CREDENTIALS by semantic-release.