mirror of
https://github.com/Azure/k8s-deploy.git
synced 2026-06-24 05:29:26 +08:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5782616d03 | |||
| c7b34876bb | |||
| d89c89ba4e | |||
| a2de818915 | |||
| 2ee6236ebc | |||
| bba74ad3b5 | |||
| 4e60e959ea | |||
| 497ce6351c | |||
| 6ecb006985 | |||
| d7506e9702 |
@@ -2,6 +2,5 @@ node_modules
|
|||||||
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
.idea
|
.idea
|
||||||
lib/
|
|
||||||
|
|
||||||
coverage/
|
coverage/
|
||||||
+24076
File diff suppressed because one or more lines are too long
@@ -150,8 +150,15 @@ export async function annotateAndLabelResources(
|
|||||||
resourceTypes: Resource[],
|
resourceTypes: Resource[],
|
||||||
allPods: any
|
allPods: any
|
||||||
) {
|
) {
|
||||||
|
const defaultWorkflowFileName = 'k8s-deploy-failed-workflow-annotation'
|
||||||
const githubToken = core.getInput('token')
|
const githubToken = core.getInput('token')
|
||||||
const workflowFilePath = await getWorkflowFilePath(githubToken)
|
let workflowFilePath
|
||||||
|
try {
|
||||||
|
workflowFilePath = await getWorkflowFilePath(githubToken)
|
||||||
|
} catch (ex) {
|
||||||
|
core.warning(`Failed to extract workflow file name: ${ex}`)
|
||||||
|
workflowFilePath = defaultWorkflowFileName
|
||||||
|
}
|
||||||
|
|
||||||
const deploymentConfig = await getDeploymentConfig()
|
const deploymentConfig = await getDeploymentConfig()
|
||||||
const annotationKeyLabel = getWorkflowAnnotationKeyLabel()
|
const annotationKeyLabel = getWorkflowAnnotationKeyLabel()
|
||||||
@@ -164,8 +171,11 @@ export async function annotateAndLabelResources(
|
|||||||
annotationKeyLabel,
|
annotationKeyLabel,
|
||||||
workflowFilePath,
|
workflowFilePath,
|
||||||
deploymentConfig
|
deploymentConfig
|
||||||
|
).catch((err) => core.warning(`Failed to annotate resources: ${err} `))
|
||||||
|
|
||||||
|
await labelResources(files, kubectl, annotationKeyLabel).catch((err) =>
|
||||||
|
core.warning(`Failed to label resources: ${err}`)
|
||||||
)
|
)
|
||||||
await labelResources(files, kubectl, annotationKeyLabel)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function annotateResources(
|
async function annotateResources(
|
||||||
@@ -258,7 +268,7 @@ async function labelResources(
|
|||||||
const labelResults = []
|
const labelResults = []
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
try {
|
try {
|
||||||
const labelResult = await kubectl.labelFiles(files, labels)
|
const labelResult = await kubectl.labelFiles(file, labels)
|
||||||
labelResults.push(labelResult)
|
labelResults.push(labelResult)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
core.warning(`failed to annotate resource: ${e}`)
|
core.warning(`failed to annotate resource: ${e}`)
|
||||||
|
|||||||
@@ -70,13 +70,21 @@ export class Kubectl {
|
|||||||
let newReplicaSet = ''
|
let newReplicaSet = ''
|
||||||
if (result?.stdout) {
|
if (result?.stdout) {
|
||||||
const stdout = result.stdout.split('\n')
|
const stdout = result.stdout.split('\n')
|
||||||
|
core.debug('stdout from getNewReplicaSet is ' + JSON.stringify(stdout))
|
||||||
stdout.forEach((line: string) => {
|
stdout.forEach((line: string) => {
|
||||||
const newreplicaset = 'newreplicaset'
|
const newreplicaset = 'newreplicaset'
|
||||||
if (line && line.toLowerCase().indexOf(newreplicaset) > -1)
|
if (line && line.toLowerCase().indexOf(newreplicaset) > -1) {
|
||||||
|
core.debug(
|
||||||
|
`found string of interest for replicaset, line is ${line}`
|
||||||
|
)
|
||||||
|
core.debug(
|
||||||
|
`substring is ${line.substring(newreplicaset.length).trim()}`
|
||||||
|
)
|
||||||
newReplicaSet = line
|
newReplicaSet = line
|
||||||
.substring(newreplicaset.length)
|
.substring(newreplicaset.length)
|
||||||
.trim()
|
.trim()
|
||||||
.split(' ')[0]
|
.split(' ')[0]
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
import {cleanLabel} from '../utilities/workflowAnnotationUtils'
|
import {
|
||||||
|
cleanLabel,
|
||||||
|
removeInvalidLabelCharacters,
|
||||||
|
VALID_LABEL_REGEX
|
||||||
|
} from '../utilities/workflowAnnotationUtils'
|
||||||
|
|
||||||
describe('WorkflowAnnotationUtils', () => {
|
describe('WorkflowAnnotationUtils', () => {
|
||||||
describe('cleanLabel', () => {
|
describe('cleanLabel', () => {
|
||||||
@@ -16,5 +20,14 @@ describe('WorkflowAnnotationUtils', () => {
|
|||||||
cleanLabel('Workflow Name / With Slashes / And Spaces')
|
cleanLabel('Workflow Name / With Slashes / And Spaces')
|
||||||
).toEqual('Workflow_Name_-_With_Slashes_-_And_Spaces')
|
).toEqual('Workflow_Name_-_With_Slashes_-_And_Spaces')
|
||||||
})
|
})
|
||||||
|
it('should return a blank string when regex fails (https://github.com/Azure/k8s-deploy/issues/266)', () => {
|
||||||
|
const label = '持续部署'
|
||||||
|
expect(cleanLabel(label)).toEqual('github-workflow-file')
|
||||||
|
|
||||||
|
let removedInvalidChars = removeInvalidLabelCharacters(label)
|
||||||
|
|
||||||
|
const regexResult = VALID_LABEL_REGEX.exec(removedInvalidChars)
|
||||||
|
expect(regexResult).toBe(null)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import {DeploymentConfig} from '../types/deploymentConfig'
|
|||||||
|
|
||||||
const ANNOTATION_PREFIX = 'actions.github.com'
|
const ANNOTATION_PREFIX = 'actions.github.com'
|
||||||
|
|
||||||
|
export const VALID_LABEL_REGEX = /([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]/
|
||||||
|
|
||||||
export function getWorkflowAnnotations(
|
export function getWorkflowAnnotations(
|
||||||
lastSuccessRunSha: string,
|
lastSuccessRunSha: string,
|
||||||
workflowFilePath: string,
|
workflowFilePath: string,
|
||||||
@@ -37,11 +39,17 @@ export function getWorkflowAnnotationKeyLabel(): string {
|
|||||||
* @returns cleaned label
|
* @returns cleaned label
|
||||||
*/
|
*/
|
||||||
export function cleanLabel(label: string): string {
|
export function cleanLabel(label: string): string {
|
||||||
let removedInvalidChars = label
|
let removedInvalidChars = removeInvalidLabelCharacters(label)
|
||||||
|
|
||||||
|
const regexResult = VALID_LABEL_REGEX.exec(removedInvalidChars) || [
|
||||||
|
'github-workflow-file'
|
||||||
|
]
|
||||||
|
return regexResult[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeInvalidLabelCharacters(label: string): string {
|
||||||
|
return label
|
||||||
.replace(/\s/gi, '_')
|
.replace(/\s/gi, '_')
|
||||||
.replace(/[\/\\\|]/gi, '-')
|
.replace(/[\/\\\|]/gi, '-')
|
||||||
.replace(/[^-A-Za-z0-9_.]/gi, '')
|
.replace(/[^-A-Za-z0-9_.]/gi, '')
|
||||||
|
|
||||||
const regex = /([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]/
|
|
||||||
return regex.exec(removedInvalidChars)[0] || ''
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user