-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci(post-release): tag the lerna packages (#1479)
- Loading branch information
Showing
3 changed files
with
169 additions
and
1 deletion.
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
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,162 @@ | ||
/** | ||
* MIT License | ||
* | ||
* Copyright (c) 2017-present, Elasticsearch BV | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
import * as process from 'node:process' | ||
import { execa } from 'execa' | ||
import * as fs from 'fs' | ||
|
||
function raiseError(msg) { | ||
console.log(msg) | ||
process.exit(1) | ||
} | ||
|
||
async function gitContext() { | ||
try { | ||
const { stdout: username } = await execa('git', ['config', 'user.name']) | ||
const { stdout: email } = await execa('git', ['config', 'user.email']) | ||
return { | ||
username, | ||
} | ||
} catch (err) { | ||
raiseError('Failed to extract git context') | ||
} | ||
} | ||
|
||
// Script logic | ||
async function main() { | ||
const isDryRun = | ||
process.env.DRY_RUN == null || process.env.DRY_RUN !== 'false' | ||
|
||
// Extract git context | ||
const ctx = await gitContext() | ||
console.log(`Git User: username=${ctx.username}, email=${ctx.email}`) | ||
|
||
if (isDryRun) { | ||
await dryRunMode() | ||
} else { | ||
await prodMode() | ||
} | ||
} | ||
|
||
async function dryRunMode() { | ||
console.log('Running in dry-run mode') | ||
|
||
} | ||
|
||
async function prodMode() { | ||
console.log('Running in prod mode') | ||
|
||
const githubToken = process.env.GITHUB_TOKEN | ||
if (githubToken == null || githubToken === '') { | ||
raiseError("The 'GITHUB_TOKEN' env var isn't defined") | ||
} | ||
|
||
try { | ||
|
||
console.log(` > List lerna versions`) | ||
|
||
await execa('npx', | ||
['lerna', 'list', '-l', '--json'], | ||
{ stdin: process.stdin } | ||
) | ||
.pipeStdout('lerna-list.txt') | ||
.pipeStderr(process.stderr) | ||
} catch (err) { | ||
raiseError('Failed to list the current lerna versions') | ||
} | ||
|
||
// Use lerna list to know the current versions to tag | ||
// all the components, if needed, using the sha commit for the | ||
// module @elastic/apm-rum-core | ||
try { | ||
// Read the lerna list output and generate the data structure | ||
// name@version | ||
let rawData = fs.readFileSync('lerna-list.txt', 'utf8') | ||
let jsonData = JSON.parse(rawData) | ||
let core | ||
var ids = [] | ||
jsonData.forEach(element => { | ||
if (element.name != '@elastic/apm-rum-core') { | ||
ids.push(`${element.name}@${element.version}`) | ||
} else { | ||
core = `${element.name}@${element.version}` | ||
} | ||
}) | ||
|
||
console.log(` >> Get sha commit for ${core}`) | ||
// Get the sha commit for @elastic/apm-rum-core, if something bad then this is a genuine error. | ||
const {stdout} = await execa('git', ['rev-list', '-n', '1', `${core}`], { stdin: process.stdin }) | ||
.pipeStderr(process.stderr) | ||
console.log(` >> ${core} = ${stdout}`) | ||
|
||
// Checkout the commit, if something bad then this is a genuine error. | ||
await execa('git', ['checkout', `${stdout}`], { stdin: process.stdin }) | ||
.pipeStdout(process.stdout) | ||
.pipeStderr(process.stderr) | ||
|
||
ids.forEach(tag => { | ||
// if it's already a tag then say that! | ||
try { | ||
execa('git', ['rev-parse', `${tag}`], { stdin: process.stdin }) | ||
.pipeStdout(process.stdout) | ||
.pipeStderr(process.stderr) | ||
console.log(` >> ${tag} already exists. There is no need to tag it`) | ||
} catch (err) { | ||
console.log(` >> ${tag} does not exist.`) | ||
// otherwise retag, if something bad then this is a genuine error. | ||
execa('git', ['tag', `${tag}`], { stdin: process.stdin }) | ||
.pipeStdout(process.stdout) | ||
.pipeStderr(process.stderr) | ||
} | ||
}) | ||
|
||
// and push the tags, if something bad then this is a genuine error. | ||
await execa('git', ['push', '--tags'], { stdin: process.stdin }) | ||
.pipeStdout(process.stdout) | ||
.pipeStderr(process.stderr) | ||
} catch (err) { | ||
console.log(`${err}`) | ||
raiseError('Failed to tag all the versions.') | ||
} | ||
|
||
// and push the tags, if something bad then this is a genuine error. | ||
try { | ||
await execa('git', ['push', '--tags'], { stdin: process.stdin }) | ||
.pipeStdout(process.stdout) | ||
.pipeStderr(process.stderr) | ||
} catch (err) { | ||
raiseError('Failed to push the tags. You might need to retag it manually.') | ||
} | ||
} | ||
|
||
// Entrypoint | ||
;(async () => { | ||
try { | ||
await main() | ||
} catch (err) { | ||
raiseError(err) | ||
} | ||
})() |