Skip to content

Commit

Permalink
Enables Support for Self-Hosted GitHub Instances (GitHub Enterprise) (#…
Browse files Browse the repository at this point in the history
…622)

* Adds Changes

* Update main.test.ts

* Format

* Update README.md

* Formatting
  • Loading branch information
JamesIves authored Mar 4, 2021
1 parent c57c6dd commit 9119c52
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</p>

<p align="center">
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.
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.
</p>

<p align="center">
Expand Down
16 changes: 16 additions & 0 deletions __tests__/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('git', () => {
describe('init', () => {
it('should execute commands', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
token: '123',
Expand All @@ -64,6 +65,7 @@ describe('git', () => {
})

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

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

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

it('should correctly continue when it cannot remove origin', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
repositoryPath: 'JamesIves/github-pages-deploy-action',
token: '123',
Expand All @@ -148,6 +153,7 @@ describe('git', () => {
describe('deploy', () => {
it('should execute commands', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
branch: 'branch',
Expand All @@ -170,6 +176,7 @@ describe('git', () => {

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

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

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

it('should execute commands with single commit and dryRun toggled', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'other',
folderPath: 'other',
Expand Down Expand Up @@ -270,6 +280,7 @@ describe('git', () => {
})

Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
folderPath: 'assets',
Expand Down Expand Up @@ -300,6 +311,7 @@ describe('git', () => {
it('should execute commands with clean options', async () => {
process.env.GITHUB_SHA = ''
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'other',
folderPath: 'other',
Expand All @@ -324,6 +336,7 @@ describe('git', () => {

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

it('should gracefully handle target folder', async () => {
Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: '.',
branch: 'branch',
Expand All @@ -367,6 +381,7 @@ describe('git', () => {

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

Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
branch: 'branch',
Expand Down
3 changes: 3 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('main', () => {
folder: 'assets',
branch: 'branch',
token: '123',
hostname: 'github.com',
pusher: {
name: 'asd',
email: 'as@cat'
Expand All @@ -56,6 +57,7 @@ describe('main', () => {

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

it('should throw if an error is encountered', async () => {
Object.assign(action, {
hostname: 'github.com',
folder: 'assets',
branch: 'branch',
token: null,
Expand Down
4 changes: 4 additions & 0 deletions __tests__/ssh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('configureSSH', () => {

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

Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
branch: 'branch',
Expand All @@ -91,6 +93,7 @@ describe('configureSSH', () => {
})

Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
branch: 'branch',
Expand All @@ -115,6 +118,7 @@ describe('configureSSH', () => {
})

Object.assign(action, {
hostname: 'github.com',
silent: false,
folder: 'assets',
branch: 'branch',
Expand Down
29 changes: 27 additions & 2 deletions __tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
generateRepositoryPath,
generateFolderPath,
suppressSensitiveInformation,
checkParameters
checkParameters,
stripProtocolFromUrl
} from '../src/util'

describe('util', () => {
Expand Down Expand Up @@ -79,11 +80,13 @@ describe('util', () => {
branch: '123',
workspace: 'src/',
folder: 'build',
hostname: 'github.com',
token: null,
sshKey: 'real_token',
silent: false,
isTest: TestFlag.NONE
}

expect(generateRepositoryPath(action)).toEqual(
'git@github.com:JamesIves/github-pages-deploy-action'
)
Expand All @@ -95,13 +98,15 @@ describe('util', () => {
branch: '123',
workspace: 'src/',
folder: 'build',
hostname: 'enterprise.github.com',
token: '123',
sshKey: null,
silent: false,
isTest: TestFlag.NONE
}

expect(generateRepositoryPath(action)).toEqual(
'https://x-access-token:123@github.com/JamesIves/github-pages-deploy-action.git'
'https://x-access-token:123@enterprise.github.com/JamesIves/github-pages-deploy-action.git'
)
})

Expand Down Expand Up @@ -302,4 +307,24 @@ describe('util', () => {
}
})
})

describe('stripProtocolFromUrl', () => {
it('removes https', () => {
expect(stripProtocolFromUrl('https://github.com')).toBe('github.com')
})

it('removes http', () => {
expect(stripProtocolFromUrl('http://github.com')).toBe('github.com')
})

it('removes https|http and www.', () => {
expect(stripProtocolFromUrl('http://www.github.com')).toBe('github.com')
})

it('works with a url that is not github.com', () => {
expect(stripProtocolFromUrl('http://github.enterprise.jamesiv.es')).toBe(
'github.enterprise.jamesiv.es'
)
})
})
})
1 change: 1 addition & 0 deletions __tests__/worktree.error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('generateWorktree', () => {
try {
await generateWorktree(
{
hostname: 'github.com',
workspace: 'somewhere',
singleCommit: false,
branch: 'gh-pages',
Expand Down
4 changes: 4 additions & 0 deletions __tests__/worktree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('generateWorktree', () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: false,
branch: 'gh-pages',
Expand Down Expand Up @@ -111,6 +112,7 @@ describe('generateWorktree', () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: false,
branch: 'no-pages',
Expand Down Expand Up @@ -138,6 +140,7 @@ describe('generateWorktree', () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: true,
branch: 'gh-pages',
Expand Down Expand Up @@ -169,6 +172,7 @@ describe('generateWorktree', () => {
const workspace = clonedir as string
await generateWorktree(
{
hostname: 'github.com',
workspace,
singleCommit: true,
branch: 'no-pages',
Expand Down
13 changes: 11 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {getInput} from '@actions/core'
import * as github from '@actions/github'
import {isNullOrUndefined} from './util'
import {isNullOrUndefined, stripProtocolFromUrl} from './util'

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

Expand All @@ -25,6 +25,8 @@ export interface ActionInterface {
cleanExclude?: string[]
/** If you need to customize the commit message for an integration you can do so. */
commitMessage?: string
/** The hostname of which the GitHub Workflow is being run on, ie: github.com */
hostname?: string
/** The git config email. */
email?: string
/** The folder to deploy. */
Expand Down Expand Up @@ -89,14 +91,21 @@ export const action: ActionInterface = {
cleanExclude: (getInput('clean-exclude') || '')
.split('\n')
.filter(l => l !== ''),
hostname: process.env.GITHUB_SERVER_URL
? stripProtocolFromUrl(process.env.GITHUB_SERVER_URL)
: 'github.com',
isTest: TestFlag.NONE,
email: !isNullOrUndefined(getInput('git-config-email'))
? getInput('git-config-email')
: pusher && pusher.email
? pusher.email
: `${
process.env.GITHUB_ACTOR || 'github-pages-deploy-action'
}@users.noreply.github.com`,
}@users.noreply.${
process.env.GITHUB_SERVER_URL
? stripProtocolFromUrl(process.env.GITHUB_SERVER_URL)
: 'github.com'
}`,
name: !isNullOrUndefined(getInput('git-config-name'))
? getInput('git-config-name')
: pusher && pusher.name
Expand Down
2 changes: 1 addition & 1 deletion src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function init(action: ActionInterface): Promise<void | Error> {
Only runs in the GitHub Actions CI environment if a user is not using an SSH key.
*/
await execute(
`git config --local --unset-all http.https://github.com/.extraheader`,
`git config --local --unset-all http.https://${action.hostname}/.extraheader`,
action.workspace,
action.silent
)
Expand Down
6 changes: 2 additions & 4 deletions src/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ export async function configureSSH(action: ActionInterface): Promise<void> {
const sshKnownHostsDirectory = `${sshDirectory}/known_hosts`

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

info(`Configuring SSH client… 🔑`)

Expand Down
8 changes: 6 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const generateTokenType = (action: ActionInterface): string =>
/* Generates a the repository path used to make the commits. */
export const generateRepositoryPath = (action: ActionInterface): string =>
action.sshKey
? `git@github.com:${action.repositoryName}`
: `https://${`x-access-token:${action.token}`}@github.com/${
? `git@${action.hostname}:${action.repositoryName}`
: `https://${`x-access-token:${action.token}`}@${action.hostname}/${
action.repositoryName
}.git`

Expand Down Expand Up @@ -89,3 +89,7 @@ export const suppressSensitiveInformation = (

return value
}

/** Strips the protocol from a provided URL. */
export const stripProtocolFromUrl = (url: string): string =>
url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, '').split('/')[0]

0 comments on commit 9119c52

Please sign in to comment.