Skip to content

Commit

Permalink
123
Browse files Browse the repository at this point in the history
  • Loading branch information
RingoTC committed Mar 6, 2024
1 parent 95284eb commit a252a03
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 16 deletions.
29 changes: 21 additions & 8 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
6 changes: 3 additions & 3 deletions .releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"channel": "rc"
},
{
"name":"alpha/**",
"name":"alpha/*",
"channel": "alpha"
},
{
"name":"beta/**",
"name":"beta/*",
"channel": "beta"
},
{
"name":"rc/**",
"name":"rc/*",
"channel": "rc"
}
],
Expand Down
39 changes: 39 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
33 changes: 28 additions & 5 deletions test.sh
Original file line number Diff line number Diff line change
@@ -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"
# 输入要检索的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

0 comments on commit a252a03

Please sign in to comment.