first commit

This commit is contained in:
qxa 2025-03-25 18:25:17 +08:00
commit 8300f4e85e
11 changed files with 144603 additions and 0 deletions

63
.gitattributes vendored Normal file
View File

@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp
###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary
###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary
###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

View File

@ -0,0 +1,33 @@
name: 自动编译
on:
push:
paths:
- "config.js"
- "restore.js"
- "save.js"
- ".gitea/workflows/build.yaml"
jobs:
job1:
name: 编译发布
runs-on: linux
steps:
- name: 下载源码
uses: actions/checkout@v3
- name: 编译
run: |
npm config set registry https://registry.npmmirror.com
npm install
npm run build
- name: 提交更改
run: |
git config --global user.email "qxa@qwit.top"
git config --global user.name "qxa"
git add .
git commit -a -m "自动编译"
git push

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
################################################################################
# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
################################################################################
/.vs
/.vscode
/node_modules
/package-lock.json

1
README.md Normal file
View File

@ -0,0 +1 @@
# 缓存插件

25
action.yaml Normal file
View File

@ -0,0 +1,25 @@
name: cahce
description: 使用缓存
author: qxa
inputs:
key:
description: 缓存压缩标识
required: true
default: ${{ gitea.repository }}
mount:
description: 挂载缓存目录
required: true
hash_files:
description: 缓存命中文件
required: false
outputs:
cache_init:
description: 是否缓存命中
runs:
using: node16
main: dist/restore/index.js
post: dist/save/index.js
post-if: "success()"

67
config.js Normal file
View File

@ -0,0 +1,67 @@
import { getInput, getMultilineInput, info } from '@actions/core'
const crypto = require('crypto')
const fs = require('fs')
function getFileHash256(fileName) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash('sha256')
var reader = fs.createReadStream(fileName)
reader.pipe(hash)
reader.on('end', function () {
resolve(hash.digest('hex'))
})
reader.on('error', function (error) {
reject(error)
})
})
}
export function isExactKeyMatch(key, cacheKey) {
return !!(
cacheKey &&
cacheKey.localeCompare(key, undefined, {
sensitivity: "accent"
}) === 0
)
}
export const mounts = getMultilineInput('mount', { require: true, trimWhitespace: true })
export const { primaryKey, restoreKeys } = await async function () {
const key = getInput('key', { required: true, trimWhitespace: true }).replace('/', '-')
const os = process.env.RUNNER_OS
let primaryKey, restoreKeys = []
const hashFiles = getMultilineInput('hash_files', { trimWhitespace: true })
info('hashFiles=' + hashFiles.join(','))
const tasks = hashFiles.filter(s => fs.existsSync(s))
.map(item => getFileHash256(item))
const hash256 = await Promise.all(tasks).then(res => {
return crypto.createHash('sha256')
.update(res.join(' ') + '|cache')
.digest('hex')
})
primaryKey = `${os}-${key}-${hash256}`
restoreKeys = [
`${os}-${key}-`,
]
return {
primaryKey,
restoreKeys,
}
}()

72167
dist/restore/index.js vendored Normal file

File diff suppressed because one or more lines are too long

72164
dist/save/index.js vendored Normal file

File diff suppressed because one or more lines are too long

14
package.json Normal file
View File

@ -0,0 +1,14 @@
{
"scripts": {
"build": "ncc build restore.js -o dist/restore & ncc build save.js -o dist/save"
},
"dependencies": {
"@actions/cache": "^3.2.3",
"@actions/core": "^1.10.0",
"crypto": "^1.0.1",
"fs": "0.0.1-security"
},
"devDependencies": {
"@vercel/ncc": "^0.34.0"
}
}

32
restore.js Normal file
View File

@ -0,0 +1,32 @@
import { mounts, primaryKey, restoreKeys, isExactKeyMatch } from './config'
import { setOutput, info, error, saveState } from '@actions/core'
import * as cache from '@actions/cache'
async function run() {
if (!cache.isFeatureAvailable()) {
error('缓存未启用')
return
}
const cacheKey = await cache.restoreCache(
mounts,
primaryKey,
restoreKeys,
{ lookupOnly: false },
false
)
if (!cacheKey) {
info(
`Cache not found for input keys: ${[
primaryKey,
...restoreKeys,
].join(", ")}`
)
return
}
saveState('cacheKey', cacheKey)
setOutput('cache_init', isExactKeyMatch(primaryKey, cacheKey).toString())
}
await run()

29
save.js Normal file
View File

@ -0,0 +1,29 @@
import { mounts, primaryKey, isExactKeyMatch } from './config'
import { info, error, getState } from '@actions/core'
import * as cache from "@actions/cache"
async function run() {
if (!cache.isFeatureAvailable()) {
error('缓存未启用')
return
}
const restoredKey = getState('cacheKey')
if (isExactKeyMatch(primaryKey, restoredKey)) {
info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
)
return
}
const cacheId = await cache.saveCache(
mounts,
primaryKey,
{ uploadChunkSize: 1024 * 1024 * 10 },
false
)
if (cacheId != -1) {
info(`Cache saved with key: ${primaryKey}`);
}
}
await run()