mirror of
https://github.com/JamesIves/github-pages-deploy-action.git
synced 2023-12-15 20:03:39 +08:00
SSH Issues (#588)
This commit is contained in:
parent
cb9b5aee83
commit
2963e5ecf1
@ -1,4 +1,6 @@
|
|||||||
|
import {exportVariable} from '@actions/core'
|
||||||
import {mkdirP} from '@actions/io'
|
import {mkdirP} from '@actions/io'
|
||||||
|
import child_process, {execFileSync, execSync} from 'child_process'
|
||||||
import {appendFileSync} from 'fs'
|
import {appendFileSync} from 'fs'
|
||||||
import {action, TestFlag} from '../src/constants'
|
import {action, TestFlag} from '../src/constants'
|
||||||
import {execute} from '../src/execute'
|
import {execute} from '../src/execute'
|
||||||
@ -11,6 +13,11 @@ jest.mock('fs', () => ({
|
|||||||
existsSync: jest.fn()
|
existsSync: jest.fn()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
jest.mock('child_process', () => ({
|
||||||
|
execFileSync: jest.fn(),
|
||||||
|
execSync: jest.fn()
|
||||||
|
}))
|
||||||
|
|
||||||
jest.mock('@actions/io', () => ({
|
jest.mock('@actions/io', () => ({
|
||||||
rmRF: jest.fn(),
|
rmRF: jest.fn(),
|
||||||
mkdirP: jest.fn()
|
mkdirP: jest.fn()
|
||||||
@ -21,12 +28,11 @@ jest.mock('@actions/core', () => ({
|
|||||||
getInput: jest.fn(),
|
getInput: jest.fn(),
|
||||||
setOutput: jest.fn(),
|
setOutput: jest.fn(),
|
||||||
isDebug: jest.fn(),
|
isDebug: jest.fn(),
|
||||||
info: jest.fn()
|
info: jest.fn(),
|
||||||
|
exportVariable: jest.fn()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
jest.mock('../src/execute', () => ({
|
jest.mock('../src/execute', () => ({
|
||||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
||||||
__esModule: true,
|
|
||||||
execute: jest.fn()
|
execute: jest.fn()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
@ -56,6 +62,10 @@ describe('configureSSH', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should configure the ssh client if a key is defined', async () => {
|
it('should configure the ssh client if a key is defined', async () => {
|
||||||
|
;(child_process.execFileSync as jest.Mock).mockImplementationOnce(() => {
|
||||||
|
return 'SSH_AUTH_SOCK=/some/random/folder/agent.123; export SSH_AUTH_SOCK;\nSSH_AGENT_PID=123; export SSH_AGENT_PID;'
|
||||||
|
})
|
||||||
|
|
||||||
Object.assign(action, {
|
Object.assign(action, {
|
||||||
silent: false,
|
silent: false,
|
||||||
folder: 'assets',
|
folder: 'assets',
|
||||||
@ -70,13 +80,37 @@ describe('configureSSH', () => {
|
|||||||
|
|
||||||
await configureSSH(action)
|
await configureSSH(action)
|
||||||
|
|
||||||
expect(execute).toBeCalledTimes(4)
|
expect(execFileSync).toBeCalledTimes(1)
|
||||||
expect(mkdirP).toBeCalledTimes(1)
|
expect(exportVariable).toBeCalledTimes(2)
|
||||||
expect(appendFileSync).toBeCalledTimes(2)
|
expect(execSync).toBeCalledTimes(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not export variables if the return from ssh-agent is skewed', async () => {
|
||||||
|
;(child_process.execFileSync as jest.Mock).mockImplementationOnce(() => {
|
||||||
|
return 'useless nonsense here;'
|
||||||
|
})
|
||||||
|
|
||||||
|
Object.assign(action, {
|
||||||
|
silent: false,
|
||||||
|
folder: 'assets',
|
||||||
|
branch: 'branch',
|
||||||
|
sshKey: '?=-----BEGIN 123 456\n 789',
|
||||||
|
pusher: {
|
||||||
|
name: 'asd',
|
||||||
|
email: 'as@cat'
|
||||||
|
},
|
||||||
|
isTest: TestFlag.HAS_CHANGED_FILES
|
||||||
|
})
|
||||||
|
|
||||||
|
await configureSSH(action)
|
||||||
|
|
||||||
|
expect(execFileSync).toBeCalledTimes(1)
|
||||||
|
expect(exportVariable).toBeCalledTimes(0)
|
||||||
|
expect(execSync).toBeCalledTimes(3)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should throw if something errors', async () => {
|
it('should throw if something errors', async () => {
|
||||||
;(execute as jest.Mock).mockImplementationOnce(() => {
|
;(child_process.execFileSync as jest.Mock).mockImplementationOnce(() => {
|
||||||
throw new Error('Mocked throw')
|
throw new Error('Mocked throw')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
20
src/ssh.ts
20
src/ssh.ts
@ -1,8 +1,8 @@
|
|||||||
import {info} from '@actions/core'
|
import {exportVariable, info} from '@actions/core'
|
||||||
import {mkdirP} from '@actions/io'
|
import {mkdirP} from '@actions/io'
|
||||||
|
import {execFileSync, execSync} from 'child_process'
|
||||||
import {appendFileSync} from 'fs'
|
import {appendFileSync} from 'fs'
|
||||||
import {ActionInterface} from './constants'
|
import {ActionInterface} from './constants'
|
||||||
import {execute} from './execute'
|
|
||||||
import {suppressSensitiveInformation} from './util'
|
import {suppressSensitiveInformation} from './util'
|
||||||
|
|
||||||
export async function configureSSH(action: ActionInterface): Promise<void> {
|
export async function configureSSH(action: ActionInterface): Promise<void> {
|
||||||
@ -25,14 +25,24 @@ export async function configureSSH(action: ActionInterface): Promise<void> {
|
|||||||
appendFileSync(sshKnownHostsDirectory, sshGitHubKnownHostDss)
|
appendFileSync(sshKnownHostsDirectory, sshGitHubKnownHostDss)
|
||||||
|
|
||||||
// Initializes SSH agent.
|
// Initializes SSH agent.
|
||||||
await execute(`ssh-agent`, sshDirectory, action.silent)
|
const agentOutput = execFileSync('ssh-agent').toString().split('\n')
|
||||||
|
|
||||||
|
agentOutput.map(line => {
|
||||||
|
const exportableVariables = /^(SSH_AUTH_SOCK|SSH_AGENT_PID)=(.*); export \1/.exec(
|
||||||
|
line
|
||||||
|
)
|
||||||
|
|
||||||
|
if (exportableVariables && exportableVariables.length) {
|
||||||
|
exportVariable(exportableVariables[1], exportableVariables[2])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Adds the SSH key to the agent.
|
// Adds the SSH key to the agent.
|
||||||
action.sshKey.split(/(?=-----BEGIN)/).map(async line => {
|
action.sshKey.split(/(?=-----BEGIN)/).map(async line => {
|
||||||
await execute(`ssh-add - ${line.trim()}\n`, sshDirectory, action.silent)
|
execSync('ssh-add -', {input: `${line.trim()}\n`})
|
||||||
})
|
})
|
||||||
|
|
||||||
await execute(`ssh-add -l`, sshDirectory, action.silent)
|
execSync('ssh-add -l')
|
||||||
} else {
|
} else {
|
||||||
info(`Skipping SSH client configuration… ⌚`)
|
info(`Skipping SSH client configuration… ⌚`)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user