Skip to content

Commit

Permalink
feat: Add release script
Browse files Browse the repository at this point in the history
  • Loading branch information
aggagen committed Nov 16, 2023
1 parent 4abab1c commit ab91cb2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
6 changes: 5 additions & 1 deletion packages/blueprints/blueprint-builder/src/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ export class Blueprint extends ParentBlueprint {

// copy-paste additional code over it
StaticAsset.findAll().forEach(asset => {
if (asset.path() === 'release.sh') {
return;
}

new File(repository, asset.path(), asset.content());
});

Expand Down Expand Up @@ -231,7 +235,7 @@ export class Blueprint extends ParentBlueprint {
*/
if (this.context.environmentId == 'default' || options.advancedSettings.releaseWorkflow) {
const releaseWorkflow = new WorkflowBuilder(this);
new Workflow(this, repository, buildReleaseWorkflow(releaseWorkflow).getDefinition());
new Workflow(this, repository, buildReleaseWorkflow(releaseWorkflow, repository).getDefinition());
}
}

Expand Down
53 changes: 14 additions & 39 deletions packages/blueprints/blueprint-builder/src/build-release-workflow.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { SourceFile, SourceRepository, SubstitionAsset } from '@amazon-codecatalyst/blueprint-component.source-repositories';
import { TriggerType, WorkflowBuilder } from '@amazon-codecatalyst/blueprint-component.workflows';

export function buildReleaseWorkflow(workflow: WorkflowBuilder): WorkflowBuilder {
export function buildReleaseWorkflow(workflow: WorkflowBuilder, repository: SourceRepository): WorkflowBuilder {
workflow.setName('blueprint-release');
const RELEASE_COMMIT_PREFIX = 'chore(release):';
const BUILD_ARTIFACT_NAME = 'codebase';

const releaseScript = new SubstitionAsset('release.sh');
new SourceFile(repository, 'release.sh', releaseScript.substitute({ commitPrefix: RELEASE_COMMIT_PREFIX }));

workflow.addBranchTrigger(['main']);
workflow.addTrigger({
Type: TriggerType.MANUAL,
Expand All @@ -26,7 +30,7 @@ export function buildReleaseWorkflow(workflow: WorkflowBuilder): WorkflowBuilder
],
});
workflow.addBuildAction({
actionName: 'build_blueprint',
actionName: 'build_and_commit',
dependsOn: ['check_commit'],
input: {
Sources: ['WorkflowSource'],
Expand All @@ -42,48 +46,19 @@ export function buildReleaseWorkflow(workflow: WorkflowBuilder): WorkflowBuilder
},
],
},
steps: [
"if $IS_RELEASE_COMMIT; then echo 'This is a release commit, skipping' && exit 1; fi",
'yum install -y rsync',
'npm install -g yarn',
'yarn',
'yarn build',
'yarn bump',
'yarn blueprint:package',
],
steps: ["if $IS_RELEASE_COMMIT; then echo 'This is a release commit, skipping'; else chmod +x release.sh && ./release.sh; fi"],
});
// TODO: Build actions can't push back to source yet:
// 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.addPublishBlueprintAction({
actionName: 'publish_blueprint',
dependsOn: ['build_blueprint'],
dependsOn: ['build_and_commit'],
inputs: {
Sources: ['WorkflowSource'],
Artifacts: [BUILD_ARTIFACT_NAME],
// TODO: The action doesn't handle this env var correctly:
// Variables: [
// {
// Name: 'IS_RELEASE_COMMIT',
// Value: '${check_commit.IS_RELEASE_COMMIT}',
// },
// ],
Variables: [
{
Name: 'IS_RELEASE_COMMIT',
Value: '${check_commit.IS_RELEASE_COMMIT}',
},
],
},
configuration: {
ArtifactPackagePath: 'dist/js/*.tgz',
Expand Down
35 changes: 35 additions & 0 deletions packages/blueprints/blueprint-builder/static-assets/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This script is used by the blueprint-release workflow to build
# the blueprint, bump its package version, and commit the version
# bump back into the repository.
echo "Installing dependencies..."
yum install -y rsync
npm install -g yarn
yarn

echo "Building blueprint..."
yarn build

echo "Bumping package version..."
yarn bump
NEW_VERSION=`jq -r .version package.json`
yarn blueprint:package

echo "Getting credentials..."
MI=`curl $AWS_CONTAINER_TOKEN_ENDPOINT`
ACCESS_KEY_ID=$(echo "$MI" | jq -r '.AccessKeyId')
SECRET_ACCESS_KEY=$(echo "$MI" | jq -r '.SecretAccessKey')
ORIGINAL_REMOTE=`git config --get remote.origin.url`
SOURCE_REPO_URL=`sed -e "s^//^//$ACCESS_KEY_ID:$SECRET_ACCESS_KEY@^" <<< $ORIGINAL_REMOTE`
echo "Configuring git..."
git remote set-url origin $SOURCE_REPO_URL
git config --global user.email "noreply@amazon.com"
git config --global user.name "Release Workflow"
git add .
echo "Committing changes..."
RELEASE_COMMIT_MESSAGE="{{commitPrefix}} release $NEW_VERSION"
git commit -m "$RELEASE_COMMIT_MESSAGE"
echo "Pushing to origin..."
git push origin HEAD:main

0 comments on commit ab91cb2

Please sign in to comment.