Skip to content

Commit 3623583

Browse files
mambaxdom
and
dom
authored
feat: Add GHES support 🏢 (#580)
Changes for #579 Co-authored-by: dom <[email protected]>
1 parent a0db9b6 commit 3623583

File tree

8 files changed

+61
-8
lines changed

8 files changed

+61
-8
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ coverage
44
.eslintcache
55
.env
66
node_modules
7+
.husky/_

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ All Actions runners: Linux (Ubuntu), macOS, and Windows are supported.
5757

5858
2. WIP, See [Issue #87](https://github.com/peaceiris/actions-gh-pages/issues/87)
5959

60+
### GitHub Enterprise Server Support
6061

62+
✅️ GitHub Enterprise Server is supported above `2.22.6`.
63+
64+
Note that the `GITHUB_TOKEN` that is created by the runner might not inherently have push/publish privileges on GHES. You might need to create/request a technical user with write permissions to your target repository.
6165

6266
## Table of Contents
6367

__tests__/set-tokens.ghes.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {setPersonalToken, setGithubToken} from '../src/set-tokens';
2+
3+
const OLD_ENV = process.env;
4+
5+
beforeEach(() => {
6+
jest.resetModules();
7+
process.env = {...OLD_ENV};
8+
});
9+
10+
afterAll(() => {
11+
process.env = OLD_ENV; // Restore old environment
12+
});
13+
14+
describe('setGithubToken()', () => {
15+
test('return remote url with GITHUB_TOKEN gh-pages', () => {
16+
process.env.GITHUB_SERVER_URL = 'https://github.enterprise.server';
17+
const expected = 'https://x-access-token:[email protected]/owner/repo.git';
18+
const test = setGithubToken(
19+
'GITHUB_TOKEN',
20+
'owner/repo',
21+
'gh-pages',
22+
'',
23+
'refs/heads/master',
24+
'push'
25+
);
26+
expect(test).toMatch(expected);
27+
});
28+
});
29+
30+
describe('setPersonalToken()', () => {
31+
test('return remote url with personal access token', () => {
32+
process.env.GITHUB_SERVER_URL = 'https://github.enterprise.server';
33+
const expected = 'https://x-access-token:[email protected]/owner/repo.git';
34+
const test = setPersonalToken('pat', 'owner/repo');
35+
expect(test).toMatch(expected);
36+
});
37+
});

lib/exec-child.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/git-utils.ts

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as exec from '@actions/exec';
33
import * as glob from '@actions/glob';
44
import path from 'path';
55
import fs from 'fs';
6+
import {URL} from 'url';
67
import {Inputs, CmdResult} from './interfaces';
78
import {createDir} from './utils';
89
import {cp, rm} from 'shelljs';
@@ -13,6 +14,10 @@ export async function createBranchForce(branch: string): Promise<void> {
1314
return;
1415
}
1516

17+
export function getServerUrl(): URL {
18+
return new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
19+
}
20+
1621
export async function deleteExcludedAssets(destDir: string, excludeAssets: string): Promise<void> {
1722
if (excludeAssets === '') return;
1823
core.info(`[INFO] delete excluded assets`);

src/set-tokens.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const cpSpawnSync = require('child_process').spawnSync;
1010
const cpexec = require('child_process').execFileSync;
1111
import {Inputs} from './interfaces';
1212
import {getHomeDir} from './utils';
13+
import {getServerUrl} from './git-utils';
1314

1415
export async function setSSHKey(inps: Inputs, publishRepo: string): Promise<string> {
1516
core.info('[INFO] setup SSH deploy key');
@@ -20,10 +21,12 @@ export async function setSSHKey(inps: Inputs, publishRepo: string): Promise<stri
2021
await exec.exec('chmod', ['700', sshDir]);
2122

2223
const knownHosts = path.join(sshDir, 'known_hosts');
23-
// ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts on Ubuntu
24+
// ssh-keyscan -t rsa github.com or serverUrl >> ~/.ssh/known_hosts on Ubuntu
2425
const cmdSSHkeyscanOutput = `\
25-
# github.com:22 SSH-2.0-babeld-1f0633a6
26-
github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
26+
# ${getServerUrl().host}.com:22 SSH-2.0-babeld-1f0633a6
27+
${
28+
getServerUrl().host
29+
} ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
2730
`;
2831
fs.writeFileSync(knownHosts, cmdSSHkeyscanOutput + '\n');
2932
core.info(`[INFO] wrote ${knownHosts}`);
@@ -36,8 +39,8 @@ github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXY
3639

3740
const sshConfigPath = path.join(sshDir, 'config');
3841
const sshConfigContent = `\
39-
Host github
40-
HostName github.com
42+
Host ${getServerUrl().host}
43+
HostName ${getServerUrl().host}
4144
IdentityFile ~/.ssh/github
4245
User git
4346
`;
@@ -60,7 +63,7 @@ Watch https://github.com/peaceiris/actions-gh-pages/issues/87
6063
core.exportVariable('SSH_AUTH_SOCK', '/tmp/ssh-auth.sock');
6164
await exec.exec('ssh-add', [idRSA]);
6265

63-
return `git@github.com:${publishRepo}.git`;
66+
return `git@${getServerUrl().host}:${publishRepo}.git`;
6467
}
6568

6669
export function setGithubToken(
@@ -94,12 +97,12 @@ This operation is prohibited to protect your contents
9497
}
9598
}
9699

97-
return `https://x-access-token:${githubToken}@github.com/${publishRepo}.git`;
100+
return `https://x-access-token:${githubToken}@${getServerUrl().host}/${publishRepo}.git`;
98101
}
99102

100103
export function setPersonalToken(personalToken: string, publishRepo: string): string {
101104
core.info('[INFO] setup personal access token');
102-
return `https://x-access-token:${personalToken}@github.com/${publishRepo}.git`;
105+
return `https://x-access-token:${personalToken}@${getServerUrl().host}/${publishRepo}.git`;
103106
}
104107

105108
export function getPublishRepo(externalRepository: string, owner: string, repo: string): string {

0 commit comments

Comments
 (0)