Skip to content

Commit

Permalink
CI: download previous build using GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jan 18, 2025
1 parent 5e0780a commit 64027e3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 69 deletions.
23 changes: 18 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ jobs:
with:
node-version-file: ".node-version"
cache: "pnpm"
- name: Create RAM Disk for building
id: ramdisk
run: |
BUILD_DIR=$(mkdtemp /dev/shm/tempdir.XXXXXX)
echo "build_dir=$BUILD_DIR" >> $GITHUB_ENV
- name: Download Previous Build
uses: actions/checkout@v4
with:
repository: SukkaLab/ruleset.skk.moe
persist-credentials: false
path: ${{ steps.ramdisk.outputs.build_dir }}
- run: rm -rf "${{ steps.ramdisk.outputs.build_dir }}/.git"
- name: Get current date
id: date
run: |
Expand Down Expand Up @@ -57,28 +69,29 @@ jobs:
${{ runner.os }}-v3-
- run: pnpm install
- run: pnpm run build
id: build
env:
PUBLIC_DIR: ${{ steps.ramdisk.outputs.build_dir }}
- name: Pre-deploy check
# If the public directory doesn't exist, the build should fail.
# If the public directory is empty, the build should fail.
run: |
if [ ! -d ${{ steps.build.outputs.public_dir }} ]; then
if [ ! -d ${{ steps.ramdisk.outputs.build_dir }} ]; then
echo "public directory not found"
exit 1
fi
if [ ! "$(ls -A ${{ steps.build.outputs.public_dir }})" ]; then
if [ ! "$(ls -A ${{ steps.ramdisk.outputs.build_dir }})" ]; then
echo "public directory is empty"
exit 1
fi
if [ ! -f .BUILD_FINISHED ]; then
echo ".BUILD_FINISHED not found"
exit 1
fi
echo "public directory is ready: ${{ steps.build.outputs.public_dir }}"
echo "public directory is ready: ${{ steps.ramdisk.outputs.build_dir }}"
- uses: actions/upload-artifact@v4
with:
name: build-artifact-${{ github. ref_name }}
path: ${{ steps.build.outputs.public_dir }}
path: ${{ steps.ramdisk.outputs.build_dir }}
if-no-files-found: error
retention-days: 1
compression-level: 4
Expand Down
7 changes: 2 additions & 5 deletions Build/constants/dir.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import path from 'node:path';
import fs from 'node:fs';
import { isCI } from 'ci-info';
import process from 'node:process';

export const ROOT_DIR = path.resolve(__dirname, '../..');

export const CACHE_DIR = path.resolve(ROOT_DIR, '.cache');

export const SOURCE_DIR = path.join(ROOT_DIR, 'Source');

export const PUBLIC_DIR = isCI
? fs.mkdtempSync('/dev/shm/sukkaw-surge-public-')
: path.resolve(ROOT_DIR, 'public');
export const PUBLIC_DIR = process.env.PUBLIC_DIR || path.resolve(ROOT_DIR, 'public');

export const OUTPUT_SURGE_DIR = path.join(PUBLIC_DIR, 'List');
export const OUTPUT_CLASH_DIR = path.resolve(PUBLIC_DIR, 'Clash');
Expand Down
6 changes: 6 additions & 0 deletions Build/download-previous-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import picocolors from 'picocolors';
import { PUBLIC_DIR } from './constants/dir';
import { requestWithLog } from './lib/fetch-retry';
import { isDirectoryEmptySync } from './lib/misc';
import { isCI } from 'ci-info';

const GITHUB_CODELOAD_URL = 'https://codeload.github.com/sukkalab/ruleset.skk.moe/tar.gz/master';
const GITLAB_CODELOAD_URL = 'https://gitlab.com/SukkaW/ruleset.skk.moe/-/archive/master/ruleset.skk.moe-master.tar.gz';
Expand All @@ -20,6 +21,11 @@ export const downloadPreviousBuild = task(require.main === module, __filename)(a
return;
}

// we uses actions/checkout to download the previous build now, so we should throw if the directory is empty
if (isCI) {
throw new Error('CI environment detected, but public directory is empty');
}

const tarGzUrl = await span.traceChildAsync('get tar.gz url', async () => {
const resp = await requestWithLog(GITHUB_CODELOAD_URL, { method: 'HEAD' });
if (resp.statusCode !== 200) {
Expand Down
8 changes: 1 addition & 7 deletions Build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ import { buildCloudMounterRules } from './build-cloudmounter-rules';
import { createSpan, printTraceResult, whyIsNodeRunning } from './trace';
import { buildDeprecateFiles } from './build-deprecate-files';
import path from 'node:path';
import { PUBLIC_DIR, ROOT_DIR } from './constants/dir';

import { setOutput } from '@actions/core';
import { ROOT_DIR } from './constants/dir';

process.on('uncaughtException', (error) => {
console.error('Uncaught exception:', error);
Expand Down Expand Up @@ -108,10 +106,6 @@ const buildFinishedLock = path.join(ROOT_DIR, '.BUILD_FINISHED');
// write a file to demonstrate that the build is finished
fs.writeFileSync(buildFinishedLock, 'BUILD_FINISHED\n');

if (process.env.GITHUB_OUTPUT) {
setOutput('public_dir', PUBLIC_DIR);
}

// Finish the build to avoid leaking timer/fetch ref
await whyIsNodeRunning();
process.exit(0);
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"@actions/core": "^1.11.1",
"@ghostery/adblocker": "^2.3.1",
"@henrygd/queue": "^1.0.7",
"async-retry": "^1.3.3",
Expand Down
51 changes: 0 additions & 51 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 64027e3

Please sign in to comment.