Skip to content

Commit

Permalink
feat: inital ci-cd workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
alexforsyth committed Nov 9, 2023
1 parent 3a53c0c commit f74b4f5
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/blueprints/blueprint-builder/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions packages/blueprints/blueprint-builder/src/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import devEnvPackage from '@amazon-codecatalyst/blueprint-component.dev-environm
import envPackage from '@amazon-codecatalyst/blueprint-component.environments/package.json';
import { SourceRepository, SourceFile, StaticAsset, File } from '@amazon-codecatalyst/blueprint-component.source-repositories';
import sourceReposPackage from '@amazon-codecatalyst/blueprint-component.source-repositories/package.json';
import { Workflow, WorkflowBuilder } from '@amazon-codecatalyst/blueprint-component.workflows';
import workflowsPackage from '@amazon-codecatalyst/blueprint-component.workflows/package.json';
import cliPackage from '@amazon-codecatalyst/blueprint-util.cli/package.json';
import { ProjenBlueprint, ProjenBlueprintOptions } from '@amazon-codecatalyst/blueprint-util.projen-blueprint';
Expand All @@ -15,6 +16,7 @@ import {
} from '@amazon-codecatalyst/blueprints.blueprint';
import baseBlueprintPackage from '@amazon-codecatalyst/blueprints.blueprint/package.json';
import * as decamelize from 'decamelize';
import { buildReleaseWorkflow } from './build-release-workflow';
import defaults from './defaults.json';

devEnvPackage.version;
Expand Down Expand Up @@ -71,6 +73,12 @@ export interface Options extends ParentOptions {
* @validationMessage Must contain only upper and lowercase letters, numbers and underscores, spaces, dashes
*/
repositoryName?: string;

/**
* Generate a release workflow?
* If this is set, the blueprint will generate a release workflow. On push to main, a workflow will release this blueprint into your codecatalyst space.
*/
releaseWorkflow?: boolean;
};
}

Expand Down Expand Up @@ -226,6 +234,14 @@ export class Blueprint extends ParentBlueprint {
],
},
});

/**
* todo: remove this once the release workflow is available in production
*/
if (this.context.environmentId == 'default' || options.advancedSettings.releaseWorkflow) {
const releaseWorkflow = new WorkflowBuilder(this);
new Workflow(this, repository, buildReleaseWorkflow(releaseWorkflow).getDefinition());
}
}

synth(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { TriggerType, WorkflowBuilder } from '@amazon-codecatalyst/blueprint-component.workflows';

export function buildReleaseWorkflow(workflow: WorkflowBuilder): WorkflowBuilder {

workflow.setName('blueprint-release');
const RELEASE_COMMIT_PREFIX = 'chore(release):';

workflow.addBranchTrigger(['main']),
workflow.addTrigger({
Type: TriggerType.MANUAL,
});
workflow.addBuildAction({
actionName: 'check_commit',
input: {
Sources: ['WorkflowSource'],
},
output: {
Variables: [
'IS_RELEASE_COMMIT',
],
},
steps: [
'TRIGGER_COMMIT_ID=$CATALYST_EVENT_SHA',
'COMMIT_MESSAGE="$(git log -n 1 $TRIGGER_COMMIT_ID --oneline)"',
`RELEASE_PREFIX='${RELEASE_COMMIT_PREFIX}'`,
'IS_RELEASE_COMMIT=false',
'if grep -q "$RELEASE_PREFIX" <<< "$COMMIT_MESSAGE"; then echo \'this is a release commit\' && IS_RELEASE_COMMIT=true; fi',
],
}),

workflow.addBuildAction({
actionName: 'build_blueprint',
dependsOn: ['check_commit'],
input: {
Sources: ['WorkflowSource'],
Variables: {
IS_RELEASE_COMMIT: '${check_commit.IS_RELEASE_COMMIT}',
},
},
output: {},
steps: [
'if $IS_RELEASE_COMMIT; then echo \'This is a release commit, skipping\' && exit 1; fi',
'yarn',
'yarn build',
'yarn bump',
'yarn blueprint:package',
],
}),

workflow.addBuildAction({
actionName: 'commit_changes',
dependsOn: ['build_blueprint'],
input: {
Sources: ['WorkflowSource'],
Variables: {
IS_RELEASE_COMMIT: '${check_commit.IS_RELEASE_COMMIT}',
},
},
output: {},
steps: [
'if $IS_RELEASE_COMMIT; then echo \'This is a release commit, skipping\' && exit 1; fi',
`RELEASE_COMMIT_MESSAGE="${RELEASE_COMMIT_PREFIX} release on $(date +"%Y %m %d %H:%M:%S")"`,
'git add .',
'git commit -m $RELEASE_COMMIT_MESSAGE',
'git push --force',
],
}),

workflow.setDefinition({
...workflow.getDefinition(),
Actions: {
...workflow.definition.Actions,
publish_blueprint: {
Identifier: 'aws/publish-blueprint-action',
dependsOn: ['commit_changes'],
Inputs: {
Sources: ['WorkflowSource'],
Variables: {
IS_RELEASE_COMMIT: '${check_commit.IS_RELEASE_COMMIT}',
},
},
Configuration: {
ArtifactPackagePath: 'dist/*.tgz',
PackageJSONPath: 'package.json',
TimeoutInSeconds: '120',
},
},
},
});
return workflow;
}
3 changes: 2 additions & 1 deletion packages/blueprints/blueprint-builder/src/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"blueprintPackageName": "",
"repositoryName": "",
"license": "Apache-2.0",
"tags": ["first-label", "second-label"]
"tags": ["first-label", "second-label"],
"releaseWorkflow": true
}
}

0 comments on commit f74b4f5

Please sign in to comment.