Skip to content

Commit 9119c52

Browse files
authored
Enables Support for Self-Hosted GitHub Instances (GitHub Enterprise) (#622)
* Adds Changes * Update main.test.ts * Format * Update README.md * Formatting
1 parent c57c6dd commit 9119c52

11 files changed

+76
-12
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</p>
3232

3333
<p align="center">
34-
This <a href="https://github.com/features/actions">GitHub Action</a> will automatically deploy your project to <a href="https://pages.github.com/">GitHub Pages</a>. It can be configured to push your production-ready code into any branch you'd like, including <b>gh-pages</b> and <b>docs</b>. It can also handle cross repository deployments too.
34+
This <a href="https://github.com/features/actions">GitHub Action</a> will automatically deploy your project to <a href="https://pages.github.com/">GitHub Pages</a>. It can be configured to push your production-ready code into any branch you'd like, including <b>gh-pages</b> and <b>docs</b>. It can also handle cross repository deployments and works with <a href="https://github.com/enterprise">GitHub Enterprise</a> too.
3535
</p>
3636

3737
<p align="center">

__tests__/git.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe('git', () => {
4242
describe('init', () => {
4343
it('should execute commands', async () => {
4444
Object.assign(action, {
45+
hostname: 'github.com',
4546
silent: false,
4647
repositoryPath: 'JamesIves/github-pages-deploy-action',
4748
token: '123',
@@ -64,6 +65,7 @@ describe('git', () => {
6465
})
6566

6667
Object.assign(action, {
68+
hostname: 'github.com',
6769
silent: false,
6870
repositoryPath: 'JamesIves/github-pages-deploy-action',
6971
token: '123',
@@ -87,6 +89,7 @@ describe('git', () => {
8789

8890
it('should correctly continue when it cannot unset a git config value', async () => {
8991
Object.assign(action, {
92+
hostname: 'github.com',
9093
silent: false,
9194
repositoryPath: 'JamesIves/github-pages-deploy-action',
9295
token: '123',
@@ -108,6 +111,7 @@ describe('git', () => {
108111
process.env.CI = 'true'
109112

110113
Object.assign(action, {
114+
hostname: 'github.com',
111115
silent: false,
112116
repositoryPath: 'JamesIves/github-pages-deploy-action',
113117
sshKey: true,
@@ -128,6 +132,7 @@ describe('git', () => {
128132

129133
it('should correctly continue when it cannot remove origin', async () => {
130134
Object.assign(action, {
135+
hostname: 'github.com',
131136
silent: false,
132137
repositoryPath: 'JamesIves/github-pages-deploy-action',
133138
token: '123',
@@ -148,6 +153,7 @@ describe('git', () => {
148153
describe('deploy', () => {
149154
it('should execute commands', async () => {
150155
Object.assign(action, {
156+
hostname: 'github.com',
151157
silent: false,
152158
folder: 'assets',
153159
branch: 'branch',
@@ -170,6 +176,7 @@ describe('git', () => {
170176

171177
it('should not push when asked to dryRun', async () => {
172178
Object.assign(action, {
179+
hostname: 'github.com',
173180
silent: false,
174181
dryRun: true,
175182
folder: 'assets',
@@ -192,6 +199,7 @@ describe('git', () => {
192199

193200
it('should execute commands with single commit toggled', async () => {
194201
Object.assign(action, {
202+
hostname: 'github.com',
195203
silent: false,
196204
folder: 'other',
197205
folderPath: 'other',
@@ -215,6 +223,7 @@ describe('git', () => {
215223

216224
it('should execute commands with single commit toggled and existing branch', async () => {
217225
Object.assign(action, {
226+
hostname: 'github.com',
218227
silent: false,
219228
folder: 'other',
220229
folderPath: 'other',
@@ -238,6 +247,7 @@ describe('git', () => {
238247

239248
it('should execute commands with single commit and dryRun toggled', async () => {
240249
Object.assign(action, {
250+
hostname: 'github.com',
241251
silent: false,
242252
folder: 'other',
243253
folderPath: 'other',
@@ -270,6 +280,7 @@ describe('git', () => {
270280
})
271281

272282
Object.assign(action, {
283+
hostname: 'github.com',
273284
silent: false,
274285
folder: 'assets',
275286
folderPath: 'assets',
@@ -300,6 +311,7 @@ describe('git', () => {
300311
it('should execute commands with clean options', async () => {
301312
process.env.GITHUB_SHA = ''
302313
Object.assign(action, {
314+
hostname: 'github.com',
303315
silent: false,
304316
folder: 'other',
305317
folderPath: 'other',
@@ -324,6 +336,7 @@ describe('git', () => {
324336

325337
it('should execute commands with clean options stored as an array', async () => {
326338
Object.assign(action, {
339+
hostname: 'github.com',
327340
silent: false,
328341
folder: 'assets',
329342
folderPath: 'assets',
@@ -347,6 +360,7 @@ describe('git', () => {
347360

348361
it('should gracefully handle target folder', async () => {
349362
Object.assign(action, {
363+
hostname: 'github.com',
350364
silent: false,
351365
folder: '.',
352366
branch: 'branch',
@@ -367,6 +381,7 @@ describe('git', () => {
367381

368382
it('should stop early if there is nothing to commit', async () => {
369383
Object.assign(action, {
384+
hostname: 'github.com',
370385
silent: false,
371386
folder: 'assets',
372387
branch: 'branch',
@@ -390,6 +405,7 @@ describe('git', () => {
390405
})
391406

392407
Object.assign(action, {
408+
hostname: 'github.com',
393409
silent: false,
394410
folder: 'assets',
395411
branch: 'branch',

__tests__/main.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('main', () => {
4141
folder: 'assets',
4242
branch: 'branch',
4343
token: '123',
44+
hostname: 'github.com',
4445
pusher: {
4546
name: 'asd',
4647
email: 'as@cat'
@@ -56,6 +57,7 @@ describe('main', () => {
5657

5758
it('should run through the commands and succeed', async () => {
5859
Object.assign(action, {
60+
hostname: 'github.com',
5961
repositoryPath: 'JamesIves/github-pages-deploy-action',
6062
folder: 'assets',
6163
branch: 'branch',
@@ -75,6 +77,7 @@ describe('main', () => {
7577

7678
it('should throw if an error is encountered', async () => {
7779
Object.assign(action, {
80+
hostname: 'github.com',
7881
folder: 'assets',
7982
branch: 'branch',
8083
token: null,

__tests__/ssh.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ describe('configureSSH', () => {
4343

4444
it('should skip client configuration if sshKey is set to true', async () => {
4545
Object.assign(action, {
46+
hostname: 'github.com',
4647
silent: false,
4748
folder: 'assets',
4849
branch: 'branch',
@@ -67,6 +68,7 @@ describe('configureSSH', () => {
6768
})
6869

6970
Object.assign(action, {
71+
hostname: 'github.com',
7072
silent: false,
7173
folder: 'assets',
7274
branch: 'branch',
@@ -91,6 +93,7 @@ describe('configureSSH', () => {
9193
})
9294

9395
Object.assign(action, {
96+
hostname: 'github.com',
9497
silent: false,
9598
folder: 'assets',
9699
branch: 'branch',
@@ -115,6 +118,7 @@ describe('configureSSH', () => {
115118
})
116119

117120
Object.assign(action, {
121+
hostname: 'github.com',
118122
silent: false,
119123
folder: 'assets',
120124
branch: 'branch',

__tests__/util.test.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
generateRepositoryPath,
66
generateFolderPath,
77
suppressSensitiveInformation,
8-
checkParameters
8+
checkParameters,
9+
stripProtocolFromUrl
910
} from '../src/util'
1011

1112
describe('util', () => {
@@ -79,11 +80,13 @@ describe('util', () => {
7980
branch: '123',
8081
workspace: 'src/',
8182
folder: 'build',
83+
hostname: 'github.com',
8284
token: null,
8385
sshKey: 'real_token',
8486
silent: false,
8587
isTest: TestFlag.NONE
8688
}
89+
8790
expect(generateRepositoryPath(action)).toEqual(
8891
'git@github.com:JamesIves/github-pages-deploy-action'
8992
)
@@ -95,13 +98,15 @@ describe('util', () => {
9598
branch: '123',
9699
workspace: 'src/',
97100
folder: 'build',
101+
hostname: 'enterprise.github.com',
98102
token: '123',
99103
sshKey: null,
100104
silent: false,
101105
isTest: TestFlag.NONE
102106
}
107+
103108
expect(generateRepositoryPath(action)).toEqual(
104-
'https://x-access-token:123@github.com/JamesIves/github-pages-deploy-action.git'
109+
'https://x-access-token:123@enterprise.github.com/JamesIves/github-pages-deploy-action.git'
105110
)
106111
})
107112

@@ -302,4 +307,24 @@ describe('util', () => {
302307
}
303308
})
304309
})
310+
311+
describe('stripProtocolFromUrl', () => {
312+
it('removes https', () => {
313+
expect(stripProtocolFromUrl('https://github.com')).toBe('github.com')
314+
})
315+
316+
it('removes http', () => {
317+
expect(stripProtocolFromUrl('http://github.com')).toBe('github.com')
318+
})
319+
320+
it('removes https|http and www.', () => {
321+
expect(stripProtocolFromUrl('http://www.github.com')).toBe('github.com')
322+
})
323+
324+
it('works with a url that is not github.com', () => {
325+
expect(stripProtocolFromUrl('http://github.enterprise.jamesiv.es')).toBe(
326+
'github.enterprise.jamesiv.es'
327+
)
328+
})
329+
})
305330
})

__tests__/worktree.error.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('generateWorktree', () => {
1616
try {
1717
await generateWorktree(
1818
{
19+
hostname: 'github.com',
1920
workspace: 'somewhere',
2021
singleCommit: false,
2122
branch: 'gh-pages',

__tests__/worktree.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ describe('generateWorktree', () => {
8181
const workspace = clonedir as string
8282
await generateWorktree(
8383
{
84+
hostname: 'github.com',
8485
workspace,
8586
singleCommit: false,
8687
branch: 'gh-pages',
@@ -111,6 +112,7 @@ describe('generateWorktree', () => {
111112
const workspace = clonedir as string
112113
await generateWorktree(
113114
{
115+
hostname: 'github.com',
114116
workspace,
115117
singleCommit: false,
116118
branch: 'no-pages',
@@ -138,6 +140,7 @@ describe('generateWorktree', () => {
138140
const workspace = clonedir as string
139141
await generateWorktree(
140142
{
143+
hostname: 'github.com',
141144
workspace,
142145
singleCommit: true,
143146
branch: 'gh-pages',
@@ -169,6 +172,7 @@ describe('generateWorktree', () => {
169172
const workspace = clonedir as string
170173
await generateWorktree(
171174
{
175+
hostname: 'github.com',
172176
workspace,
173177
singleCommit: true,
174178
branch: 'no-pages',

src/constants.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {getInput} from '@actions/core'
22
import * as github from '@actions/github'
3-
import {isNullOrUndefined} from './util'
3+
import {isNullOrUndefined, stripProtocolFromUrl} from './util'
44

55
const {pusher, repository} = github.context.payload
66

@@ -25,6 +25,8 @@ export interface ActionInterface {
2525
cleanExclude?: string[]
2626
/** If you need to customize the commit message for an integration you can do so. */
2727
commitMessage?: string
28+
/** The hostname of which the GitHub Workflow is being run on, ie: github.com */
29+
hostname?: string
2830
/** The git config email. */
2931
email?: string
3032
/** The folder to deploy. */
@@ -89,14 +91,21 @@ export const action: ActionInterface = {
8991
cleanExclude: (getInput('clean-exclude') || '')
9092
.split('\n')
9193
.filter(l => l !== ''),
94+
hostname: process.env.GITHUB_SERVER_URL
95+
? stripProtocolFromUrl(process.env.GITHUB_SERVER_URL)
96+
: 'github.com',
9297
isTest: TestFlag.NONE,
9398
email: !isNullOrUndefined(getInput('git-config-email'))
9499
? getInput('git-config-email')
95100
: pusher && pusher.email
96101
? pusher.email
97102
: `${
98103
process.env.GITHUB_ACTOR || 'github-pages-deploy-action'
99-
}@users.noreply.github.com`,
104+
}@users.noreply.${
105+
process.env.GITHUB_SERVER_URL
106+
? stripProtocolFromUrl(process.env.GITHUB_SERVER_URL)
107+
: 'github.com'
108+
}`,
100109
name: !isNullOrUndefined(getInput('git-config-name'))
101110
? getInput('git-config-name')
102111
: pusher && pusher.name

src/git.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function init(action: ActionInterface): Promise<void | Error> {
2929
Only runs in the GitHub Actions CI environment if a user is not using an SSH key.
3030
*/
3131
await execute(
32-
`git config --local --unset-all http.https://github.com/.extraheader`,
32+
`git config --local --unset-all http.https://${action.hostname}/.extraheader`,
3333
action.workspace,
3434
action.silent
3535
)

src/ssh.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ export async function configureSSH(action: ActionInterface): Promise<void> {
1212
const sshKnownHostsDirectory = `${sshDirectory}/known_hosts`
1313

1414
// SSH fingerprints provided by GitHub: https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/githubs-ssh-key-fingerprints
15-
const sshGitHubKnownHostRsa =
16-
'\ngithub.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW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3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==\n`
16+
const sshGitHubKnownHostDss = `\n${action.hostname} ssh-dss AAAAB3NzaC1kc3MAAACBANGFW2P9xlGU3zWrymJgI/lKo//ZW2WfVtmbsUZJ5uyKArtlQOT2+WRhcg4979aFxgKdcsqAYW3/LS1T2km3jYW/vr4Uzn+dXWODVk5VlUiZ1HFOHf6s6ITcZvjvdbp6ZbpM+DuJT7Bw+h5Fx8Qt8I16oCZYmAPJRtu46o9C2zk1AAAAFQC4gdFGcSbp5Gr0Wd5Ay/jtcldMewAAAIATTgn4sY4Nem/FQE+XJlyUQptPWMem5fwOcWtSXiTKaaN0lkk2p2snz+EJvAGXGq9dTSWHyLJSM2W6ZdQDqWJ1k+cL8CARAqL+UMwF84CR0m3hj+wtVGD/J4G5kW2DBAf4/bqzP4469lT+dF2FRQ2L9JKXrCWcnhMtJUvua8dvnwAAAIB6C4nQfAA7x8oLta6tT+oCk2WQcydNsyugE8vLrHlogoWEicla6cWPk7oXSspbzUcfkjN3Qa6e74PhRkc7JdSdAlFzU3m7LMkXo1MHgkqNX8glxWNVqBSc0YRdbFdTkL0C6gtpklilhvuHQCdbgB3LBAikcRkDp+FCVkUgPC/7Rw==\n`
1917

2018
info(`Configuring SSH client… 🔑`)
2119

src/util.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export const generateTokenType = (action: ActionInterface): string =>
1818
/* Generates a the repository path used to make the commits. */
1919
export const generateRepositoryPath = (action: ActionInterface): string =>
2020
action.sshKey
21-
? `git@github.com:${action.repositoryName}`
22-
: `https://${`x-access-token:${action.token}`}@github.com/${
21+
? `git@${action.hostname}:${action.repositoryName}`
22+
: `https://${`x-access-token:${action.token}`}@${action.hostname}/${
2323
action.repositoryName
2424
}.git`
2525

@@ -89,3 +89,7 @@ export const suppressSensitiveInformation = (
8989

9090
return value
9191
}
92+
93+
/** Strips the protocol from a provided URL. */
94+
export const stripProtocolFromUrl = (url: string): string =>
95+
url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, '').split('/')[0]

0 commit comments

Comments
 (0)