Skip to content

Commit

Permalink
feat: support undeployments of hashed branches
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonwellsjo committed Jan 8, 2025
1 parent 7b07ca9 commit 7cb520b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/bin/undeploy-gae.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import yargs from 'yargs'
import { undeployGae } from '../deploy/deployGae'

runScript(async () => {
const { branch } = yargs.options({
const { branch, hashedBranch } = yargs.options({
branch: {
type: 'string',
demandOption: true,
desc: `Because Github Actions delete event happens after the branch is already deleted - you need to pass it manually`,
},
hashedBranch: {
type: 'boolean',
desc: 'Set to true if `hashedBranches` flag was used on deployment',
default: false,
},
}).argv

await undeployGae(branch)
await undeployGae(branch, hashedBranch)
})
9 changes: 3 additions & 6 deletions src/deploy/deploy.util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { _assert, _mapValues, _merge, _truncate, localTime } from '@naturalcycles/js-lib'
import { dimGrey, fs2, sha256, white } from '@naturalcycles/nodejs-lib'
import { dimGrey, fs2, white } from '@naturalcycles/nodejs-lib'
import { BackendCfg } from './backend.cfg.util'
import { AppYaml, DeployInfo } from './deploy.model'
import { hashBranch } from './hashBranch.util'

const APP_YAML_DEFAULT = (): AppYaml => ({
runtime: 'nodejs22',
Expand Down Expand Up @@ -60,11 +61,7 @@ export async function createDeployInfo(
let branchName = gitBranch

if (backendCfg.hashedBranches) {
// Obfuscates the branch name by hashing it.
// If there are Jira issue names in the branch name, the first one found will be used as a prefix.
const jiraIssue = gitBranch.match(/([Dd][Ee][Vv]-\d+)/)?.[0]
const branchHash = sha256(gitBranch).slice(0, 10)
branchName = [jiraIssue, branchHash].filter(Boolean).join('-')
branchName = hashBranch(branchName)
}

gaeService = validateGAEServiceName([branchName, gaeService].join('--'))
Expand Down
6 changes: 5 additions & 1 deletion src/deploy/deployGae.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getBackendCfg } from './backend.cfg.util'
import { createDeployInfo } from './deploy.util'
import { deployHealthCheck, DeployHealthCheckOptions } from './deployHealthCheck'
import { deployPrepare, DeployPrepareOptions } from './deployPrepare'
import { hashBranch } from './hashBranch.util'

export interface DeployGaeOptions extends DeployPrepareOptions, DeployHealthCheckOptions {}

Expand Down Expand Up @@ -80,7 +81,10 @@ export async function deployGae(opt: DeployGaeOptions = {}): Promise<void> {
* Undeploys/removes the GAE service/version, using the same rules as deployGae.
* Detects the service/version by the same criteria: branch name, backend.cfg.yaml, etc.
*/
export async function undeployGae(branch: string): Promise<void> {
export async function undeployGae(branch: string, hashedBranch: boolean): Promise<void> {
if (hashedBranch) {
branch = hashBranch(branch)
}
const projectDir = '.'
const backendCfg = getBackendCfg(projectDir)

Expand Down
12 changes: 12 additions & 0 deletions src/deploy/hashBranch.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { sha256 } from '@naturalcycles/nodejs-lib'

/**
* Obfuscates the branch name by hashing it. Will keep the first jira issue number if found.
*
* @param branch Branch name to hash.
*/
export function hashBranch(branch: string): string {
const jiraIssue = branch.match(/([Dd][Ee][Vv]-\d+)/)?.[0]
const branchHash = sha256(branch).slice(0, 10)
return [jiraIssue, branchHash].filter(Boolean).join('-')
}

0 comments on commit 7cb520b

Please sign in to comment.