Skip to content

Commit

Permalink
feat(github-actions-release): properly tag to latest (#2472)
Browse files Browse the repository at this point in the history
## Proposed change

Properly tag to `latest` the actual latest version

<!--
Please include a summary of the changes and the related issue.
Please also include relevant motivation and context.
-->

## Related issues

<!--
Please make sure to follow the [contribution
guidelines](https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md)
-->

*- No issue associated -*

<!-- * 🐛 Fix #issue -->
<!-- * 🐛 Fix resolves #issue -->
<!-- * 🚀 Feature #issue -->
<!-- * 🚀 Feature resolves #issue -->
<!-- * :octocat: Pull Request #issue -->
  • Loading branch information
kpanot authored Nov 18, 2024
2 parents 6ef4b71 + dc481e4 commit 72e7556
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 36 deletions.
8 changes: 6 additions & 2 deletions tools/github-actions/release/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ runs:
- name: Previous release tag
if: ${{(!contains( inputs.version, '-' )) && endsWith( inputs.version, '.0' )}}
shell: bash
run: echo "PREVIOUS_VERSION_TAG=$(gh release list --exclude-drafts --exclude-pre-releases --limit ${{ inputs.previousVersionsLimit }} --json tagName --template '[{{range .}}"{{.tagName}}",{{end}}""]{{"\n"}}' | node '${GITHUB_ACTION_PATH}/packaged-action/index.cjs' ${{ inputs.version }})" >> $GITHUB_ENV
run: echo "PREVIOUS_VERSION_TAG=$(gh release list --exclude-drafts --exclude-pre-releases --limit ${{ inputs.previousVersionsLimit }} --json tagName --template '[{{range .}}"{{.tagName}}",{{end}}""]{{"\n"}}' | node '${GITHUB_ACTION_PATH}/packaged-action/index.cjs' previous-version ${{ inputs.version }})" >> $GITHUB_ENV
- name: Determine if latest tag
if: ${{(!contains( inputs.version, '-' ))}}
shell: bash
run: echo "IS_LATEST_TAG=$(gh release list --exclude-drafts --exclude-pre-releases --limit ${{ inputs.previousVersionsLimit }} --json tagName --template '[{{range .}}"{{.tagName}}",{{end}}""]{{"\n"}}' | node '${GITHUB_ACTION_PATH}/packaged-action/index.cjs' is-latest ${{ inputs.version }})" >> $GITHUB_ENV
- name: Create release
shell: bash
run: gh release create v${{ inputs.version }} --generate-notes ${{ contains( inputs.version, '-' ) && '--prerelease' || '' }} --target ${{ inputs.target }} ${{ env.PREVIOUS_VERSION_TAG != '' && format('--notes-start-tag {0}', env.PREVIOUS_VERSION_TAG) || '' }}
run: gh release create v${{ inputs.version }} --generate-notes ${{ contains( inputs.version, '-' ) && '--prerelease' || '' }} --target ${{ inputs.target }} --latest=${{env.IS_LATEST_TAG != '' && env.IS_LATEST_TAG || 'false'}} ${{ env.PREVIOUS_VERSION_TAG != '' && format('--notes-start-tag {0}', env.PREVIOUS_VERSION_TAG) || '' }}
2 changes: 1 addition & 1 deletion tools/github-actions/release/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"name": "release-gh-action",
"description": "Git release action provided by Otter",
"main": "tmp/src/get-previous-version.cjs",
"main": "tmp/src/main.cjs",
"scripts": {
"nx": "nx",
"build": "tsc -b tsconfig.build.json",
Expand Down
96 changes: 73 additions & 23 deletions tools/github-actions/release/packaged-action/index.cjs

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

2 changes: 1 addition & 1 deletion tools/github-actions/release/packaged-action/index.cjs.map

Large diffs are not rendered by default.

12 changes: 3 additions & 9 deletions tools/github-actions/release/src/get-previous-version.cts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#!/usr/bin/env node
import { valid, gt, lt } from 'semver';
import * as minimist from 'minimist';

const argv = minimist(process.argv.slice(2));
const version = valid(argv._.at(0));
export async function getPreviousVersion(versionInput?: string) {
const version = valid(versionInput);
let data = '';

let data = '';

async function main() {
for await (const chunk of process.stdin) data += chunk;

if (!version) {
Expand All @@ -28,5 +24,3 @@ async function main() {

process.stdout.write(previousVersion?.tag || '');
}

void main();
21 changes: 21 additions & 0 deletions tools/github-actions/release/src/is-latest.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { valid, lt } from 'semver';

export async function isLatest(versionInput?: string) {
const newVersion = valid(versionInput);
let data = '';

for await (const chunk of process.stdin) data += chunk;

if (!newVersion) {
console.error('Invalid version provided');
process.exit(1);
}

const isLatest = (JSON.parse(data) as string[])
.filter((tag) => !!tag)
.map((tag) => ({ tag, version: valid(tag) }))
.filter((item): item is { tag: string, version: string } => !!item.version)
.every(({ version }) => lt(version, newVersion));

process.stdout.write(isLatest ? 'true' : 'false');
}
15 changes: 15 additions & 0 deletions tools/github-actions/release/src/main.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env node
import * as minimist from 'minimist';
import { getPreviousVersion } from './get-previous-version.cjs';
import { isLatest } from './is-latest.cjs';

const argv = minimist(process.argv.slice(2));
const cmd = argv._.at(0);

if (cmd === 'previous-version') {
void getPreviousVersion(argv._.at(1));
} else if (cmd === 'is-latest') {
void isLatest(argv._.at(1));
} else {
throw new Error(`Unknown command ${cmd}`)
}

0 comments on commit 72e7556

Please sign in to comment.