Skip to content

Create Nightly Release #86

Create Nightly Release

Create Nightly Release #86

name: Create Nightly Release
on:
# This can be used to automatically publish nightlies at UTC nighttime
schedule:
- cron: '0 2 * * *' # run at 2 AM UTC
# This can be used to allow manually triggering nightlies from the web interface
workflow_dispatch:
jobs:
nightly-release:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: '0'
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 18
- name: Check Latest Commit Date
id: check_commit_date
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Get the latest commit date in ISO 8601 format
LATEST_COMMIT_DATE=$(git log -1 --format=%cI)
# Convert the latest commit date and the current date to seconds since epoch
LATEST_COMMIT_SECONDS=$(date --date="$LATEST_COMMIT_DATE" +%s)
CURRENT_DATE_SECONDS=$(date +%s)
# Calculate the time difference in seconds
TIME_DIFF=$((CURRENT_DATE_SECONDS - LATEST_COMMIT_SECONDS))
# Check if the difference is less than 24 hours (86400 seconds)
# This is to avoid releasing nightly code older than 24 hours.
# We only release a nightly if there were commits in the last 24 hours.
if [ $TIME_DIFF -lt 86400 ]; then
# generate a unique tag for nightly release
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
VERSION=$(cat VERSION)
TAG_NAME="nightly-v$VERSION-$TIMESTAMP"
# Get commit history and create release
# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28
node -e '
const commits = `'"$(git log --since="24 hours ago" --oneline)"'`.split("\n");
let message = "\n";
commits.forEach(commit => {
message += "- " + commit + "\n";
});
const payload = {
tag_name: `'"$TAG_NAME"'`,
target_commitish: "main",
name: `Nightly Build '"$TAG_NAME"'`,
body: `## Automated nightly release from main branch` + message,
draft: false,
prerelease: true,
};
fetch(
`https://api.github.com/repos/'"$GITHUB_REPOSITORY"'/releases`,
{
method: "POST",
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer '"$GITHUB_TOKEN"'`,
"X-GitHub-Api-Version": "2022-11-28",
},
body: JSON.stringify(payload),
}
)
.then((response) => {
if (!response.ok) {
console.error(`Error: ` + response.status);
return response.text().then((text) => {
console.error(text);
process.exit(1);
});
}
return response.json();
})
.then((data) => {
console.log("Release created successfully:", data);
})
.catch((error) => {
console.error("An error occurred:", error);
process.exit(1);
});
'
fi