Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(setup): add github actions as CI option #502

Merged
merged 23 commits into from
Dec 20, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add manual prepare config generation
  • Loading branch information
jeetiss committed Dec 7, 2019
commit 2be0e05068863e381735bcb8cd833b313fd59a53
142 changes: 107 additions & 35 deletions packages/shipjs/src/step/setup/CI/addGithubActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,68 @@ import mkdirp from 'mkdirp';
import { print } from '../../../util';
import { info, warning } from '../../../color';

// manualPrepare
export default ({ releaseBranch, dir, dryRun }) =>
export default ({ releaseBranch, manualPrepare, dir, dryRun }) =>
runStep(
{
title: 'Creating GitHub Actions configuration',
},
() => {
const filePath = path.resolve(
dir,
'.github/workflows',
'shipjs-trigger.yml'
);
const gitUserName = getGitConfig('user.name') || 'Your Name';
const gitUserEmail = getGitConfig('user.email') || '[email protected]';

if (fs.existsSync(filePath)) {
return () => {
print(
`${warning(
'✘'
)} \`.github/workflows/shipjs-trigger.yml\` already exists.`
);
print(' You can manually configure GitHub Actions');
};
}
const content = getBaseConfig({
releaseBranch,
gitUserName: getGitConfig('user.name') || 'Your Name',
gitUserEmail: getGitConfig('user.email') || '[email protected]',
});
if (dryRun) {
print(`shipjs-trigger.yml`);
print(content);
} else {
mkdirp.sync(path.dirname(filePath));
fs.writeFileSync(filePath, content);
}
return () => {
print(`${info('✔')} Created \`.github/workflows/shipjs-trigger.yml\`.`);
print(' You still need to finish setting up at GitHub Actions.');
};
const log = [
createGithubAction({
jeetiss marked this conversation as resolved.
Show resolved Hide resolved
content: getBaseConfig({ releaseBranch }),
actionPath: '.github/workflows/shipjs-trigger.yml',
dir,
dryRun,
}),
manualPrepare &&
createGithubAction({
content: getManualPrepareConfig({
releaseBranch,
dir,
dryRun,
gitUserName,
gitUserEmail,
}),
actionPath: '.github/workflows/shipjs-manual-prepare.yml',
dir,
dryRun,
}),

() => {
print(' You still need to finish setting up at GitHub Actions.');
// add link to readme here
jeetiss marked this conversation as resolved.
Show resolved Hide resolved
},
].filter(Boolean);

return () => log.forEach(printResult => printResult());
}
);

function getBaseConfig({ releaseBranch, gitUserName, gitUserEmail }) {
function createGithubAction({ content, actionPath, dir, dryRun }) {
const filePath = path.resolve(dir, actionPath);

if (fs.existsSync(filePath)) {
return () => {
print(`${warning('✘')} \`${actionPath}\` already exists.`);
};
}

if (dryRun) {
print(actionPath);
print(content);
} else {
mkdirp.sync(path.dirname(filePath));
fs.writeFileSync(filePath, content);
}
return () => {
print(`${info('✔')} Created \`${actionPath}\`.`);
};
}

function getBaseConfig({ releaseBranch }) {
return ejs.render(
`
name: Ship js trigger
Expand All @@ -74,6 +93,59 @@ name: Ship js trigger
NODE_AUTH_TOKEN: \${{ secrets.NPM_TOKEN }}

`,
{ releaseBranch, gitUserName, gitUserEmail }
{ releaseBranch }
);
}

function getManualPrepareConfig({ gitUserName, gitUserEmail }) {
return ejs.render(
`
name: Ship js Manual Prepare
on:
issue_comment:
types: [created]
jobs:
manual_prepare:
if: |
github.event_name == 'issue_comment' &&
(github.event.comment.author_association == 'member' || github.event.comment.author_association == 'owner') &&
startsWith(github.event.comment.body, '@shipjs prepare')
runs-on: Ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@master
- run: git switch master
- run: npm install
- run: |
git config --global user.email "<%= gitUserEmail %>"
git config --global user.name "<%= gitUserName %>"
- run: npm run release:prepare -- --yes --no-browse
env:
GITHUB_TOKEN: \${{ secrets.GH_TOKEN }}

create_done_comment:
if: success()
needs: manual_prepare
runs-on: Ubuntu-latest
steps:
- uses: actions/github@master
env:
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}
with:
args: comment "@\${{ github.actor }} \`shipjs prepare\` done"

create_fail_comment:
if: cancelled() || failure()
needs: manual_prepare
runs-on: Ubuntu-latest
steps:
- uses: actions/github@master
env:
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}
with:
args: comment "@\${{ github.actor }} \`shipjs prepare\` fail"

`,
{ gitUserName, gitUserEmail }
);
}