-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from bcgov-nr/feat/ghNodejsBuild
Feat: gh nodejs build
- Loading branch information
Showing
11 changed files
with
741 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM node:21-alpine | ||
FROM node:22-alpine | ||
|
||
ARG APP=app | ||
ARG HOME=/home/node | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,329 @@ | ||
'use strict'; | ||
import * as fs from 'fs'; | ||
import Generator from 'yeoman-generator'; | ||
import yosay from 'yosay'; | ||
import chalk from 'chalk'; | ||
import { Document, parseDocument } from 'yaml'; | ||
import { | ||
BACKSTAGE_FILENAME, | ||
pathToProps, | ||
extractFromYaml, | ||
generateSetAnswerPropPredicate, | ||
writePropToPath, | ||
} from '../util/yaml.js'; | ||
|
||
/** | ||
* Generate the CI workflow and NR Broker intention files needed for Java/Tomcat Maven builds in GitHub | ||
*/ | ||
export default class extends Generator { | ||
constructor(args, opts) { | ||
super(args, opts); | ||
|
||
this.option('promptless', { | ||
type: String, | ||
required: false, | ||
description: 'Run headless with no user prompts', | ||
}); | ||
} | ||
|
||
async initializing() { | ||
const backstagePath = this.destinationPath(BACKSTAGE_FILENAME); | ||
if (fs.existsSync(backstagePath)) { | ||
const backstageYaml = fs.readFileSync(backstagePath, 'utf8'); | ||
this.backstageDoc = parseDocument(backstageYaml); | ||
} else { | ||
this.backstageDoc = new Document(); | ||
} | ||
} | ||
|
||
async prompting() { | ||
const promptless = !!this.options.promptless; | ||
this.answers = extractFromYaml(this.backstageDoc, pathToProps); | ||
console.log(this.answers); | ||
|
||
if (!promptless) { | ||
this.log( | ||
yosay( | ||
'Welcome to the GitHub workflow and NR Broker intention file generator!', | ||
), | ||
); | ||
|
||
this.log(chalk.bold('Usage')); | ||
this.log(''); | ||
this.log( | ||
' ' + | ||
chalk.bold('Project: ') + | ||
chalk.dim( | ||
'Lowercase kebab-case name that uniquely identifies the project', | ||
), | ||
); | ||
this.log(' ' + chalk.dim('Example: super-project')); | ||
this.log( | ||
' ' + | ||
chalk.bold('Service: ') + | ||
chalk.dim( | ||
'Lowercase kebab-case name that uniquely indentifies the service', | ||
), | ||
); | ||
this.log( | ||
' ' + | ||
chalk.dim( | ||
'Should start with project, have an optional descriptor and end with an artifact identifier', | ||
), | ||
); | ||
this.log( | ||
' ' + chalk.dim('Example: super-project-backend-war'), | ||
); | ||
this.log( | ||
' ' + | ||
chalk.bold('Client ID: ') + | ||
chalk.dim( | ||
'The client id of the Broker account to use. Leave blank to use manually set BROKER_JWT secret.', | ||
), | ||
); | ||
this.log( | ||
' ' + | ||
chalk.bold('Artifactory: ') + | ||
chalk.dim('The OCP Artifactory namespace this will be published to'), | ||
); | ||
this.log( | ||
' ' + | ||
chalk.bold('Root: ') + | ||
chalk.dim( | ||
"The path to where your build is located relative to the repository's root", | ||
), | ||
); | ||
this.log( | ||
' ' + | ||
chalk.bold('GitHub Owner with repo path: ') + | ||
chalk.dim( | ||
'The Github owner with repo path (e.g. bcgov-nr/edqa-war) ', | ||
), | ||
); | ||
this.log(''); | ||
|
||
this.log(chalk.bold('Prompts')); | ||
this.log(''); | ||
} | ||
|
||
const backstageAnswer = promptless | ||
? { skip: true } | ||
: await this.prompt([ | ||
{ | ||
type: 'checkbox', | ||
name: 'skip', | ||
message: `Skip prompts for values found in Backstage file (${BACKSTAGE_FILENAME}):`, | ||
choices: ['yes', 'no'], | ||
default: 'yes', | ||
store: true, | ||
}, | ||
]); | ||
|
||
this.answers = { | ||
...this.answers, | ||
...(await this.prompt( | ||
[ | ||
{ | ||
type: 'input', | ||
name: 'projectName', | ||
message: 'Project:', | ||
store: true, | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'serviceName', | ||
message: 'Service:', | ||
store: true, | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'clientId', | ||
message: 'Client ID:', | ||
default: '', | ||
store: true, | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'unitTestsPath', | ||
message: 'Path to unit tests (./.github/workflows/test.yaml):', | ||
default: '', | ||
store: true, | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'gitHubPackages', | ||
message: 'Publish to GitHub Packages:', | ||
default: false, | ||
store: true, | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'gitHubOwnerPack', | ||
message: 'GitHub Owner with repo path (e.g. bcgov-nr/results-war):', | ||
store: true, | ||
when: (answers) => answers.gitHubPackages, | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'artifactoryProject', | ||
message: 'Artifactory:', | ||
default: 'cc20', | ||
store: true, | ||
when: (answers) => !answers.gitHubPackages, | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'artifactoryPackageType', | ||
message: 'Artifactory Package Type (maven, ivy, npm):', | ||
default: 'maven', | ||
store: true, | ||
when: (answers) => !answers.gitHubPackages, | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'deployOnPrem', | ||
message: 'Deploy on-prem:', | ||
default: false, | ||
store: true, | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'playbookPath', | ||
message: 'Playbook path:', | ||
default: 'playbooks', | ||
store: true, | ||
when: (answers) => answers.deployOnPrem, | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'tomcatContext', | ||
message: 'Tomcat Context (e.g. ext#results):', | ||
store: true, | ||
when: (answers) => answers.deployOnPrem, | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'useAltAppDirName', | ||
message: 'Use alternative webapp directory:', | ||
default: false, | ||
store: true, | ||
when: (answers) => answers.deployOnPrem, | ||
}, | ||
{ | ||
type: 'input', | ||
name: 'altAppDirName', | ||
message: 'Alternative webapp directory name:', | ||
store: true, | ||
when: (answers) => answers.useAltAppDirName, | ||
}, | ||
{ | ||
type: 'confirm', | ||
name: 'addWebadeConfig', | ||
message: 'Add Webade configuration:', | ||
default: false, | ||
store: true, | ||
when: (answers) => answers.deployOnPrem, | ||
}, | ||
].filter( | ||
generateSetAnswerPropPredicate( | ||
this.answers, | ||
backstageAnswer.skip === 'yes', | ||
), | ||
), | ||
)), | ||
}; | ||
} | ||
|
||
async configuring() { | ||
// this.config.save(); | ||
} | ||
|
||
// Generate GitHub workflows and NR Broker intention files | ||
writingWorkflow() { | ||
const brokerJwt = this.answers.clientId.trim() | ||
? `broker-jwt:${this.answers.clientId.trim()}`.replace( | ||
/[^a-zA-Z0-9_]/g, | ||
'_', | ||
) | ||
: 'BROKER_JWT'; | ||
this.fs.copyTpl( | ||
this.templatePath('build-release.yaml'), | ||
this.destinationPath('.github/workflows/build-release.yaml'), | ||
{ | ||
projectName: this.answers.projectName, | ||
serviceName: this.answers.serviceName, | ||
brokerJwt, | ||
artifactoryProject: this.answers.artifactoryProject, | ||
pomRoot: this.answers.pomRoot, | ||
unitTestsPath: this.answers.unitTestsPath, | ||
gitHubPackages: this.answers.gitHubPackages, | ||
artifactoryPackageType: this.answers.artifactoryPackageType, | ||
gitHubOwnerPack: this.answers.gitHubOwnerPack, | ||
}, | ||
); | ||
this.fs.copyTpl( | ||
this.templatePath('build-intention.json'), | ||
this.destinationPath('.github/workflows/build-intention.json'), | ||
{ | ||
projectName: this.answers.projectName, | ||
serviceName: this.answers.serviceName, | ||
}, | ||
); | ||
this.fs.copyTpl( | ||
this.templatePath('build-intention.sh'), | ||
this.destinationPath('.github/workflows/build-intention.sh'), | ||
); | ||
this.fs.copyTpl( | ||
this.templatePath('check-token.yaml'), | ||
this.destinationPath('.github/workflows/check-token.yaml'), | ||
); | ||
if (this.answers.deployOnPrem) { | ||
this.fs.copyTpl( | ||
this.templatePath('deploy.yaml'), | ||
this.destinationPath('.github/workflows/deploy.yaml'), | ||
{ | ||
projectName: this.answers.projectName, | ||
serviceName: this.answers.serviceName, | ||
brokerJwt, | ||
artifactoryProject: this.answers.artifactoryProject, | ||
pomRoot: this.answers.pomRoot, | ||
gitHubPackages: this.answers.gitHubPackages, | ||
artifactoryPackageType: this.answers.artifactoryPackageType, | ||
gitHubOwnerPack: this.answers.gitHubOwnerPack, | ||
}, | ||
); | ||
this.fs.copyTpl( | ||
this.templatePath('deployment-intention.json'), | ||
this.destinationPath('.jenkins/deployment-intention.json'), | ||
{ | ||
projectName: this.answers.projectName, | ||
serviceName: this.answers.serviceName, | ||
}, | ||
); | ||
const playbook_args = [ | ||
this.answers.projectName, | ||
this.answers.serviceName, | ||
this.answers.playbookPath, | ||
this.answers.tomcatContext, | ||
this.answers.altAppDirName, | ||
]; | ||
const playbook_options = { | ||
addWebadeConfig: this.answers.addWebadeConfig, | ||
altAppDirName: this.answers.altAppDirName, | ||
}; | ||
this.composeWith( | ||
'nr-repository-composer:pd-ansible-playbook', | ||
playbook_args, | ||
playbook_options, | ||
); | ||
} | ||
} | ||
|
||
writingBackstage() { | ||
writePropToPath(this.backstageDoc, pathToProps, this.answers); | ||
this.fs.write( | ||
this.destinationPath(BACKSTAGE_FILENAME), | ||
this.backstageDoc.toString(), | ||
); | ||
} | ||
} |
Oops, something went wrong.