Skip to content

Commit 8aecd69

Browse files
committed
release: add step to create draft release with artifacts
Add a job, dependent on the success of all packaging steps, to download the generated artifacts and attach them to a newly-created draft release. The created draft release description is populated with auto-generated release notes [1]. Note the intermediate step "Consolidate artifact directory"; because downloading all artifacts with the 'download-artifacts' action creates a wrapper directory around each artifact [2], we remove those wrappers by moving all files into a single consolidated 'artifacts' directory before running the release creation & attachment script. [1] https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes [2] https://github.com/actions/download-artifact#download-all-artifacts Signed-off-by: Victoria Dye <[email protected]>
1 parent 386c0c7 commit 8aecd69

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

Diff for: .github/workflows/release.yml

+69
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ jobs:
1010
prereqs:
1111
runs-on: ubuntu-latest
1212
outputs:
13+
tag_name: ${{ steps.tag.outputs.name }} # The full name of the tag, e.g. v1.0.0
1314
tag_version: ${{ steps.tag.outputs.version }} # The version number (without preceding "v"), e.g. 1.0.0
1415
steps:
1516
- name: Determine tag to build
1617
run: |
18+
echo "name=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
1719
echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
1820
id: tag
1921

@@ -66,3 +68,70 @@ jobs:
6668
name: ${{env.artifactName}}
6769
path: ${{github.workspace}}/${{env.artifactPath}}
6870
if-no-files-found: error
71+
72+
create-github-release:
73+
needs: [prereqs, package]
74+
name: Create draft release with artifacts
75+
runs-on: ubuntu-latest
76+
steps:
77+
- name: Download packages
78+
uses: actions/download-artifact@v3
79+
with:
80+
path: artifacts-raw
81+
- name: Consolidate artifact directory
82+
shell: bash
83+
run: |
84+
# This step is needed to extract the artifacts from their wrapper
85+
# parent directories. For more details, see
86+
# https://github.com/actions/download-artifact#download-all-artifacts
87+
mkdir artifacts
88+
mv artifacts-raw/*/* artifacts/
89+
- name: Create release & attach artifacts
90+
uses: actions/github-script@v6
91+
with:
92+
script: |
93+
const fs = require('fs');
94+
const path = require('path');
95+
96+
var releaseMetadata = {
97+
owner: context.repo.owner,
98+
repo: context.repo.repo
99+
};
100+
101+
// Create the release
102+
var tagName = "${{ needs.prereqs.outputs.tag_name }}";
103+
var createdRelease = await github.rest.repos.createRelease({
104+
...releaseMetadata,
105+
draft: true,
106+
tag_name: tagName,
107+
name: tagName,
108+
generate_release_notes: true
109+
});
110+
releaseMetadata.release_id = createdRelease.data.id;
111+
112+
// Upload contents of directory to the release created above
113+
async function uploadDirectoryToRelease(directory, includeExtensions=[]) {
114+
return fs.promises.readdir(directory)
115+
.then(async(files) => Promise.all(
116+
files.filter(file => {
117+
return includeExtensions.length==0 || includeExtensions.includes(path.extname(file).toLowerCase());
118+
})
119+
.map(async (file) => {
120+
var filePath = path.join(directory, file);
121+
return github.rest.repos.uploadReleaseAsset({
122+
...releaseMetadata,
123+
name: file,
124+
headers: {
125+
"content-length": (await fs.promises.stat(filePath)).size
126+
},
127+
data: fs.createReadStream(filePath)
128+
});
129+
}))
130+
);
131+
}
132+
133+
await Promise.all([
134+
// Upload all artifacts
135+
uploadDirectoryToRelease('artifacts', ['.pkg', '.deb'])
136+
]);
137+

0 commit comments

Comments
 (0)