feat: add support for Bitbucket token in environment variables
This commit is contained in:
parent
22c5bbe06b
commit
c93775cc38
@ -10,13 +10,14 @@ See [CI configuration recipes](../recipes/README.md#ci-configurations) for more
|
|||||||
|
|
||||||
**semantic-release** requires push access to the project Git repository in order to create [Git tags](https://git-scm.com/book/en/v2/Git-Basics-Tagging). The Git authentication can be set with one of the following environment variables:
|
**semantic-release** requires push access to the project Git repository in order to create [Git tags](https://git-scm.com/book/en/v2/Git-Basics-Tagging). The Git authentication can be set with one of the following environment variables:
|
||||||
|
|
||||||
| Variable | Description |
|
| Variable | Description |
|
||||||
|------------------------------|-------------------------------------------------------------------------------------------------------------------------------|
|
|---------------------------------|-------------------------------------------------------------------------------------------------------------------------------|
|
||||||
| `GH_TOKEN` or `GITHUB_TOKEN` | A GitHub [personal access token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line). |
|
| `GH_TOKEN` or `GITHUB_TOKEN` | A GitHub [personal access token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line). |
|
||||||
| `GL_TOKEN` or `GITLAB_TOKEN` | A GitLab [personal access token](https://docs.gitlab.com/ce/user/profile/personal_access_tokens.html). |
|
| `GL_TOKEN` or `GITLAB_TOKEN` | A GitLab [personal access token](https://docs.gitlab.com/ce/user/profile/personal_access_tokens.html). |
|
||||||
| `GIT_CREDENTIALS` | [URL encoded basic HTTP Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication#URL_encoding) credentials). |
|
| `BB_TOKEN` or `BITBUCKET_TOKEN` | A Bitbucket [personal access token](https://confluence.atlassian.com/bitbucketserver/personal-access-tokens-939515499.html). |
|
||||||
|
| `GIT_CREDENTIALS` | [URL encoded basic HTTP Authentication](https://en.wikipedia.org/wiki/Basic_access_authentication#URL_encoding) credentials). |
|
||||||
|
|
||||||
`GIT_CREDENTIALS` can be the Git username and password in the format `<username>:<password>` or a token for certain Git providers like [Bitbucket](https://confluence.atlassian.com/bitbucketserver/personal-access-tokens-939515499.html).
|
`GIT_CREDENTIALS` must be the Git username and password in the format `<username>:<password>`.
|
||||||
|
|
||||||
Alternatively the Git authentication can be set up via [SSH keys](../recipes/git-auth-ssh-keys.md).
|
Alternatively the Git authentication can be set up via [SSH keys](../recipes/git-auth-ssh-keys.md).
|
||||||
|
|
||||||
|
@ -4,7 +4,15 @@ const gitUrlParse = require('git-url-parse');
|
|||||||
const hostedGitInfo = require('hosted-git-info');
|
const hostedGitInfo = require('hosted-git-info');
|
||||||
const {verifyAuth} = require('./git');
|
const {verifyAuth} = require('./git');
|
||||||
|
|
||||||
const GIT_TOKENS = ['GIT_CREDENTIALS', 'GH_TOKEN', 'GITHUB_TOKEN', 'GL_TOKEN', 'GITLAB_TOKEN'];
|
const GIT_TOKENS = {
|
||||||
|
GIT_CREDENTIALS: undefined,
|
||||||
|
GH_TOKEN: undefined,
|
||||||
|
GITHUB_TOKEN: undefined,
|
||||||
|
GL_TOKEN: 'gitlab-ci-token:',
|
||||||
|
GITLAB_TOKEN: 'gitlab-ci-token:',
|
||||||
|
BB_TOKEN: 'x-token-auth:',
|
||||||
|
BITBUCKET_TOKEN: 'x-token-auth:',
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine the the git repository URL to use to push, either:
|
* Determine the the git repository URL to use to push, either:
|
||||||
@ -36,10 +44,8 @@ module.exports = async ({repositoryUrl, branch}) => {
|
|||||||
|
|
||||||
// Test if push is allowed without transforming the URL (e.g. is ssh keys are set up)
|
// Test if push is allowed without transforming the URL (e.g. is ssh keys are set up)
|
||||||
if (!await verifyAuth(repositoryUrl, branch)) {
|
if (!await verifyAuth(repositoryUrl, branch)) {
|
||||||
const envVar = GIT_TOKENS.find(envVar => !isUndefined(process.env[envVar]));
|
const envVar = Object.keys(GIT_TOKENS).find(envVar => !isUndefined(process.env[envVar]));
|
||||||
const gitCredentials = ['GL_TOKEN', 'GITLAB_TOKEN'].includes(envVar)
|
const gitCredentials = `${GIT_TOKENS[envVar] || ''}${process.env[envVar] || ''}`;
|
||||||
? `gitlab-ci-token:${process.env[envVar]}`
|
|
||||||
: process.env[envVar];
|
|
||||||
const {protocols, ...parsed} = gitUrlParse(repositoryUrl);
|
const {protocols, ...parsed} = gitUrlParse(repositoryUrl);
|
||||||
const protocol = protocols.includes('https') ? 'https' : protocols.includes('http') ? 'http' : 'https';
|
const protocol = protocols.includes('https') ? 'https' : protocols.includes('http') ? 'http' : 'https';
|
||||||
|
|
||||||
|
@ -13,6 +13,8 @@ test.beforeEach(() => {
|
|||||||
delete process.env.GITHUB_TOKEN;
|
delete process.env.GITHUB_TOKEN;
|
||||||
delete process.env.GL_TOKEN;
|
delete process.env.GL_TOKEN;
|
||||||
delete process.env.GITLAB_TOKEN;
|
delete process.env.GITLAB_TOKEN;
|
||||||
|
delete process.env.BB_TOKEN;
|
||||||
|
delete process.env.BITBUCKET_TOKEN;
|
||||||
process.env.GIT_ASKPASS = 'echo';
|
process.env.GIT_ASKPASS = 'echo';
|
||||||
process.env.GIT_TERMINAL_PROMPT = 0;
|
process.env.GIT_TERMINAL_PROMPT = 0;
|
||||||
});
|
});
|
||||||
@ -152,6 +154,22 @@ test.serial('Return the "https" formatted URL if "gitCredentials" is defined wit
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.serial('Return the "https" formatted URL if "gitCredentials" is defined with "BB_TOKEN"', async t => {
|
||||||
|
process.env.BB_TOKEN = 'token';
|
||||||
|
t.is(
|
||||||
|
await getAuthUrl({repositoryUrl: 'git@host.null:owner/repo.git'}),
|
||||||
|
'https://x-token-auth:token@host.null/owner/repo.git'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test.serial('Return the "https" formatted URL if "gitCredentials" is defined with "BITBUCKET_TOKEN"', async t => {
|
||||||
|
process.env.BITBUCKET_TOKEN = 'token';
|
||||||
|
t.is(
|
||||||
|
await getAuthUrl({repositoryUrl: 'git@host.null:owner/repo.git'}),
|
||||||
|
'https://x-token-auth:token@host.null/owner/repo.git'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test.serial('Handle "https" URL with group and subgroup, with "GIT_CREDENTIALS"', async t => {
|
test.serial('Handle "https" URL with group and subgroup, with "GIT_CREDENTIALS"', async t => {
|
||||||
process.env.GIT_CREDENTIALS = 'user:pass';
|
process.env.GIT_CREDENTIALS = 'user:pass';
|
||||||
t.is(
|
t.is(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user