@@ -2,6 +2,7 @@ name: "Package"
22description : |
33 Create and upload a package tarball from prepared files, optionally restored from a build artifact.
44 Check out the project, configure Node.js and its package manager, and prepare dependencies, versions and build output before calling this action.
5+ Uses the project's npm, pnpm or Yarn Classic configuration to select its native pack command.
56 Packaging never installs dependencies, changes versions or runs lifecycle scripts.
67author : hoverkraft
78branding :
@@ -77,12 +78,18 @@ runs:
7778 artifact-ids : ${{ inputs.build-artifact-id }}
7879 path : ${{ inputs.build-artifact-path }}
7980
81+ - id : get-package-manager
82+ uses : $/actions/get-package-manager
83+ with :
84+ working-directory : ${{ steps.working-directory.outputs.absolute-path }}
85+
8086 - id : package
8187 name : Create package tarball for verification
8288 uses : actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
8389 env :
8490 WORKING_DIRECTORY : ${{ steps.working-directory.outputs.absolute-path }}
8591 PACKAGE_DIRECTORY : ${{ inputs.package-directory }}
92+ PACK_COMMAND : ${{ steps.get-package-manager.outputs.pack-command }}
8693 ARTIFACT_NAME_INPUT : ${{ inputs.artifact-name }}
8794 GITHUB_RUN_ID : ${{ github.run_id }}
8895 GITHUB_RUN_ATTEMPT : ${{ github.run_attempt }}
@@ -108,52 +115,34 @@ runs:
108115 return;
109116 }
110117
111- const packCommand = 'npm';
112- const packArgs = ['pack', '--json', '--pack-destination', runnerTemp, '--ignore-scripts'];
113- const npmPackResult = await exec.getExecOutput(packCommand, packArgs, {
114- cwd: packageDirectory,
115- ignoreReturnCode: true,
116- env: { ...process.env, npm_config_ignore_scripts: 'true' },
117- });
118-
119- core.debug(`${packCommand} pack stdout: ${npmPackResult.stdout}`);
120- core.debug(`${packCommand} pack stderr: ${npmPackResult.stderr}`);
121-
122- if (npmPackResult.exitCode !== 0) {
123- core.setFailed(`${packCommand} pack failed with exit code ${npmPackResult.exitCode}: ${npmPackResult.stderr}`);
124- return;
125- }
126-
127- let packageInfo;
128- try {
129- packageInfo = JSON.parse(npmPackResult.stdout);
130- } catch (error) {
131- core.setFailed(`Failed to parse ${packCommand} pack output: ${error.message}`);
132- return;
133- }
134-
135- core.debug(`packageInfo: ${JSON.stringify(packageInfo)}`);
136-
137- const tarballName = packageInfo?.[0]?.filename;
138- if (!tarballName) {
139- core.setFailed('No tarball filename returned by npm pack');
140- return;
141- }
142-
143- const tarballPath = path.resolve(runnerTemp, tarballName);
144- if (!fs.existsSync(tarballPath)) {
145- core.setFailed(`Tarball was not created at expected path: ${tarballPath}`);
118+ const packCommand = process.env.PACK_COMMAND?.trim();
119+ if (!packCommand) {
120+ core.setFailed('Pack command is empty');
146121 return;
147122 }
148123
124+ const packDirectory = fs.mkdtempSync(path.join(runnerTemp, 'package-'));
149125 const artifactNameInput = (process.env.ARTIFACT_NAME_INPUT || '').trim();
150126 const artifactName = artifactNameInput || `package-tarball-${process.env.GITHUB_RUN_ID}-${process.env.GITHUB_RUN_ATTEMPT}-${randomUUID()}`;
151- const tarballExtension = path.extname(tarballPath) || '.tgz';
152127 const sanitizedArtifactName = artifactName.replace(/[^a-zA-Z0-9._-]+/g, '-');
153- const uniqueTarballPath = path.join(runnerTemp, `${sanitizedArtifactName}${tarballExtension} `);
128+ const uniqueTarballPath = path.join(runnerTemp, `${sanitizedArtifactName}.tgz `);
154129
155- if (path.resolve(tarballPath) !== path.resolve(uniqueTarballPath)) {
156- await io.mv(tarballPath, uniqueTarballPath);
130+ try {
131+ await exec.exec('bash', ['-e', '-o', 'pipefail', '-c', packCommand], {
132+ cwd: packageDirectory,
133+ env: { ...process.env, PACK_DESTINATION: packDirectory },
134+ });
135+
136+ const tarballs = await (await glob.create(`${packDirectory}/*.tgz`, {
137+ followSymbolicLinks: false,
138+ matchDirectories: false,
139+ })).glob();
140+ if (tarballs.length !== 1) {
141+ throw new Error(`Expected one package tarball, found ${tarballs.length}.`);
142+ }
143+ await io.mv(tarballs[0], uniqueTarballPath);
144+ } finally {
145+ await io.rmRF(packDirectory);
157146 }
158147
159148 core.info(`Generated package tarball: ${uniqueTarballPath}`);
0 commit comments