gpt4 book ai didi

python - Github Action 使用现有的 Python 脚本并获取字符串输出

转载 作者:行者123 更新时间:2023-12-04 23:46:47 27 4
gpt4 key购买 nike

我是第一次在 Github Action 上工作用于在 Android 中设置 CI/CD 工作流

我有一个现有的 Python script生成 Change logHTML String out put .

我想在 github 操作中使用它并接收更改日志作为字符串输出。

我有这样的

- name: Generate Change logs
id: changelogs
working-directory: .github/workflows/
run: python release_mail_generator.py ${{ secrets.GITHUB_TOKEN }} v2.21

想用这样的东西来获得changelog字符串作为输出
- name: Generate Change logs
id: changelogs
working-directory: .github/workflows/
run: echo "::set-output name=changelog::$(echo python release_mail_generator.py ${{ secrets.GITHUB_TOKEN }} v2.21)"

我的python脚本
from __future__ import print_function

import sys
from collections import defaultdict
from github import Github

# Install dependencies using command pip3 install -r scripts/delivery/requirements.txt
# Run script by running command python3 scripts/delivery/release_mail_generator.py ${token} ${milestone}
class ReleaseMailGenerator:
MASTER_LABEL_LIST = set(
["Collections", "Core Experience", "Growth", "Platform", "Product Initiatives", "User Success", "Translations"]
)
PAGE_ITEM_COUNT = 200

TITLE_TAG_TMPL = '<h2>{}</h2>'
LABEL_SECTION_TMPL = '<br/><b><i>{}</i></b>'
LIST_OPEN_TAG = '<ul style="list-style-type:disc">'
LIST_LINE_ITEM_TMPL = '<li>{}<a target="_blank" href=\"{}\"> (#{})</a> by {}<br/></li>'
LIST_CLOSE_TAG = '</ul>'
MESSAGE = '<p>Android app {} has been pushed to Play Store on internal track. Please review the build and give sign off soon</p>'
INTERNAL_TEST_DISCLAIMER = '<p>Click on the following link to join internal test distribution program on Play Store</p>' + '<a href="https://play.google.com/apps/internaltest/xxxxxxxx">https://play.google.com/apps/internaltest/xxxxxx</a>'

def get_pull_request_details(self, label, pull_requests_list):
"""

:param label:
:param pull_requests_list:
:return:
"""
pr_details = self.LABEL_SECTION_TMPL.format(label) + self.LIST_OPEN_TAG
for pr in pull_requests_list:
if pr.user.name:
name = pr.user.name
else:
name = pr.user.login
pr_details += self.LIST_LINE_ITEM_TMPL.format(pr.title, pr.html_url, pr.number, name)
pr_details += self.LIST_CLOSE_TAG

return pr_details

def get_pull_request_label_map(self, repo, issues):
"""

:param issues:
:param repo:
:return:
"""
pull_request_label_map = defaultdict(list)

for issue in issues:
if not issue.pull_request:
continue

pr = repo.get_pull(issue.number)
if not pr.is_merged():
continue

valid_label_list = set([label.name for label in issue.labels]).intersection(self.MASTER_LABEL_LIST)

if not valid_label_list:
pull_request_label_map['Other'].append(issue)

for label in valid_label_list:
pull_request_label_map[label].append(issue)
return pull_request_label_map

def get_pull_requests(self, repo, milestone_name):

try:
milestones = repo.get_milestones()
for milestone in milestones:
if milestone.title == milestone_name:
milestone = milestone
break
else:
print('---------------------------------------------------------------------------------------\n')
print('Incorrect Milestone, Please Check\n')
print('----------------------------------------------------------------------------------------')
return
except Exception:
print(e)
return

return repo.get_issues(milestone, state="closed")

def get_changelog(self, auth_token, milestone):
"""

:param auth_token:
:param milestone:
:return:
"""

github = Github(login_or_token=auth_token, per_page=self.PAGE_ITEM_COUNT)
repo = github.get_repo("karthikkolanji/ShaadiDemo")
pull_requests = self.get_pull_requests(repo, milestone)
if not pull_requests:
print('---------------------------------------------------------------------------------------\n')
print('No pull requests found for given Milestone, Please check \n')
print('----------------------------------------------------------------------------------------')
return
pull_request_label_map = self.get_pull_request_label_map(repo, pull_requests)

if not pull_request_label_map:
print('---------------------------------------------------------------------------------------\n')
print('No pull requests is merged for given Milestone, Please check \n')
print('----------------------------------------------------------------------------------------')
return

changelog = self.MESSAGE.format(milestone) + self.INTERNAL_TEST_DISCLAIMER + self.TITLE_TAG_TMPL.format('Changelog')
for label, pull_requests_list in pull_request_label_map.items():
changelog += self.get_pull_request_details(label, pull_requests_list)

print(changelog)

return changelog


length = len(sys.argv)

if length < 3:
print('This script requires two command line areguments\n1. Github access token\n2. Release milestone')
sys.exit()

ReleaseMailGenerator().get_changelog(sys.argv[1], sys.argv[2])

最佳答案

echo python script.py是无效的语法。您必须在没有 echo 的情况下运行 python 脚本直接在 set-output命令或将结果分配给变量,然后将此变量设置为输出。当您的 python 脚本产生多行输出时,第二个选项将为您工作,因为您需要转义 \r , \n符号(根据 this 讨论)。所以,这是适合我的配置:

测试python脚本

import sys

print("argument {}".format(sys.argv[1]))
print("changelog 1")
print("changelog 2")
print("changelog 3")

.github/workflows/test.yml

---
name: Test

on: push

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.1.0
- run: |
output=$(python release_mail_generator.py v2.21)
output="${output//'%'/'%25'}"
output="${output//$'\n'/'%0A'}"
output="${output//$'\r'/'%0D'}"
echo "::set-output name=changelog::$output"
id: step1
- name: Print changelog
run: echo "${{ steps.step1.outputs.changelog }}"

结果

enter image description here

关于python - Github Action 使用现有的 Python 脚本并获取字符串输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61656704/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com