-
Notifications
You must be signed in to change notification settings - Fork 2
126 lines (115 loc) · 5.21 KB
/
Copy pathrelease.yml
File metadata and controls
126 lines (115 loc) · 5.21 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
name: create releases from archive artifacts
permissions:
contents: write
on:
workflow_dispatch: {}
push:
branches: [ archive ]
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout archive branch
uses: actions/checkout@v6
with:
fetch-depth: 0
ref: archive
- name: Create releases and upload assets
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const owner = context.repo.owner;
const repo = context.repo.repo;
// Get files changed in the pushed commit (added files)
const sha = process.env.GITHUB_SHA;
if (!sha) {
const core = require('@actions/core');
core.info('GITHUB_SHA not set; nothing to release.');
return;
}
const nameStatus = execSync('git diff-tree --no-commit-id --name-status -r ' + sha).toString('utf8').trim();
if (!nameStatus) {
console.log('No files in commit.');
return;
}
const added = [];
nameStatus.split('\n').forEach(line => {
const parts = line.split('\t');
const status = parts[0].trim();
const file = parts[1] && parts[1].trim();
if (status === 'A' && file && (file.endsWith('.jar') || file.endsWith('.bat'))) added.push(file);
});
if (added.length === 0) {
console.log('No added jar files in commit; nothing to release.');
return;
}
// Read allowed artifacts from variable (one artifactId per line)
const allowedRaw = process.env.RELEASE_ARTIFACTS || '';
const allowedSet = new Set(allowedRaw.split(/\r?\n/).map(s => s.trim()).filter(Boolean));
// Group by artifactId and version
const groups = {}; // key = artifactId||version
for (const fp of added) {
const base = path.basename(fp);
const name_noext = base.replace(/\.jar$/, '');
const parts = name_noext.split('-');
// find first token that starts with digit to be version
let idx = -1;
for (let i = 0; i < parts.length; i++) {
if (/^[0-9]/.test(parts[i])) { idx = i; break; }
}
let artid = name_noext;
let ver = 'unknown';
if (idx > 0) {
artid = parts.slice(0, idx).join('-');
ver = parts[idx];
} else if (idx === 0) {
artid = parts[0];
ver = parts.slice(1).join('-') || 'unknown';
}
// if RELEASE_ARTIFACTS is defined, skip artifacts not listed
if (allowedSet.size > 0 && !allowedSet.has(artid)) {
console.log('Skipping file ' + fp + ' because ' + artid + ' is not in RELEASE_ARTIFACTS');
continue;
}
const key = artid + '::' + ver;
groups[key] = groups[key] || { artid, ver, files: [] };
groups[key].files.push(fp);
}
// Log groups for debugging
console.log('Computed artifact groups:');
console.log(JSON.stringify(groups, null, 2));
// For each group create release and upload files
for (const key of Object.keys(groups)) {
const { artid, ver, files } = groups[key];
const tag = artid + '-' + ver;
let releaseResp;
try {
releaseResp = await github.rest.repos.createRelease({ owner, repo, tag_name: tag, name: artid + '-' + ver, body: 'Artifacts for ' + artid + '-' + ver, draft: true, prerelease: false, make_latest: "" + (artid == process.env.MAIN_ARTIFACT) });
console.log('Created release ' + tag);
} catch (e) {
if (e.status === 422) {
const releases = await github.rest.repos.listReleases({ owner, repo });
const existing = releases.data.find(r => r.tag_name === tag);
if (!existing) throw e;
releaseResp = { data: existing };
console.log('Using existing release ' + tag);
} else {
throw e;
}
}
const releaseId = releaseResp.data.id;
for (const fp of files) {
const abs = path.join(process.env.GITHUB_WORKSPACE, fp);
if (!fs.existsSync(abs)) { console.log('File not found: ' + abs); continue; }
const data = fs.readFileSync(abs);
const name = path.basename(fp);
await github.rest.repos.uploadReleaseAsset({ owner, repo, release_id: releaseId, name: name, data: data, headers: { 'content-type': 'application/java-archive', 'content-length': data.length } });
console.log('Uploaded ' + name + ' to ' + tag);
}
}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_ARTIFACTS: ${{ vars.RELEASE_ARTIFACTS }}