Skip to content

Commit

Permalink
chore: make separate step for publish
Browse files Browse the repository at this point in the history
  • Loading branch information
yeager-eren committed May 30, 2023
1 parent b769f8b commit a5d6ddc
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 126 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
dist
*.js
scripts
45 changes: 45 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Deploy
on:
workflow_dispatch:

jobs:
deploy-it:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup NodeJS
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'yarn'

- name: Git
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Restore cache
uses: actions/cache@v3
with:
path: '**/node_modules'
key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

- name: Install packages
run: yarn install --frozen-lockfile

- name: Deploy packages
run: yarn run deploy
env:
REF: ${{ github.ref }}
BASE_REF: ${{ github.event.pull_request.base.ref }}
GH_TOKEN: ${{ github.token }}
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
VERCEL_PROJECT_WALLETS: ${{ secrets.VERCEL_PROJECT_WALLETS }}
VERCEL_PROJECT_Q: ${{ secrets.VERCEL_PROJECT_Q }}
VERCEL_PROJECT_WALLET_ADAPTER: ${{ secrets.VERCEL_PROJECT_WALLET_ADAPTER }}
VERCEL_PROJECT_WIDGET_CONFIG: ${{ secrets.VERCEL_PROJECT_WIDGET_CONFIG }}
VERCEL_PROJECT_WIDGET_APP: ${{ secrets.VERCEL_PROJECT_WIDGET_APP }}
19 changes: 6 additions & 13 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ jobs:
node-version: '18'
cache: 'yarn'

- name: Restore cache
uses: actions/cache@v3
with:
path: '**/node_modules'
key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

- name: Git
run: |
git config --global user.name 'github-actions[bot]'
Expand All @@ -37,6 +31,12 @@ jobs:
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Restore cache
uses: actions/cache@v3
with:
path: '**/node_modules'
key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

- name: Install packages
run: yarn install --frozen-lockfile

Expand All @@ -46,10 +46,3 @@ jobs:
REF: ${{ github.ref }}
BASE_REF: ${{ github.event.pull_request.base.ref }}
GH_TOKEN: ${{ github.token }}
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
VERCEL_PROJECT_WALLETS: ${{ secrets.VERCEL_PROJECT_WALLETS }}
VERCEL_PROJECT_Q: ${{ secrets.VERCEL_PROJECT_Q }}
VERCEL_PROJECT_WALLET_ADAPTER: ${{ secrets.VERCEL_PROJECT_WALLET_ADAPTER }}
VERCEL_PROJECT_WIDGET_CONFIG: ${{ secrets.VERCEL_PROJECT_WIDGET_CONFIG }}
VERCEL_PROJECT_WIDGET_APP: ${{ secrets.VERCEL_PROJECT_WIDGET_APP }}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"lint": "nx run-many --target=lint",
"format": "nx run-many --target=format",
"publish": "node ./scripts/publish/command.mjs",
"deploy": "node ./scripts/deploy/command.mjs",
"release": "node ./scripts/release/command.mjs",
"upgrade-all": "node ./scripts/upgrade-all/command.mjs --project "
},
Expand Down
10 changes: 9 additions & 1 deletion scripts/common/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fileURLToPath } from 'url';
import { readFileSync } from 'fs';
import { join } from 'path';
import { execa } from 'execa';
import process from 'node:process';

const root = join(printDirname(), '..', '..');

Expand Down Expand Up @@ -36,7 +37,9 @@ export function pacakgeJson(location) {

export async function packageNamesToPackagesWithInfo(names) {
const allPackages = await workspacePackages();
return names.map((pkgName) => allPackages.find((pkg) => pkg.name === pkgName));
return names.map((pkgName) =>
allPackages.find((pkg) => pkg.name === pkgName)
);
}

/*
Expand All @@ -46,3 +49,8 @@ export async function packageNamesToPackagesWithInfo(names) {
export function packageNameWithoutScope(name) {
return name.replace(/@.+\//, '');
}

// we are adding a fallback, to make sure predefiend VERCEL_PACKAGES always will be true.
export function getEnvWithFallback(name) {
return process.env[name] || 'NOT SET';
}
35 changes: 35 additions & 0 deletions scripts/deploy/command.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env node
'use strict';
import process from 'node:process';
import { workspacePackages } from '../common/utils.mjs';
import { buildPackages, logAsSection } from '../publish/utils.mjs';
import { deployProjectsToVercel } from './utils.mjs';

// TODO: Working directory should be empty.
async function run() {
// Detect last relase and what packages has changed since then.
const packages = await workspacePackages();
const privatePackages = packages.filter((pkg) => {
return pkg.private;
});

await buildPackages(privatePackages).catch((e) => {
console.log(
'[-] BUILD FAILED. Ignore it to workflow run the rest of tasks.'
);
console.log(e);
});
logAsSection('[x] Build for VERCEL');
await deployProjectsToVercel(privatePackages).catch((e) => {
console.log(
'[-] DEPLOY FAILED. Ignore it to workflow run the rest of tasks.'
);
console.log(e);
});
logAsSection('[x] Deploy to VERCEL');
}

run().catch((e) => {
console.error(e);
process.exit(1);
});
6 changes: 3 additions & 3 deletions scripts/publish/config.mjs → scripts/deploy/config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getEnvWithFallback } from './utils.mjs';
import { getEnvWithFallback } from '../common/utils.mjs';
import process from 'node:process';

const scope = `@rango-dev`;
export const VERCEL_ORG_ID = process.env.VERCEL_ORG_ID;
Expand All @@ -7,6 +8,5 @@ export const VERCEL_PACKAGES = {
[`${scope}/wallets-demo`]: getEnvWithFallback('VERCEL_PROJECT_WALLETS'),
[`${scope}/queue-manager-demo`]: getEnvWithFallback('VERCEL_PROJECT_Q'),
[`${scope}/wallets-adapter-demo`]: getEnvWithFallback('VERCEL_PROJECT_WALLET_ADAPTER'),
[`${scope}/widget-playground`]: getEnvWithFallback('VERCEL_PROJECT_WIDGET_CONFIG'),
[`${scope}/widget-app`]: getEnvWithFallback('VERCEL_PROJECT_WIDGET_APP'),
[`${scope}/config-client`]: getEnvWithFallback('VERCEL_PROJECT_WIDGET_CONFIG'),
};
60 changes: 60 additions & 0 deletions scripts/deploy/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { execa } from 'execa';
import { detectChannel } from '../publish/utils.mjs';
import { VERCEL_ORG_ID, VERCEL_PACKAGES, VERCEL_TOKEN } from './config.mjs';

export function getVercelProjectId(packageName) {
return VERCEL_PACKAGES[packageName];
}

export async function deployProjectsToVercel(pkgs) {
await Promise.all(pkgs.map((pkg) => deploySingleProjectToVercel(pkg)));
}
export async function deploySingleProjectToVercel(pkg) {
const deployTo = detectChannel() === 'prod' ? 'production' : 'preview';

const env = {
VERCEL_ORG_ID: VERCEL_ORG_ID,
VERCEL_PROJECT_ID: getVercelProjectId(pkg.name),
};

console.log(`start deplyoing ${pkg.name}...`);

await execa(
'yarn',
[
'vercel',
'pull',
'--cwd',
pkg.location,
'--environment',
deployTo,
'--token',
VERCEL_TOKEN,
'--yes',
],
{ env },
);
await execa('yarn', ['vercel', 'build', '--cwd', pkg.location, '--token', VERCEL_TOKEN], { env });
await execa('yarn', ['vercel', pkg.location, '--prebuilt', '--token', VERCEL_TOKEN], { env });

console.log(`${pkg.name} deployed.`);
}

export function groupPackagesForDeploy(packages) {
const output = {
npm: [],
vercel: [],
};

packages.forEach((pkg) => {
if (!!getVercelProjectId(pkg.name)) {
output.vercel.push(pkg);
} else if (!pkg.private) {
// If getVercelProjectId returns undefined, it's possible to be added as npm package
// So here we are making sure it's not a private package and can be published using npm
output.npm.push(pkg);
}
});

return output;
}
54 changes: 25 additions & 29 deletions scripts/publish/command.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/usr/bin/env node
'use strict';
import process from 'node:process';

import {
buildPackages,
changed,
deployProjectsToVercel,
detectChannel,
exportNx,
generateChangelog,
getLastReleasedHashId,
groupPackagesForDeploy,
increaseVersionForMain,
increaseVersionForNext,
logAsSection,
Expand All @@ -23,6 +22,7 @@ import { nxToGraph } from '../common/graph/helpers.mjs';
import { execa, $ } from 'execa';
import { performance } from 'node:perf_hooks';
import { packageNamesToPackagesWithInfo } from '../common/utils.mjs';
import { groupPackagesForDeploy } from '../deploy/utils.mjs';

// TODO: Working directory should be empty.
async function run() {
Expand All @@ -36,7 +36,9 @@ async function run() {
logAsSection('Run...', `at ${baseCommit}`);

console.log(
changedPkgs.map((pkg) => `- ${pkg.name} (current version: ${pkg.version})`).join('\n'),
changedPkgs
.map((pkg) => `- ${pkg.name} (current version: ${pkg.version})`)
.join('\n')
);

// If any package has changed, we exit from the process.
Expand All @@ -52,15 +54,19 @@ async function run() {
nxToGraph(nxGraph, graph);
graph.onlyAffected(changedPkgs.map((pkg) => pkg.name));
const sortedList = graph.sort();
const sortedPackagesToPublish = await packageNamesToPackagesWithInfo([...sortedList]);
const sortedPackagesToPublish = await packageNamesToPackagesWithInfo([
...sortedList,
]);
performance.mark('end-analyze');

console.log(
`Execution time: ${performance.measure('analyze', 'start-analyze', 'end-analyze').duration}ms`,
`Execution time: ${
performance.measure('analyze', 'start-analyze', 'end-analyze').duration
}ms`
);
console.log(
'Packages will be published, in this order:\n',
sortedPackagesToPublish.map((pkg) => pkg.name).join(' -> '),
sortedPackagesToPublish.map((pkg) => pkg.name).join(' -> ')
);
console.log('::endgroup::');

Expand All @@ -75,7 +81,8 @@ async function run() {
// Git tag & commit
console.log(`::group::Tag, commit and push to repository.`);
logAsSection(`Git Tagging..`);
const tagOptions = channel === 'prod' ? { skipGitTagging: false } : { skipGitTagging: true };
const tagOptions =
channel === 'prod' ? { skipGitTagging: false } : { skipGitTagging: true };
await tagPackagesAndCommit(updatedPackages, tagOptions);

logAsSection(`Pushing tags to remote...`);
Expand Down Expand Up @@ -108,13 +115,17 @@ async function publish(changedPkg, channel) {

console.log(
updatedPackages
.map((pkg) => `[x] Versioninig: ${pkg.name} (next version: ${pkg.version})`)
.join('\n'),
.map(
(pkg) => `[x] Versioninig: ${pkg.name} (next version: ${pkg.version})`
)
.join('\n')
);

// 2. Changelog & Github Release
if (channel === 'prod') {
await Promise.all(updatedPackages.map((pkg) => generateChangelog(pkg, { saveToFile: true })));
await Promise.all(
updatedPackages.map((pkg) => generateChangelog(pkg, { saveToFile: true }))
);

await Promise.all(updatedPackages.map(makeGithubRelease));
logAsSection(`[x] Github Release & Changelog generated.`);
Expand All @@ -129,7 +140,7 @@ async function publish(changedPkg, channel) {
const duration_build = performance.measure(
`publish-build-${changedPkg.name}`,
`start-publish-build-${changedPkg.name}`,
`end-publish-build-${changedPkg.name}`,
`end-publish-build-${changedPkg.name}`
).duration;
logAsSection(`[x] Build for NPM ${duration_build}ms`);
performance.mark(`start-publish-npm-${changedPkg.name}`);
Expand All @@ -139,27 +150,12 @@ async function publish(changedPkg, channel) {
const duration_npm = performance.measure(
`publish-npm-${changedPkg.name}`,
`start-publish-npm-${changedPkg.name}`,
`end-publish-npm-${changedPkg.name}`,
`end-publish-npm-${changedPkg.name}`
).duration;
logAsSection(`[x] Publish on NPM ${duration_npm}ms`);
}

// 4. Publish to Vercel
if (packages.vercel.length) {
// TODO: This is not a good solution, because it will build the package itself twice.
await buildPackages(packages.vercel).catch((e) => {
console.log('[-] BUILD FAILED. Ignore it to workflow run the rest of tasks.');
console.log(e);
});
logAsSection('[x] Build for VERCEL');
await deployProjectsToVercel(packages.vercel).catch((e) => {
console.log('[-] DEPLOY FAILED. Ignore it to workflow run the rest of tasks.');
console.log(e);
});
logAsSection('[x] Deploy to VERCEL');
}

// 6. Yarn upgrade-all
// 5. Yarn upgrade-all
const { stdout: upgradeStdOut } = await execa('yarn', [
'upgrade-all',
changedPkg.name,
Expand All @@ -173,7 +169,7 @@ async function publish(changedPkg, channel) {
const duration_publish = performance.measure(
`publish-${changedPkg.name}`,
`start-publish-${changedPkg.name}`,
`end-publish-${changedPkg.name}`,
`end-publish-${changedPkg.name}`
).duration;
console.log(`Execution time: ${duration_publish}ms`);
console.log(`::endgroup::`);
Expand Down
Loading

0 comments on commit a5d6ddc

Please sign in to comment.