Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: Add GHES support 🏢 (#580)
Changes for #579

Co-authored-by: dom <dominik.meyer01@sap.com>
  • Loading branch information
2 people authored and GitHub committed Mar 9, 2021
1 parent a0db9b6 commit 3623583
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ coverage
.eslintcache
.env
node_modules
.husky/_
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -57,7 +57,11 @@ All Actions runners: Linux (Ubuntu), macOS, and Windows are supported.

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

### GitHub Enterprise Server Support

✅️ GitHub Enterprise Server is supported above `2.22.6`.

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.

## Table of Contents

Expand Down
37 changes: 37 additions & 0 deletions __tests__/set-tokens.ghes.test.ts
@@ -0,0 +1,37 @@
import {setPersonalToken, setGithubToken} from '../src/set-tokens';

const OLD_ENV = process.env;

beforeEach(() => {
jest.resetModules();
process.env = {...OLD_ENV};
});

afterAll(() => {
process.env = OLD_ENV; // Restore old environment
});

describe('setGithubToken()', () => {
test('return remote url with GITHUB_TOKEN gh-pages', () => {
process.env.GITHUB_SERVER_URL = 'https://github.enterprise.server';
const expected = 'https://x-access-token:GITHUB_TOKEN@github.enterprise.server/owner/repo.git';
const test = setGithubToken(
'GITHUB_TOKEN',
'owner/repo',
'gh-pages',
'',
'refs/heads/master',
'push'
);
expect(test).toMatch(expected);
});
});

describe('setPersonalToken()', () => {
test('return remote url with personal access token', () => {
process.env.GITHUB_SERVER_URL = 'https://github.enterprise.server';
const expected = 'https://x-access-token:pat@github.enterprise.server/owner/repo.git';
const test = setPersonalToken('pat', 'owner/repo');
expect(test).toMatch(expected);
});
});
1 change: 1 addition & 0 deletions lib/exec-child.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/git-utils.ts
Expand Up @@ -3,6 +3,7 @@ import * as exec from '@actions/exec';
import * as glob from '@actions/glob';
import path from 'path';
import fs from 'fs';
import {URL} from 'url';
import {Inputs, CmdResult} from './interfaces';
import {createDir} from './utils';
import {cp, rm} from 'shelljs';
Expand All @@ -13,6 +14,10 @@ export async function createBranchForce(branch: string): Promise<void> {
return;
}

export function getServerUrl(): URL {
return new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
}

export async function deleteExcludedAssets(destDir: string, excludeAssets: string): Promise<void> {
if (excludeAssets === '') return;
core.info(`[INFO] delete excluded assets`);
Expand Down
19 changes: 11 additions & 8 deletions src/set-tokens.ts
Expand Up @@ -10,6 +10,7 @@ const cpSpawnSync = require('child_process').spawnSync;
const cpexec = require('child_process').execFileSync;
import {Inputs} from './interfaces';
import {getHomeDir} from './utils';
import {getServerUrl} from './git-utils';

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

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

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

return `git@github.com:${publishRepo}.git`;
return `git@${getServerUrl().host}:${publishRepo}.git`;
}

export function setGithubToken(
Expand Down Expand Up @@ -94,12 +97,12 @@ This operation is prohibited to protect your contents
}
}

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

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

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

0 comments on commit 3623583

Please sign in to comment.