From a252a03f5a0cf0cd501b35d201297c9dbd05258d Mon Sep 17 00:00:00 2001 From: ringotc Date: Wed, 6 Mar 2024 12:16:14 -0800 Subject: [PATCH] 123 --- .github/workflows/deploy.yml | 29 +++++++++++++++++++-------- .releaserc | 6 +++--- test.js | 39 ++++++++++++++++++++++++++++++++++++ test.sh | 33 +++++++++++++++++++++++++----- 4 files changed, 91 insertions(+), 16 deletions(-) create mode 100644 test.js diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index ea09f3c..2aa4609 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -32,14 +32,27 @@ jobs: - name: Get channel information id: get-channel run: | - echo "Current branch: ${{ steps.branch-names.outputs.current_branch }}" - branch_name=${{ steps.branch-names.outputs.current_branch }} - - content=$(cat .releaserc) - - channel=$(echo "$content" | jq -r --arg branch "$branch_name" '.branches[] | select(.name | test("^" + $branch + "$")) | .channel') - echo "Matched channel: $channel" - echo "::set-output name=channel::$channel" + node -e ' + const fs = require("fs"); + const branchName = process.env.BRANCH_NAME; + const content = fs.readFileSync(".releaserc", "utf8"); + const config = JSON.parse(content); + const branches = config.branches; + let channel = null; + for (const branch of branches) { + const regex = new RegExp("^" + branch.name.replace("*", ".*") + "$"); + if (regex.test(branchName)) { + channel = branch.channel; + break; + }else{ + console.log("No match found for branch: ", branchName); + } + } + console.log("Matched channel: ", channel); + process.stdout.write(channel); + ' + env: + BRANCH_NAME: ${{ steps.branch-names.outputs.current_branch }} shell: bash diff --git a/.releaserc b/.releaserc index f6d6ac5..93fec76 100644 --- a/.releaserc +++ b/.releaserc @@ -5,15 +5,15 @@ "channel": "rc" }, { - "name":"alpha/**", + "name":"alpha/*", "channel": "alpha" }, { - "name":"beta/**", + "name":"beta/*", "channel": "beta" }, { - "name":"rc/**", + "name":"rc/*", "channel": "rc" } ], diff --git a/test.js b/test.js new file mode 100644 index 0000000..4c86ee5 --- /dev/null +++ b/test.js @@ -0,0 +1,39 @@ +const fs = require('fs'); + +// 读取.releaserc文件内容 +fs.readFile('.releaserc', 'utf8', (err, data) => { + if (err) { + console.error('Error reading file:', err); + return; + } + + try { + const config = JSON.parse(data); + const branches = config.branches; + + // 函数用于查找匹配的通道 + function findChannel(branchName) { + for (const branch of branches) { + const regex = new RegExp('^' + branch.name.replace('*', '.*') + '$'); + if (regex.test(branchName)) { + return branch.channel; + } + } + return null; // 如果没有匹配项,则返回null + } + + // 测试分支名 + const branchName = 'beta/123'; + const channel = findChannel(branchName); + if (channel) { + console.log(`Branch '${branchName}' matches channel '${channel}'.`); + } else { + console.log(`No matching channel found for branch '${branchName}'.`); + } + + // 可以根据需要对其他分支进行测试 + + } catch (error) { + console.error('Error parsing JSON:', error); + } +}); diff --git a/test.sh b/test.sh index 06d7c67..73c6bb6 100644 --- a/test.sh +++ b/test.sh @@ -1,7 +1,30 @@ -branch_name=$(git rev-parse --abbrev-ref HEAD) - -content=$(cat .releaserc) +#!/bin/bash -channel=$(echo "$content" | jq -r --arg branch "$branch_name" '.branches[] | select(.name | test("^" + $branch + "$")) | .channel') +# 读取.releaserc文件内容,并提取branches部分的JSON数据 +releaserc_content=$(cat .releaserc) +branches_json=$(echo "$releaserc_content" | jq -r '.branches') -echo "::set-output name=channel::$channel" \ No newline at end of file +# 输入要检索的branch name +read -p "请输入要检索的branch name: " branch_name + +# 遍历branches数组,检查是否匹配branch name +matched=false +while IFS= read -r branch_info; do + name=$(echo "$branch_info" | jq -r '.name') + channel=$(echo "$branch_info" | jq -r '.channel') + + echo $branch_info + echo $name + + # 判断是否匹配 + if [[ "$branch_name" == $name || "$branch_name" == $name* ]]; then + echo "匹配成功,$branch_name 对应的channel 是: $channel" + matched=true + break + fi +done <<< "$branches_json" + +# 若没有找到匹配的branch name,则输出未找到匹配信息 +if ! $matched; then + echo "未找到与 $branch_name 匹配的channel" +fi