Родитель
ad02432be6
Коммит
ad10104b67
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -20,15 +20,16 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.2.6",
|
"@actions/core": "^1.2.6",
|
||||||
"@actions/exec": "^1.1.0",
|
"@actions/exec": "^1.1.0",
|
||||||
"@actions/io": "^1.1.1"
|
"@actions/io": "^1.1.1",
|
||||||
|
"crypto": "^1.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^25.2.2",
|
"@types/jest": "^25.2.2",
|
||||||
"@types/node": "^12.0.4",
|
"@types/node": "^12.0.4",
|
||||||
"@vercel/ncc": "^0.34.0",
|
"@vercel/ncc": "^0.34.0",
|
||||||
"jest": "^25.5.4",
|
"jest": "^26.0.0",
|
||||||
"prettier": "2.7.1",
|
"prettier": "2.7.1",
|
||||||
"ts-jest": "^25.5.1",
|
"ts-jest": "^26.0.0",
|
||||||
"typescript": "3.9.2"
|
"typescript": "3.9.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
132
src/run.ts
132
src/run.ts
|
@ -3,66 +3,102 @@ import * as io from '@actions/io'
|
||||||
import * as exec from '@actions/exec'
|
import * as exec from '@actions/exec'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
|
import * as crypto from 'crypto'
|
||||||
|
|
||||||
const AZ_TOOL_NAME = 'az'
|
const AZ_TOOL_NAME = 'az'
|
||||||
const KUBELOGIN_TOOL_NAME = 'kubelogin'
|
const KUBELOGIN_TOOL_NAME = 'kubelogin'
|
||||||
|
const ACTION_NAME = 'Azure/aks-set-context'
|
||||||
|
const AZ_USER_AGENT_ENV = 'AZURE_HTTP_USER_AGENT'
|
||||||
|
const AZ_USER_AGENT_ENV_PS = 'AZUREPS_HOST_ENVIRONMENT'
|
||||||
|
|
||||||
export async function run() {
|
export async function run() {
|
||||||
// get inputs
|
const originalAzUserAgent = process.env[AZ_USER_AGENT_ENV] || ''
|
||||||
const resourceGroupName = core.getInput('resource-group', {required: true})
|
const originalAzUserAgentPs = process.env[AZ_USER_AGENT_ENV_PS] || ''
|
||||||
const clusterName = core.getInput('cluster-name', {required: true})
|
|
||||||
const subscription = core.getInput('subscription') || ''
|
|
||||||
const adminInput = core.getInput('admin') || ''
|
|
||||||
const admin = adminInput.toLowerCase() === 'true'
|
|
||||||
const useKubeLoginInput = core.getInput('use-kubelogin') || ''
|
|
||||||
const useKubeLogin = useKubeLoginInput.toLowerCase() === 'true' && !admin
|
|
||||||
|
|
||||||
// check az tools
|
// use try finally to always unset temp user agent
|
||||||
const azPath = await io.which(AZ_TOOL_NAME, false)
|
try {
|
||||||
if (!azPath)
|
// set az user agent
|
||||||
throw Error(
|
core.exportVariable(AZ_USER_AGENT_ENV, getUserAgent(originalAzUserAgent))
|
||||||
'Az cli tools not installed. You must install them before running this action.'
|
core.exportVariable(
|
||||||
|
AZ_USER_AGENT_ENV_PS,
|
||||||
|
getUserAgent(originalAzUserAgentPs)
|
||||||
)
|
)
|
||||||
|
|
||||||
// get kubeconfig
|
// get inputs
|
||||||
const runnerTempDirectory = process.env['RUNNER_TEMP'] // use process.env until the core libs are updated
|
const resourceGroupName = core.getInput('resource-group', {
|
||||||
const kubeconfigPath = path.join(
|
required: true
|
||||||
runnerTempDirectory,
|
})
|
||||||
`kubeconfig_${Date.now()}`
|
const clusterName = core.getInput('cluster-name', {required: true})
|
||||||
)
|
const subscription = core.getInput('subscription') || ''
|
||||||
core.debug(`Writing kubeconfig to ${kubeconfigPath}`)
|
const adminInput = core.getInput('admin') || ''
|
||||||
const cmd = [
|
const admin = adminInput.toLowerCase() === 'true'
|
||||||
'aks',
|
const useKubeLoginInput = core.getInput('use-kubelogin') || ''
|
||||||
'get-credentials',
|
const useKubeLogin = useKubeLoginInput.toLowerCase() === 'true' && !admin
|
||||||
'--resource-group',
|
|
||||||
resourceGroupName,
|
|
||||||
'--name',
|
|
||||||
clusterName,
|
|
||||||
'-f',
|
|
||||||
kubeconfigPath
|
|
||||||
]
|
|
||||||
if (subscription) cmd.push('--subscription', subscription)
|
|
||||||
if (admin) cmd.push('--admin')
|
|
||||||
|
|
||||||
const exitCode = await exec.exec(AZ_TOOL_NAME, cmd)
|
// check az tools
|
||||||
if (exitCode !== 0) throw Error('az cli exited with error code ' + exitCode)
|
const azPath = await io.which(AZ_TOOL_NAME, false)
|
||||||
|
if (!azPath)
|
||||||
|
throw Error(
|
||||||
|
'Az cli tools not installed. You must install them before running this action.'
|
||||||
|
)
|
||||||
|
|
||||||
fs.chmodSync(kubeconfigPath, '600')
|
// get kubeconfig
|
||||||
|
const runnerTempDirectory = process.env['RUNNER_TEMP'] // use process.env until the core libs are updated
|
||||||
// export variable
|
const kubeconfigPath = path.join(
|
||||||
core.exportVariable('KUBECONFIG', kubeconfigPath)
|
runnerTempDirectory,
|
||||||
core.debug('KUBECONFIG environment variable set')
|
`kubeconfig_${Date.now()}`
|
||||||
|
|
||||||
if (useKubeLogin) {
|
|
||||||
const kubeloginCmd = ['convert-kubeconfig', '-l', 'azurecli']
|
|
||||||
|
|
||||||
const kubeloginExitCode = await exec.exec(
|
|
||||||
KUBELOGIN_TOOL_NAME,
|
|
||||||
kubeloginCmd
|
|
||||||
)
|
)
|
||||||
if (kubeloginExitCode !== 0)
|
core.debug(`Writing kubeconfig to ${kubeconfigPath}`)
|
||||||
throw Error('kubelogin exited with error code ' + exitCode)
|
const cmd = [
|
||||||
|
'aks',
|
||||||
|
'get-credentials',
|
||||||
|
'--resource-group',
|
||||||
|
resourceGroupName,
|
||||||
|
'--name',
|
||||||
|
clusterName,
|
||||||
|
'-f',
|
||||||
|
kubeconfigPath
|
||||||
|
]
|
||||||
|
if (subscription) cmd.push('--subscription', subscription)
|
||||||
|
if (admin) cmd.push('--admin')
|
||||||
|
|
||||||
|
const exitCode = await exec.exec(AZ_TOOL_NAME, cmd)
|
||||||
|
if (exitCode !== 0)
|
||||||
|
throw Error('az cli exited with error code ' + exitCode)
|
||||||
|
|
||||||
|
fs.chmodSync(kubeconfigPath, '600')
|
||||||
|
|
||||||
|
// export variable
|
||||||
|
core.exportVariable('KUBECONFIG', kubeconfigPath)
|
||||||
|
core.debug('KUBECONFIG environment variable set')
|
||||||
|
|
||||||
|
if (useKubeLogin) {
|
||||||
|
const kubeloginCmd = ['convert-kubeconfig', '-l', 'azurecli']
|
||||||
|
|
||||||
|
const kubeloginExitCode = await exec.exec(
|
||||||
|
KUBELOGIN_TOOL_NAME,
|
||||||
|
kubeloginCmd
|
||||||
|
)
|
||||||
|
if (kubeloginExitCode !== 0)
|
||||||
|
throw Error('kubelogin exited with error code ' + exitCode)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw e
|
||||||
|
} finally {
|
||||||
|
core.exportVariable(AZ_USER_AGENT_ENV, originalAzUserAgent)
|
||||||
|
core.exportVariable(AZ_USER_AGENT_ENV_PS, originalAzUserAgentPs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getUserAgent(prevUserAgent: string): string {
|
||||||
|
const actionName = process.env.GITHUB_ACTION_REPOSITORY || ACTION_NAME
|
||||||
|
const runRepo = process.env.GITHUB_REPOSITORY || ''
|
||||||
|
const runRepoHash = crypto.createHash('sha256').update(runRepo).digest('hex')
|
||||||
|
const runId = process.env.GITHUB_RUN_ID
|
||||||
|
const newUserAgent = `GitHubActions/${actionName}(${runRepoHash}; ${runId})`
|
||||||
|
|
||||||
|
if (prevUserAgent) return `${prevUserAgent}+${newUserAgent}`
|
||||||
|
return newUserAgent
|
||||||
|
}
|
||||||
|
|
||||||
run().catch(core.setFailed)
|
run().catch(core.setFailed)
|
||||||
|
|
Загрузка…
Ссылка в новой задаче