* add user agent
This commit is contained in:
Oliver King 2022-07-21 10:20:41 -04:00 коммит произвёл GitHub
Родитель ad02432be6
Коммит ad10104b67
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 6369 добавлений и 3602 удалений

9832
package-lock.json сгенерированный

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -20,15 +20,16 @@
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/exec": "^1.1.0",
"@actions/io": "^1.1.1"
"@actions/io": "^1.1.1",
"crypto": "^1.0.1"
},
"devDependencies": {
"@types/jest": "^25.2.2",
"@types/node": "^12.0.4",
"@vercel/ncc": "^0.34.0",
"jest": "^25.5.4",
"jest": "^26.0.0",
"prettier": "2.7.1",
"ts-jest": "^25.5.1",
"ts-jest": "^26.0.0",
"typescript": "3.9.2"
}
}

Просмотреть файл

@ -3,66 +3,102 @@ import * as io from '@actions/io'
import * as exec from '@actions/exec'
import * as path from 'path'
import * as fs from 'fs'
import * as crypto from 'crypto'
const AZ_TOOL_NAME = 'az'
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() {
// get inputs
const resourceGroupName = core.getInput('resource-group', {required: true})
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
const originalAzUserAgent = process.env[AZ_USER_AGENT_ENV] || ''
const originalAzUserAgentPs = process.env[AZ_USER_AGENT_ENV_PS] || ''
// check az tools
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.'
// use try finally to always unset temp user agent
try {
// set az user agent
core.exportVariable(AZ_USER_AGENT_ENV, getUserAgent(originalAzUserAgent))
core.exportVariable(
AZ_USER_AGENT_ENV_PS,
getUserAgent(originalAzUserAgentPs)
)
// get kubeconfig
const runnerTempDirectory = process.env['RUNNER_TEMP'] // use process.env until the core libs are updated
const kubeconfigPath = path.join(
runnerTempDirectory,
`kubeconfig_${Date.now()}`
)
core.debug(`Writing kubeconfig to ${kubeconfigPath}`)
const cmd = [
'aks',
'get-credentials',
'--resource-group',
resourceGroupName,
'--name',
clusterName,
'-f',
kubeconfigPath
]
if (subscription) cmd.push('--subscription', subscription)
if (admin) cmd.push('--admin')
// get inputs
const resourceGroupName = core.getInput('resource-group', {
required: true
})
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
const exitCode = await exec.exec(AZ_TOOL_NAME, cmd)
if (exitCode !== 0) throw Error('az cli exited with error code ' + exitCode)
// check az tools
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')
// 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
// get kubeconfig
const runnerTempDirectory = process.env['RUNNER_TEMP'] // use process.env until the core libs are updated
const kubeconfigPath = path.join(
runnerTempDirectory,
`kubeconfig_${Date.now()}`
)
if (kubeloginExitCode !== 0)
throw Error('kubelogin exited with error code ' + exitCode)
core.debug(`Writing kubeconfig to ${kubeconfigPath}`)
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)