Skip to content

Commit

Permalink
feat: extra-info action refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehonal committed Mar 5, 2022
1 parent 90156e0 commit 35b20dc
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 88 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true

# Matches multiple files with brace expansion notation
[*.{js,py,jsx,ts,tsx,json}]
charset = utf-8
indent_style = space
indent_size = 2

# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ jobs:
runs-on: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
- name: Test prepare action
- name: Test extra-info action
with:
npm_version: ${{ env.NPM_VERSION }}
node_version: ${{ env.NODE_VERSION }}
uses: ./prepare
uses: ./extra-info
test-remote-usage:
runs-on: [ubuntu-latest]
steps:
- name: Test prepare action
- name: Test extra-info action
with:
npm_version: ${{ env.NPM_VERSION }}
node_version: ${{ env.NODE_VERSION }}
package_json_path: ../package.json
uses: Drassil/gh-actions-collection/prepare@master
uses: Drassil/gh-actions-collection/extra-info@master
8 changes: 5 additions & 3 deletions prepare/action.yml → extra-info/action.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
name: 'action-prepare'
description: 'Reusable actions for the frontend workflows'
name: 'action-extra-info'
description: 'It injects in your pipeline extra info that can be used by your workflows'
outputs:
github_branch:
description: 'Github branch name'
package_version:
description: 'The package version of the repository. PACKAGE_VERSION env variable is also exported.'
inputs:
node_version:
description: 'node version used for the node commands'
required: false
default: '16'
npm_version:
description: 'npm version used for the npm commands'
required: false
required: falses
default: '8.5'
package_json_path:
description: 'Path of the package.json to retrieve the package version'
Expand Down
29 changes: 29 additions & 0 deletions extra-info/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const prepare = require('../src/prepare')

const GITHUB_WORKSPACE = process.env.GITHUB_WORKSPACE;

async function run() {
await prepare();

// now we can require the action modules
const { getBranchName, setFailed } = require(`../src/utils`);
const core = require('@actions/core');

try {
const packageJsonPath = core.getInput('package_json_path') || `${GITHUB_WORKSPACE}/package.json`;

const packageJson = require(packageJsonPath);

const branch = getBranchName();

core.setOutput('github_branch', branch);

core.setOutput('package_version', packageJson.version);
core.exportVariable('PACKAGE_VERSION', packageJson.version);

} catch (error) {
setFailed(error);
}
}

run();
3 changes: 1 addition & 2 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"**/node_modules"
],
"include": [
"index.js",
"src/**/*.js"
"**/*.js"
]
}
78 changes: 0 additions & 78 deletions prepare/index.js

This file was deleted.

50 changes: 50 additions & 0 deletions src/prepare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

const child_process = require('child_process');

const { step } = require(`../src/helpers`);
const { NPM_GLOBAL_DIR, env } = require(`../src/defs`);

const actionRootDir = `${__dirname}/../`;
const cwd = process.env.GITHUB_WORKSPACE;


module.exports = async function () {
/**
* Installing action dependencies before executing any actions
*/
step('NPM Dir change', () => {
child_process.execSync(`mkdir -p ${NPM_GLOBAL_DIR}`, {
stdio: [0, 1, 2],
cwd,
});
child_process.execSync(`npm config set prefix '${NPM_GLOBAL_DIR}'`, {
stdio: [0, 1, 2],
cwd,
});
child_process.execSync(`echo "${NPM_GLOBAL_DIR}/bin" >> $GITHUB_PATH`);
});

// make sure that node_modules are installed
step('Npm action modules install', () => {
child_process.execSync('npm install', {
stdio: [0, 1, 2],
cwd: actionRootDir,
env,
});
});

const { npmVersionCheck, getNpmVersion } = require(`../src/utils`);
const core = require('@actions/core');
// set new npm dir in pipeline paths
core.addPath(`${NPM_GLOBAL_DIR}/bin`);

step('NPM upgrade', () => {
child_process.execSync(`npm install -g npm@${getNpmVersion()}`, {
stdio: [0, 1, 2],
cwd,
});
});

await npmVersionCheck();

}
13 changes: 12 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,19 @@ function getBranchName() {
return branchName;
}

/**
*
* @param {Error} error
*/
function setFailed(error) {
console.log(error.message, error.stack);
core.setFailed(error.message);
}


module.exports = {
npmVersionCheck,
getNpmVersion,
getBranchName
getBranchName,
setFailed
};

0 comments on commit 35b20dc

Please sign in to comment.