Skip to content

Commit

Permalink
fix!: pushing changes to github
Browse files Browse the repository at this point in the history
  • Loading branch information
juliendargelos committed Dec 20, 2021
1 parent af4b09b commit 7d38383
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 46 deletions.
14 changes: 7 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ inputs:
description: Enable or disable publishing to package registries. When set to false, releases are only created on GitHub.
default: true
required: false
commit:
description: Enable or disable commiting and pushing changes made by the action.
push:
description: Enable or disable pushing changes made by the action.
default: true
required: false
commit_name:
name:
description: User name to use when pushing to GitHub.
default: github-actions[bot]
required: false
commit_email:
email:
description: Email to use when pushing to GitHub.
default: github-actions[bot]@users.noreply.github.com
required: false
commit_message:
description: Commit message to use when pushing to GitHub.
default: 'ci: release'
message:
description: Commit message to use when bumping package.json.
default: 'ci(release): %s'
required: false
runs:
using: node12
Expand Down
33 changes: 17 additions & 16 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6947,7 +6947,7 @@ function wrappy (fn, cb) {

/***/ }),

/***/ 648:
/***/ 986:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

"use strict";
Expand All @@ -6966,18 +6966,18 @@ var external_fs_ = __webpack_require__(747);
var external_path_ = __webpack_require__(622);
var external_path_default = /*#__PURE__*/__webpack_require__.n(external_path_);

// CONCATENATED MODULE: ./src/config.ts
// CONCATENATED MODULE: ./src/npm-config.ts



const npmrc = external_path_default().resolve(process.env['RUNNER_TEMP'] || process.cwd(), '.npmrc');
async function npmConfigRegistry(registryUrl, token) {
async function npmConfig(registryUrl, token, message) {
if (!registryUrl.endsWith('/')) {
registryUrl += '/';
}
core.info(`Setup NPM registry URL: ${registryUrl} on ${npmrc}`);
registryUrl = registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}';
await external_fs_.promises.writeFile(npmrc, registryUrl, 'utf-8');
await external_fs_.promises.writeFile(npmrc, (`${registryUrl.replace(/(^\w+:|^)/, '')}:_authToken=\${NODE_AUTH_TOKEN}\n` +
`message=${message}\n`), 'utf-8');
return {
NPM_CONFIG_USERCONFIG: npmrc,
NODE_AUTH_TOKEN: token
Expand All @@ -6998,8 +6998,9 @@ async function npmConfigRegistry(registryUrl, token) {
const githubToken = core.getInput('github_token', { required: true });
const publish = core.getInput('publish') !== 'false';
const push = core.getInput('push') !== 'false';
const gitUserName = core.getInput('git_user_name');
const gitUserEmail = core.getInput('git_user_email');
const name = core.getInput('name');
const email = core.getInput('email');
const message = core.getInput('message');
let publishToGithub;
let publishToNPM;
let privatePackage;
Expand Down Expand Up @@ -7057,8 +7058,10 @@ async function npmConfigRegistry(registryUrl, token) {
cliPath = `node_modules/${cli}/${JSON
.parse((await external_fs_.promises.readFile(`node_modules/${cli}/package.json`)).toString())
.bin[cli]}`;
await exec.exec('git', ['config', '--global', 'user.name', name]);
await exec.exec('git', ['config', '--global', 'user.email', email]);
core.info(`Creating release on GitHub${publishToGithub ? ' and publishing to GitHub registry' : ''}...`);
await release(cliPath, true, publishToGithub, {
await release(cliPath, true, publishToGithub, message, {
...process.env,
NPM_CONFIG_REGISTRY: `https://npm.pkg.github.com`,
NPM_TOKEN: githubToken,
Expand All @@ -7067,7 +7070,7 @@ async function npmConfigRegistry(registryUrl, token) {
core.info('Release available on GitHub');
publishToGithub && core.info('Package available on GitHub registry');
publishToNPM && core.info('Publishing to NPM registry...');
await release(cliPath, false, publishToNPM, {
await release(cliPath, false, publishToNPM, message, {
...process.env,
NPM_CONFIG_REGISTRY: 'https://registry.npmjs.org',
NPM_TOKEN: npmToken,
Expand All @@ -7076,8 +7079,6 @@ async function npmConfigRegistry(registryUrl, token) {
publishToNPM && core.info('Package available on NPM registry');
if (push) {
core.info('Pushing changes to GitHub repository...');
await exec.exec('git', ['config', '--global', 'user.name', gitUserName]);
await exec.exec('git', ['config', '--global', 'user.email', gitUserEmail]);
await exec.exec('git', ['push']);
core.info('GitHub repository up to date');
}
Expand All @@ -7086,7 +7087,7 @@ async function npmConfigRegistry(registryUrl, token) {
core.setFailed(error.message);
}
})();
async function lernaRelease(path, release, publish, env = {}) {
async function lernaRelease(path, release, publish, message, env = {}) {
if (release) {
await exec.exec('node', [
path,
Expand All @@ -7097,7 +7098,7 @@ async function lernaRelease(path, release, publish, env = {}) {
], { env });
}
if (publish) {
const npmEnv = await npmConfigRegistry(env.NPM_CONFIG_REGISTRY, env.NPM_TOKEN);
const npmEnv = await npmConfig(env.NPM_CONFIG_REGISTRY, env.NPM_TOKEN, message);
await exec.exec('node', [
path,
'publish',
Expand All @@ -7110,7 +7111,7 @@ async function lernaRelease(path, release, publish, env = {}) {
});
}
}
async function semanticRelease(path, release, publish, env = {}) {
async function semanticRelease(path, release, publish, message, env = {}) {
if (release) {
await exec.exec('node', [
path,
Expand All @@ -7120,7 +7121,7 @@ async function semanticRelease(path, release, publish, env = {}) {
], { env });
}
if (publish) {
const npmEnv = await npmConfigRegistry(env.NPM_CONFIG_REGISTRY, env.NPM_TOKEN);
const npmEnv = await npmConfig(env.NPM_CONFIG_REGISTRY, env.NPM_TOKEN, message);
await exec.exec('yarn', [
'publish',
'--non-interactive',
Expand Down Expand Up @@ -7331,6 +7332,6 @@ module.exports = require("zlib");;
/******/ // module exports must be returned from runtime so entry inlining is disabled
/******/ // startup
/******/ // Load entry module and return exports
/******/ return __webpack_require__(648);
/******/ return __webpack_require__(986);
/******/ })()
;
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,26 @@ jobs:

Enable or disable publishing to package registries. When set to false, only releases are created on GitHub.

### `commit`
### `push`

`boolean` *(optional, default `true`)*

Enable or disable commiting and pushing changes made by the action.
Enable or disable pushing changes made by the action.

## `commit_name`
## `name`

`string` *(optional, default: `'github-actions[bot]'`)*

User name to use when pushing to GitHub.

## `commit_email`
## `email`

`string` *(optional, default: `'github-actions[bot]@users.noreply.github.com'`)*

Email to use when pushing to GitHub.

## `commit_message`
## `message`

`string` *(optional, default: `'ci: release'`)*

Commit message to use when pushing to GitHub.
Commit message to use when bumping package.json.
Empty file removed release.commit.js
Empty file.
29 changes: 18 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as github from '@actions/github'
import { promises as fs } from 'fs'
import { npmConfigRegistry } from './config'
import { npmConfig } from './npm-config'

(async () => {
try {
Expand All @@ -12,8 +12,9 @@ import { npmConfigRegistry } from './config'
const githubToken: string = core.getInput('github_token', { required: true })
const publish: boolean = core.getInput('publish') !== 'false'
const push: boolean = core.getInput('push') !== 'false'
const gitUserName: string = core.getInput('git_user_name')
const gitUserEmail: string = core.getInput('git_user_email')
const name: string = core.getInput('name')
const email: string = core.getInput('email')
const message: string = core.getInput('message')

let publishToGithub: boolean
let publishToNPM: boolean
Expand All @@ -25,6 +26,7 @@ import { npmConfigRegistry } from './config'
path: string,
release: boolean,
publish: boolean,
message: string,
env?: { [variable: string]: string }
) => Promise<void>

Expand Down Expand Up @@ -100,11 +102,14 @@ import { npmConfigRegistry } from './config'
.bin[cli]
}`

await exec.exec('git', ['config', '--global', 'user.name', name])
await exec.exec('git', ['config', '--global', 'user.email', email])

core.info(
`Creating release on GitHub${publishToGithub ? ' and publishing to GitHub registry' : ''}...`
)

await release(cliPath, true, publishToGithub, {
await release(cliPath, true, publishToGithub, message, {
...process.env,
NPM_CONFIG_REGISTRY: `https://npm.pkg.github.com`,
NPM_TOKEN: githubToken,
Expand All @@ -123,7 +128,7 @@ import { npmConfigRegistry } from './config'
'Publishing to NPM registry...'
)

await release(cliPath, false, publishToNPM, {
await release(cliPath, false, publishToNPM, message, {
...process.env,
NPM_CONFIG_REGISTRY: 'https://registry.npmjs.org',
NPM_TOKEN: npmToken,
Expand All @@ -139,8 +144,6 @@ import { npmConfigRegistry } from './config'
'Pushing changes to GitHub repository...'
)

await exec.exec('git', ['config', '--global', 'user.name', gitUserName])
await exec.exec('git', ['config', '--global', 'user.email', gitUserEmail])
await exec.exec('git', ['push'])

core.info(
Expand All @@ -156,6 +159,7 @@ async function lernaRelease(
path: string,
release: boolean,
publish: boolean,
message: string,
env: { [variable: string]: string } = {}
): Promise<void> {
if (release) {
Expand All @@ -169,9 +173,10 @@ async function lernaRelease(
}

if (publish) {
const npmEnv = await npmConfigRegistry(
const npmEnv = await npmConfig(
env.NPM_CONFIG_REGISTRY,
env.NPM_TOKEN
env.NPM_TOKEN,
message
)

await exec.exec('node', [
Expand All @@ -191,6 +196,7 @@ async function semanticRelease(
path: string,
release: boolean,
publish: boolean,
message: string,
env: { [variable: string]: string } = {}
): Promise<void> {
if (release) {
Expand All @@ -203,9 +209,10 @@ async function semanticRelease(
}

if (publish) {
const npmEnv = await npmConfigRegistry(
const npmEnv = await npmConfig(
env.NPM_CONFIG_REGISTRY,
env.NPM_TOKEN
env.NPM_TOKEN,
message
)

await exec.exec('yarn', [
Expand Down
15 changes: 9 additions & 6 deletions src/config.ts → src/npm-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ const npmrc = path.resolve(
'.npmrc'
)

export async function npmConfigRegistry(
registryUrl: string,
token: string
export async function npmConfig(
registryUrl: string,
token: string,
message: string
): Promise<NodeJS.ProcessEnv> {
if (!registryUrl.endsWith('/')) {
registryUrl += '/'
}

core.info(`Setup NPM registry URL: ${registryUrl} on ${npmrc}`)

registryUrl = registryUrl.replace(/(^\w+:|^)/, '') + ':_authToken=${NODE_AUTH_TOKEN}'
await fs.writeFile(npmrc, registryUrl, 'utf-8')
await fs.writeFile(npmrc, (
`${registryUrl.replace(/(^\w+:|^)/, '')}:_authToken=\${NODE_AUTH_TOKEN}\n` +
`message=${message}\n`
), 'utf-8')

return {
NPM_CONFIG_USERCONFIG: npmrc,
NODE_AUTH_TOKEN: token
}
}
}

0 comments on commit 7d38383

Please sign in to comment.