Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow GIT_USER env var to be unset if SSH is used #5840

Merged
merged 17 commits into from Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/create-docusaurus/templates/facebook/README.md
Expand Up @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv

### Deployment

Using SSH:

```
$ USE_SSH=true yarn deploy
```

Not using SSH:

```
$ GIT_USER=<Your GitHub username> USE_SSH=true yarn deploy
$ GIT_USER=<Your GitHub username> yarn deploy
```

If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch.
Expand Down
10 changes: 9 additions & 1 deletion packages/create-docusaurus/templates/shared/README.md
Expand Up @@ -26,8 +26,16 @@ This command generates static content into the `build` directory and can be serv

### Deployment

Using SSH:

```
$ USE_SSH=true yarn deploy
```

Not using SSH:

```
$ GIT_USER=<Your GitHub username> USE_SSH=true yarn deploy
$ GIT_USER=<Your GitHub username> yarn deploy
```

If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch.
Expand Up @@ -5,50 +5,33 @@
* LICENSE file in the root directory of this source tree.
*/

import {buildUrl} from '../buildRemoteBranchUrl';
import {buildSshUrl, buildHttpsUrl} from '../buildRemoteBranchUrl';

describe('remoteeBranchUrl', () => {
test('should build a normal ssh url', async () => {
const url = buildUrl(
'github.com',
undefined,
undefined,
'facebook',
'docusaurus',
true,
);
const url = buildSshUrl('github.com', 'facebook', 'docusaurus');
expect(url).toEqual('git@github.com:facebook/docusaurus.git');
});
test('should build a ssh url with port', async () => {
const url = buildUrl(
'github.com',
'422',
undefined,
'facebook',
'docusaurus',
true,
);
const url = buildSshUrl('github.com', 'facebook', 'docusaurus', '422');
expect(url).toEqual('ssh://git@github.com:422/facebook/docusaurus.git');
});
test('should build a normal http url', async () => {
const url = buildUrl(
'github.com',
undefined,
const url = buildHttpsUrl(
'user:pass',
'github.com',
'facebook',
'docusaurus',
false,
);
expect(url).toEqual('https://user:pass@github.com/facebook/docusaurus.git');
});
test('should build a normal http url', async () => {
const url = buildUrl(
'github.com',
'5433',
const url = buildHttpsUrl(
'user:pass',
'github.com',
'facebook',
'docusaurus',
false,
'5433',
);
expect(url).toEqual(
'https://user:pass@github.com:5433/facebook/docusaurus.git',
Expand Down
31 changes: 6 additions & 25 deletions packages/docusaurus/src/commands/buildRemoteBranchUrl.ts
Expand Up @@ -5,44 +5,25 @@
* LICENSE file in the root directory of this source tree.
*/

export function buildUrl(
export function buildSshUrl(
githubHost: string,
githubPort: string | undefined,
gitCredentials: string | undefined,
organizationName: string,
projectName: string,
useSSH: boolean | undefined,
githubPort?: string,
): string {
return useSSH
? buildSshUrl(githubHost, organizationName, projectName, githubPort)
: buildHttpsUrl(
gitCredentials,
githubHost,
organizationName,
projectName,
githubPort,
);
}

function buildSshUrl(
githubHost: string,
organizationName: string,
projectName: string,
githubPort: string | undefined,
) {
if (githubPort) {
return `ssh://git@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`;
}
return `git@${githubHost}:${organizationName}/${projectName}.git`;
}

function buildHttpsUrl(
gitCredentials: string | undefined,
export function buildHttpsUrl(
gitCredentials: string,
githubHost: string,
organizationName: string,
projectName: string,
githubPort: string | undefined,
) {
githubPort?: string,
): string {
if (githubPort) {
return `https://${gitCredentials}@${githubHost}:${githubPort}/${organizationName}/${projectName}.git`;
}
Expand Down
60 changes: 38 additions & 22 deletions packages/docusaurus/src/commands/deploy.ts
Expand Up @@ -13,7 +13,7 @@ import build from './build';
import {BuildCLIOptions} from '@docusaurus/types';
import path from 'path';
import os from 'os';
import {buildUrl} from './buildRemoteBranchUrl';
import {buildSshUrl, buildHttpsUrl} from './buildRemoteBranchUrl';

// GIT_PASS env variable should not appear in logs
function obfuscateGitPass(str: string) {
Expand Down Expand Up @@ -63,9 +63,25 @@ This behavior can have SEO impacts and create relative link issues.
throw new Error('Git not installed or on the PATH!');
}

const currentRepoUrl = shell
.exec('git config --get remote.origin.url')
.stdout.trim();

const repoUrlUseSSH =
/^ssh:\/\//.test(currentRepoUrl) || // ssh://***: explicit protocol
/^([\w-]+@)?[\w.-]+:[\w./_-]+(\.git)?/.test(currentRepoUrl); // git@github.com:facebook/docusaurus.git

const envUseSSH =
process.env.USE_SSH !== undefined &&
process.env.USE_SSH.toLowerCase() === 'true';

const useSSH = envUseSSH || repoUrlUseSSH;

const gitUser = process.env.GIT_USER;
if (!gitUser) {
throw new Error('Please set the GIT_USER environment variable!');
if (!gitUser && !useSSH) {
throw new Error(
'Please set the GIT_USER environment variable, or use SSH instead!',
);
}

// The branch that contains the latest docs changes that will be deployed.
Expand Down Expand Up @@ -107,8 +123,7 @@ This behavior can have SEO impacts and create relative link issues.
// Organization deploys looks like:
// - Git repo: https://github.com/<organization>/<organization>.github.io
// - Site url: https://<organization>.github.io
const isGitHubPagesOrganizationDeploy =
projectName.indexOf('.github.io') !== -1;
const isGitHubPagesOrganizationDeploy = projectName.includes('.github.io');
if (
isGitHubPagesOrganizationDeploy &&
!process.env.DEPLOYMENT_BRANCH &&
Expand All @@ -127,30 +142,31 @@ You can also set the deploymentBranch property in docusaurus.config.js .`);
process.env.GITHUB_HOST || siteConfig.githubHost || 'github.com';
const githubPort = process.env.GITHUB_PORT || siteConfig.githubPort;

const gitPass: string | undefined = process.env.GIT_PASS;
let gitCredentials = `${gitUser}`;
if (gitPass) {
gitCredentials = `${gitCredentials}:${gitPass}`;
let remoteBranch: string;
if (useSSH) {
remoteBranch = buildSshUrl(
githubHost,
organizationName,
projectName,
githubPort,
);
} else {
const gitPass = process.env.GIT_PASS;
const gitCredentials = gitPass ? `${gitUser!}:${gitPass}` : gitUser!;
remoteBranch = buildHttpsUrl(
gitCredentials,
githubHost,
organizationName,
projectName,
githubPort,
);
}

const useSSH = process.env.USE_SSH;
const remoteBranch = buildUrl(
githubHost,
githubPort,
gitCredentials,
organizationName,
projectName,
useSSH !== undefined && useSSH.toLowerCase() === 'true',
);

console.log(
`${chalk.cyan('Remote branch:')} ${obfuscateGitPass(remoteBranch)}`,
);

// Check if this is a cross-repo publish.
const currentRepoUrl = shell
.exec('git config --get remote.origin.url')
.stdout.trim();
const crossRepoPublish = !currentRepoUrl.endsWith(
`${organizationName}/${projectName}.git`,
);
Expand Down
15 changes: 3 additions & 12 deletions website/docs/deployment.mdx
Expand Up @@ -138,19 +138,12 @@ By default, GitHub Pages runs published files through [Jekyll](https://jekyllrb.

### Environment settings {#environment-settings}

Specify the Git user as an environment variable.

| Name | Description |
| --- | --- |
| `GIT_USER` | The username for a GitHub account that **has push access to the deployment repo**. For your own repositories, this will usually be your GitHub username. |

Optional parameters, also set as environment variables:

| Name | Description |
| --- | --- |
| `USE_SSH` | Set to `true` to use SSH instead of the default HTTPS for the connection to the GitHub repo. |
| `USE_SSH` | Set to `true` to use SSH instead of the default HTTPS for the connection to the GitHub repo. If the source repo URL is an SSH URL (e.g. `git@github.com:facebook/docusaurus.git`), `USE_SSH` is inferred to be `true`. |
| `GIT_USER` | The username for a GitHub account that **has push access to the deployment repo**. For your own repositories, this will usually be your GitHub username. Required if not using SSH, and ignored otherwise. |
| `GIT_PASS` | Personal access token of the git user (specified by `GIT_USER`), to facilitate non-interactive deployment (e.g. continuous deployment) |
| `CURRENT_BRANCH` | The source branch. Usually, the branch will be `main` or `master`, but it could be any branch except for `gh-pages`. If nothing is set for this variable, then the current branch from which `docusaurus deploy` is invoked will be used. |
| `GIT_PASS` | Personal access token of the `git` user (specified by `GIT_USER`), to facilitate non-interactive deployment (e.g. continuous deployment) |

GitHub enterprise installations should work in the same manner as github.com; you only need to set the organization's GitHub Enterprise host as an environment variable:

Expand Down Expand Up @@ -362,7 +355,6 @@ jobs:
- name: Deploy to GitHub Pages
env:
USE_SSH: true
GIT_USER: git
run: |
git config --global user.email "actions@github.com"
git config --global user.name "gh-actions"
Expand Down Expand Up @@ -492,7 +484,6 @@ trigger:
- yarn deploy
environment:
USE_SSH: true
GIT_USER: $DRONE_COMMIT_AUTHOR
GITHUB_PRIVATE_KEY:
from_secret: git_deploy_private_key
```
Expand Down