-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
177 lines (154 loc) · 6.57 KB
/
action.yml
File metadata and controls
177 lines (154 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
name: "Package"
description: "Action to create and upload an npm package tarball from a Node.js project"
author: hoverkraft
branding:
icon: package
color: blue
inputs:
working-directory:
description: |
Working directory where dependencies are installed for packaging.
Can be absolute or relative to the repository root.
required: false
default: "."
package-directory:
description: |
Optional package directory to version and pack.
Can be absolute or relative to `working-directory`.
Useful for monorepos where dependencies are installed at the root.
required: false
default: ""
build-artifact-id:
description: |
Optional build artifact ID to download before packaging.
When provided, the artifact will be downloaded to the workspace.
required: false
default: ""
build-artifact-path:
description: |
Optional path to the build artifact contents relative to the workspace root.
Used to locate the files to be included in the package when a build artifact is downloaded.
required: false
default: "${{ github.workspace }}"
version:
description: |
Optional version to apply with `npm version` before packaging.
The version is applied without creating a Git tag.
required: false
default: ""
artifact-name:
description: "Name of the uploaded package tarball artifact"
required: false
default: "package-tarball"
outputs:
package-tarball-path:
description: "Absolute path to the generated package tarball"
value: ${{ steps.package.outputs.package-tarball-path }}
package-tarball-artifact-id:
description: "Artifact ID of the uploaded package tarball"
value: ${{ steps.upload-package-tarball.outputs.artifact-id }}
runs:
using: "composite"
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
- shell: bash
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
run: mkdir -p ./self-package-action/ && cp -r $GITHUB_ACTION_PATH/../* ./self-package-action/
- id: working-directory
uses: hoverkraft-tech/ci-github-common/actions/working-directory@f5847cb398fe65d53794e6aba98ebdfa0801f691 # 0.32.0
with:
working-directory: ${{ inputs.working-directory }}
enforce-path-in-workspace: "false"
- id: setup-node
uses: ./self-package-action/setup-node
with:
working-directory: ${{ steps.working-directory.outputs.absolute-path }}
- name: Download build artifacts
if: inputs.build-artifact-id != ''
uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
with:
artifact-ids: ${{ inputs.build-artifact-id }}
path: ${{ inputs.build-artifact-path }}
- id: package
name: Create package tarball for verification
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
env:
WORKING_DIRECTORY: ${{ steps.working-directory.outputs.absolute-path }}
PACKAGE_DIRECTORY: ${{ inputs.package-directory }}
RELEASE_VERSION: ${{ inputs.version }}
RUNNER_TEMP: ${{ runner.temp }}
with:
script: |
const fs = require('node:fs');
const path = require('node:path');
const workingDirectory = process.env.WORKING_DIRECTORY;
const packageDirectoryInput = (process.env.PACKAGE_DIRECTORY || '').trim();
const packageDirectory = packageDirectoryInput ? path.resolve(workingDirectory, packageDirectoryInput) : workingDirectory;
core.debug(`Running in working directory: ${workingDirectory}`);
core.debug(`Package directory: ${packageDirectory}`);
process.chdir(workingDirectory);
const releaseVersion = (process.env.RELEASE_VERSION || '').trim();
const runnerTemp = process.env.RUNNER_TEMP;
const packageJsonPath = path.join(packageDirectory, 'package.json');
if (!fs.existsSync(packageJsonPath)) {
core.setFailed(`package.json not found in package directory: ${packageDirectory}`);
return;
}
if (releaseVersion) {
core.info(`Updating package version to provided release version: ${releaseVersion}`);
const npmVersionResult = await exec.getExecOutput(
'npm',
['version', releaseVersion, '--no-git-tag-version', '--no-workspaces-update'],
{
cwd: packageDirectory,
ignoreReturnCode: true,
},
);
if (npmVersionResult.exitCode !== 0) {
core.setFailed(`npm version failed with exit code ${npmVersionResult.exitCode}`);
return;
}
}
const npmPackResult = await exec.getExecOutput('npm', ['pack', '--json', '--pack-destination', runnerTemp], {
cwd: packageDirectory,
ignoreReturnCode: true,
});
core.debug(`npm pack stdout: ${npmPackResult.stdout}`);
core.debug(`npm pack stderr: ${npmPackResult.stderr}`);
if (npmPackResult.exitCode !== 0) {
core.setFailed(`npm pack failed with exit code ${npmPackResult.exitCode}: ${npmPackResult.stderr}`);
return;
}
let packageInfo;
try {
packageInfo = JSON.parse(npmPackResult.stdout);
} catch (error) {
core.setFailed(`Failed to parse npm pack output: ${error.message}`);
return;
}
core.debug(`packageInfo: ${JSON.stringify(packageInfo)}`);
const tarballName = packageInfo?.[0]?.filename;
if (!tarballName) {
core.setFailed('No tarball filename returned by npm pack');
return;
}
const tarballPath = path.join(runnerTemp, tarballName);
if (!fs.existsSync(tarballPath)) {
core.setFailed(`Tarball was not created at expected path: ${tarballPath}`);
return;
}
core.info(`Generated package tarball: ${tarballPath}`);
core.setOutput('package-tarball-path', tarballPath);
- name: Upload package tarball
id: upload-package-tarball
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
with:
name: ${{ inputs.artifact-name }}
path: ${{ steps.package.outputs.package-tarball-path }}
if-no-files-found: error
# FIXME: workaround until will be merged: https://github.com/actions/runner/pull/1684
- shell: bash
if: always()
run: rm -fr ./self-package-action