From 4000b0bfebf9589cc27ea6bf34621b3d3e54e362 Mon Sep 17 00:00:00 2001 From: HolographicHat Date: Wed, 23 Nov 2022 14:30:13 +0800 Subject: [PATCH] auto release --- .github/postbuild.js | 18 ++++++++++++ .github/utils.js | 52 +++++++++++++++++++++++++++++++++++ .github/workflows/android.yml | 21 +++++++------- 3 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 .github/postbuild.js create mode 100644 .github/utils.js diff --git a/.github/postbuild.js b/.github/postbuild.js new file mode 100644 index 0000000..2fc2a75 --- /dev/null +++ b/.github/postbuild.js @@ -0,0 +1,18 @@ +const fs = require("fs") +const utils = require("./utils"); + +(async () => { + try { + const latestVersionInfo = await utils.getAppVersion() + let releaseInfo = await utils.getLatestRelease() + if (releaseInfo.name !== latestVersionInfo["package_version"]) { + releaseInfo = await utils.createRelease(latestVersionInfo["package_version"], undefined) + } + for (const path of fs.readdirSync("out")) { + console.log(`upload ${path}...`) + await utils.uploadReleaseAsset(releaseInfo["upload_url"], path) + } + } catch (e) { + console.log(e) + } +})() diff --git a/.github/utils.js b/.github/utils.js new file mode 100644 index 0000000..6713c02 --- /dev/null +++ b/.github/utils.js @@ -0,0 +1,52 @@ +const fs = require("fs") +const axios = require("axios").create({ + validateStatus: _ => true +}) + +const repo = "HolographicHat/GetToken" +const token = process.env.GHP_TOKEN + +const getAppVersion = async () => { + const latestVersionRsp = await axios.get("https://api-takumi.mihoyo.com/ptolemaios/api/getLatestRelease", { + headers: { + "x-rpc-client_type": "2", + "x-rpc-app_version": "2.37.1", + "x-rpc-channel": "miyousheluodi" + } + }) + return latestVersionRsp.data.data +} + +const getLatestRelease = async () => { + const repoLatestVersionRsp = await axios.get(`https://api.github.com/repos/${repo}/releases/latest`) + return repoLatestVersionRsp.data.name +} + +const createRelease = async (name, desc) => { + const result = await axios.post(`https://api.github.com/repos/${repo}/releases`, { + tag_name: name, + name: name, + body: desc, + generate_release_notes: true + }, { + headers: { + "Authorization": `Bearer ${token}`, + } + }) + return result.data +} + +const uploadReleaseAsset = async (url, path) => { + const uri = url.substring(0, url.indexOf("{")) + "?name=" + require("path").basename(path) + const size = fs.statSync(path).size + const data = fs.readFileSync(path) + await axios.post(uri, data, { + headers: { + "Content-Type": "application/vnd.android.package-archive", + "Content-Length": `${size}`, + "Authorization": `Bearer ${token}`, + } + }) +} + +module.exports = { getAppVersion, getLatestRelease, createRelease, uploadReleaseAsset } diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index 59e5e03..206ad25 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -12,6 +12,7 @@ jobs: build: runs-on: ubuntu-latest env: + GHP_TOKEN: ${{ secrets.GHP_TOKEN }} SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }} SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }} steps: @@ -30,19 +31,19 @@ jobs: - name: Install jq run: sudo apt-get install -y jq - name: Download package - run: "curl -H 'x-rpc-client_type: 2' -H 'x-rpc-app_version: 2.37.1' -H 'x-rpc-channel: miyousheluodi' https://api-takumi.mihoyo.com/ptolemaios/api/getLatestRelease | jq --raw-output '.data.package_url' | xargs curl -o origin.apk" + run: "curl -H 'x-rpc-client_type: 2' -H 'x-rpc-app_version: 2.37.1' -H 'x-rpc-channel: miyousheluodi' https://api-takumi.mihoyo.com/ptolemaios/api/getLatestRelease | jq --raw-output '.data.package_url' | xargs curl -o miyoushe.apk" - name: Patch package - run: "mkdir out && mv app/build/outputs/apk/release/app-release.apk out && java -jar gradle/lspatch/jar-v0.5.1-361-release.jar -f -l 2 -m out/app-release.apk -o out/patched.apk origin.apk" + run: "mkdir out && mv app/build/outputs/apk/release/app-release.apk . && java -jar gradle/lspatch/jar-v0.5.1-361-release.jar -f -l 2 -m app-release.apk -o out miyoushe.apk && mv app-release.apk out" - name: Upload build artifacts uses: actions/upload-artifact@v3.1.0 with: name: Artifacts path: out - #- name: Set up Node 16 - # uses: actions/setup-node@v3 - # with: - # node-version: 16 - #- name: Install depencies - # run: npm install axios@0.27.2 - #- name: Run post build script - # run: + - name: Set up Node 16 + uses: actions/setup-node@v3 + with: + node-version: 16 + - name: Install dependencies + run: npm install axios@0.27.2 + - name: Run post build script + run: node .github/postbuild.js