diff --git a/lib/index.js b/lib/index.js index 0a5731525..5e237ae82 100644 --- a/lib/index.js +++ b/lib/index.js @@ -22017,12 +22017,20 @@ function checkManifestStability(kubectl, resources) { exports.checkManifestStability = checkManifestStability; function annotateAndLabelResources(files, kubectl, resourceTypes, allPods) { return __awaiter(this, void 0, void 0, function* () { + const defaultWorkflowFileName = 'k8s-deploy-failed-workflow-annotation'; const githubToken = core.getInput('token'); - const workflowFilePath = yield githubUtils_1.getWorkflowFilePath(githubToken); + let workflowFilePath; + try { + workflowFilePath = yield githubUtils_1.getWorkflowFilePath(githubToken); + } + catch (ex) { + core.warning(`Failed to extract workflow file name: ${ex}`); + workflowFilePath = defaultWorkflowFileName; + } const deploymentConfig = yield dockerUtils_1.getDeploymentConfig(); const annotationKeyLabel = workflowAnnotationUtils_1.getWorkflowAnnotationKeyLabel(); - yield annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel, workflowFilePath, deploymentConfig); - yield labelResources(files, kubectl, annotationKeyLabel); + yield annotateResources(files, kubectl, resourceTypes, allPods, annotationKeyLabel, workflowFilePath, deploymentConfig).catch((err) => core.warning(`Failed to annotate resources: ${err} `)); + yield labelResources(files, kubectl, annotationKeyLabel).catch((err) => core.warning(`Failed to label resources: ${err}`)); }); } exports.annotateAndLabelResources = annotateAndLabelResources; @@ -22360,13 +22368,17 @@ class Kubectl { let newReplicaSet = ''; if (result === null || result === void 0 ? void 0 : result.stdout) { const stdout = result.stdout.split('\n'); + core.debug('stdout from getNewReplicaSet is ' + JSON.stringify(stdout)); stdout.forEach((line) => { 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 .substring(newreplicaset.length) .trim() .split(' ')[0]; + } }); } return newReplicaSet; @@ -23803,8 +23815,9 @@ exports.getTrafficSplitAPIVersion = getTrafficSplitAPIVersion; "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.cleanLabel = exports.getWorkflowAnnotationKeyLabel = exports.getWorkflowAnnotations = void 0; +exports.removeInvalidLabelCharacters = exports.cleanLabel = exports.getWorkflowAnnotationKeyLabel = exports.getWorkflowAnnotations = exports.VALID_LABEL_REGEX = void 0; const ANNOTATION_PREFIX = 'actions.github.com'; +exports.VALID_LABEL_REGEX = /([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]/; function getWorkflowAnnotations(lastSuccessRunSha, workflowFilePath, deploymentConfig) { const annotationObject = { run: process.env.GITHUB_RUN_ID, @@ -23836,14 +23849,20 @@ exports.getWorkflowAnnotationKeyLabel = getWorkflowAnnotationKeyLabel; * @returns cleaned label */ function cleanLabel(label) { - let removedInvalidChars = label + let removedInvalidChars = removeInvalidLabelCharacters(label); + const regexResult = exports.VALID_LABEL_REGEX.exec(removedInvalidChars) || [ + 'github-workflow-file' + ]; + return regexResult[0]; +} +exports.cleanLabel = cleanLabel; +function removeInvalidLabelCharacters(label) { + return label .replace(/\s/gi, '_') .replace(/[\/\\\|]/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] || ''; } -exports.cleanLabel = cleanLabel; +exports.removeInvalidLabelCharacters = removeInvalidLabelCharacters; /***/ }), diff --git a/src/strategyHelpers/deploymentHelper.ts b/src/strategyHelpers/deploymentHelper.ts index bd1d7c029..30336e5fb 100644 --- a/src/strategyHelpers/deploymentHelper.ts +++ b/src/strategyHelpers/deploymentHelper.ts @@ -150,8 +150,15 @@ export async function annotateAndLabelResources( resourceTypes: Resource[], allPods: any ) { + const defaultWorkflowFileName = 'k8s-deploy-failed-workflow-annotation' 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 annotationKeyLabel = getWorkflowAnnotationKeyLabel() @@ -164,8 +171,11 @@ export async function annotateAndLabelResources( annotationKeyLabel, workflowFilePath, 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( diff --git a/src/types/kubectl.ts b/src/types/kubectl.ts index 831c859e6..61e0d8d6a 100644 --- a/src/types/kubectl.ts +++ b/src/types/kubectl.ts @@ -70,13 +70,21 @@ export class Kubectl { let newReplicaSet = '' if (result?.stdout) { const stdout = result.stdout.split('\n') + core.debug('stdout from getNewReplicaSet is ' + JSON.stringify(stdout)) stdout.forEach((line: string) => { 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 .substring(newreplicaset.length) .trim() .split(' ')[0] + } }) } diff --git a/src/utilities/workflowAnnotationUtils.test.ts b/src/utilities/workflowAnnotationUtils.test.ts index bed2bd1a8..f989e879e 100644 --- a/src/utilities/workflowAnnotationUtils.test.ts +++ b/src/utilities/workflowAnnotationUtils.test.ts @@ -1,4 +1,8 @@ -import {cleanLabel} from '../utilities/workflowAnnotationUtils' +import { + cleanLabel, + removeInvalidLabelCharacters, + VALID_LABEL_REGEX +} from '../utilities/workflowAnnotationUtils' describe('WorkflowAnnotationUtils', () => { describe('cleanLabel', () => { @@ -16,5 +20,14 @@ describe('WorkflowAnnotationUtils', () => { cleanLabel('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) + }) }) }) diff --git a/src/utilities/workflowAnnotationUtils.ts b/src/utilities/workflowAnnotationUtils.ts index 248b53f33..3c4e18a36 100644 --- a/src/utilities/workflowAnnotationUtils.ts +++ b/src/utilities/workflowAnnotationUtils.ts @@ -2,6 +2,8 @@ import {DeploymentConfig} from '../types/deploymentConfig' 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( lastSuccessRunSha: string, workflowFilePath: string, @@ -37,11 +39,17 @@ export function getWorkflowAnnotationKeyLabel(): string { * @returns cleaned label */ 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(/[\/\\\|]/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] || '' }