forked from naveenrajm7/rpmbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
140 lines (115 loc) · 5.1 KB
/
Copy pathmain.ts
File metadata and controls
140 lines (115 loc) · 5.1 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
const core = require('@actions/core');
const github = require('@actions/github');
const exec = require('@actions/exec');
const io = require('@actions/io');
const cp = require('child_process');
const fs = require('fs');
const path = require('path');
async function run() {
try {
// Get github context data
const context = github.context;
// To be used to get contents of this git ref
const owner = context.repo.owner
const repo = context.repo.repo
const ref = context.ref
// get inputs from workflow
// specFile name
const configPath = core.getInput('spec_file'); // user input, eg: `foo.spec' or `rpm/foo.spec'
const basename = path.basename(configPath); // always just `foo.spec`
const specFile = {
srcFullPath: `/github/workspace/${configPath}`,
destFullPath: `/github/home/rpmbuild/SPECS/${basename}`,
};
// Read spec file and get values
var data = fs.readFileSync(specFile.srcFullPath, 'utf8');
let name = '';
let version = '';
for (var line of data.split('\n')){
var lineArray = line.split(/[ ]+/);
if(lineArray[0].includes('Name')){
name = name+lineArray[1];
}
if(lineArray[0].includes('Version')){
version = version+lineArray[1];
}
}
console.log(`name: ${name}`);
console.log(`version: ${version}`);
// setup rpm tree
await exec.exec('rpmdev-setuptree');
// Copy spec file from path specFile to /github/home/rpmbuild/SPECS/
await exec.exec(`cp ${specFile.srcFullPath} ${specFile.destFullPath}`);
// Make the code in /github/workspace/ into a tar.gz, located in /github/home/rpmbuild/SOURCES/
const oldGitDir = process.env.GIT_DIR;
process.env.GIT_DIR = '/github/workspace/.git';
await exec.exec(`git archive --output=/github/home/rpmbuild/SOURCES/${name}-${version}.tar.gz --prefix=${name}-${version}/ HEAD`);
await exec.exec(`ln -s /github/home/rpmbuild/SOURCES/${name}-${version}.tar.gz /github/home/rpmbuild/SOURCES/${name}.tar.gz`);
process.env.GIT_DIR = oldGitDir;
// Copy any *.patch files from the repo root into SOURCES so spec files
// can reference them via standard Patch0:/Patch1:/... declarations.
// No-op if the repo has no patch files.
const workspaceFiles = fs.readdirSync('/github/workspace');
for (const f of workspaceFiles) {
if (f.endsWith('.patch')) {
console.log(`Copying patch file to SOURCES: ${f}`);
await exec.exec(`cp /github/workspace/${f} /github/home/rpmbuild/SOURCES/`);
}
}
// Installs additional repositories
const additionalRepos = core.getInput('additional_repos'); // user input, eg: '["centos-release-scl"]'
if (additionalRepos) {
const arr = JSON.parse(additionalRepos);
for (let i = 0; i < arr.length; i++) {
console.log(`Installing repo': ${arr[i]}`);
await exec.exec(`dnf install -y ${arr[i]}`);
};
}
// Installs build dependencies
await exec.exec(`dnf builddep -y ${specFile.destFullPath}`);
// Execute rpmbuild , -ba generates both RPMS and SPRMS
try {
await exec.exec(
`rpmbuild -ba ${specFile.destFullPath}`
);
} catch (err) {
core.setFailed(`action failed with error: ${err}`);
}
// Verify RPM is created
await exec.exec('ls /github/home/rpmbuild/RPMS');
// setOutput rpm_path to /root/rpmbuild/RPMS , to be consumed by other actions like
// actions/upload-release-asset
// Get source rpm name , to provide file name, path as output
let myOutput = '';
await cp.exec('ls /github/home/rpmbuild/SRPMS/', (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err)
} else {
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
myOutput = myOutput+`${stdout}`.trim();
console.log(`stderr: ${stderr}`);
}
});
// only contents of workspace can be changed by actions and used by subsequent actions
// So copy all generated rpms into workspace , and publish output path relative to workspace (/github/workspace)
await exec.exec(`mkdir -p rpmbuild/SRPMS`);
await exec.exec(`mkdir -p rpmbuild/RPMS`);
await exec.exec(`cp /github/home/rpmbuild/SRPMS/${myOutput} rpmbuild/SRPMS`);
await cp.exec(`cp -R /github/home/rpmbuild/RPMS/. rpmbuild/RPMS/`);
await exec.exec(`ls -la rpmbuild/SRPMS`);
await exec.exec(`ls -la rpmbuild/RPMS`);
// set outputs to path relative to workspace ex ./rpmbuild/
core.setOutput("source_rpm_dir_path", `rpmbuild/SRPMS/`); // path to SRPMS directory
core.setOutput("source_rpm_path", `rpmbuild/SRPMS/${myOutput}`); // path to Source RPM file
core.setOutput("source_rpm_name", `${myOutput}`); // name of Source RPM file
core.setOutput("rpm_dir_path", `rpmbuild/RPMS/`); // path to RPMS directory
core.setOutput("rpm_content_type", "application/octet-stream"); // Content-type for Upload
} catch (error: unknown) {
if (error instanceof Error) {
core.setFailed(error.message);
}
}
}
run();