Skip to content

Commit

Permalink
chore: release semver tags
Browse files Browse the repository at this point in the history
  • Loading branch information
corymhall committed Dec 9, 2023
1 parent 46f6cd0 commit 13b3db5
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml

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

12 changes: 12 additions & 0 deletions .projen/deps.json

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

12 changes: 10 additions & 2 deletions .projen/tasks.json

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

8 changes: 8 additions & 0 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ const project = new GitHubActionTypeScriptProject({
'@aws-sdk/credential-providers',
],
devDeps: [
'commit-and-tag-version',
'@types/conventional-changelog-config-spec',
'conventional-changelog-config-spec',
'mock-fs@^5',
'aws-sdk-client-mock',
'@types/mock-fs@^4',
Expand All @@ -99,6 +102,9 @@ jestConfig?.patch(JsonPatch.add('/transform', {
}));
const actionYml = project.tryFindObjectFile('action.yml');
actionYml?.addOverride('runs.using', 'node20');
project.tasks.addTask('gh-release', {
exec: 'ts-node projenrc/release-version.ts'
})

// setup merge queue
project.github?.tryFindWorkflow('build')?.on({
Expand All @@ -107,6 +113,8 @@ project.github?.tryFindWorkflow('build')?.on({
},
});

project.github?.tryFindWorkflow('release')?.file?.patch(JsonPatch.replace("/jobs/release_github/steps/3/run", "npx ts-node projenrc/release-version.ts"))

const autoMergeJob: github.workflows.Job = {
name: 'Set AutoMerge on PR #${{ github.event.number }}',
runsOn: ['ubuntu-latest'],
Expand Down
4 changes: 4 additions & 0 deletions package.json

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

86 changes: 86 additions & 0 deletions projenrc/release-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { promises as fs } from 'fs';
import { join } from 'path';
import * as logging from 'projen/lib/logging';
import { exec } from 'projen/lib/util';

export interface BumpOptions {
/**
* The name of the file which will include the release tag (a text file).
*
* Relative to cwd.
*
* @default 'dist/releasetag.txt'
*/
readonly releaseTagFile?: string;

/**
* The github ref (branch or tag) that triggered the release.
*/
readonly githubRef: string;

/**
* The github repository (e.g. "owner/repo").
*/
readonly githubRepo: string;

readonly dryRun: boolean;
}

/**
*
*
* @param cwd working directory (git repository)
* @param options options
*/
export async function release(cwd: string, options: BumpOptions) {
const releaseTagFile = join(cwd, options.releaseTagFile ?? 'dist/releasetag.txt');

const tagVersion = (await fs.readFile(releaseTagFile, 'utf-8')).trim();

logging.info(
`${releaseTagFile} has resolved version: ${tagVersion}`,
);

const [majorVersion, minorVersion] = tagVersion.split('.');
const cmds = [
`gh release create ${tagVersion} -R ${options.githubRepo} -F dist/changelog.md -t ${tagVersion} --target ${options.githubRef}`,
`git tag ${majorVersion} --force`,
`git tag ${majorVersion}.${minorVersion} --force`,
'git push origin --tags',
];
if (options.dryRun) {
cmds.forEach(cmd => {
logging.info(cmd);
});
} else {
cmds.forEach(cmd => {
exec(cmd, { cwd });
});
}
}

const releaseTagFile = process.env.RELEASETAG;
const githubRepo = process.env.GITHUB_REPOSITORY;
const githubRef = process.env.GITHUB_REF;
const dryRun = process.env.RELEASE_DRY_RUN;

if (!githubRepo) {
throw new Error('GITHUB_REPOSITORY is required');
}

if (!githubRef) {
throw new Error('GITHUB_REF is required');
}

const opts: BumpOptions = {
dryRun: dryRun != '',
releaseTagFile,
githubRef,
githubRepo,
};
logging.debug(opts);

release(process.cwd(), opts).catch((e: Error) => {
console.log(e.stack);
process.exit(1);
});
Loading

0 comments on commit 13b3db5

Please sign in to comment.