使用Python3实现Gitee码云自动化发布

仓库信息

https://gitee.com/liumou_site/ip

实现代码

python 复制代码
import os

import requests
from loguru import logger


def gitee(ver, message, prerelease: bool = False):
	"""
    在 Gitee 上创建发布版本

    :param ver: 版本号
    :param message: 发布信息
    :param prerelease: 是否为预发布版本,默认为 False
    :return: 无
    """
	# 配置信息
	GITEE_ACCESS_TOKEN = os.getenv('GITEE_ACCESS_TOKEN')
	if GITEE_ACCESS_TOKEN is None:
		print("请设置环境变量 GITEE_ACCESS_TOKEN")
		exit(1)
	logger.debug("GITEE_ACCESS_TOKEN: " + GITEE_ACCESS_TOKEN)
	OWNER = 'liumou_site'
	REPO = 'ip'
	TAG_NAME = f'v{ver}'
	NAME = f'Version {ver}'

	# 构建请求 URL
	url = f'https://gitee.com/api/v5/repos/{OWNER}/{REPO}/releases'


	# 构建请求体
	data = {
		'tag_name': TAG_NAME,
		'name': NAME,
		'body': message,
		'target_commitish': 'master',
		'owner': OWNER,
		'repo': REPO,
		'prerelease': prerelease,
		'access_token': GITEE_ACCESS_TOKEN
	}

	# 发送 POST 请求
	response = requests.post(url, data=data)

	# 检查响应
	if response.status_code == 201:
		logger.info('Release created successfully!')
		logger.info(response.json())
	else:
		logger.error(f'Failed to create release: {response.status_code}')
		logger.error(response.text)
		logger.error(response.request)
		exit(2)


if __name__ == '__main__':
	gitee(ver='1.0.9', message='master', prerelease=False)

效果

powershell 复制代码
"D:\Program Files\Python313\python.exe" D:\code\gitee\go\ip\gitee.py 
2024-11-21 10:36:57.933 | DEBUG    | __main__:gitee:21 - GITEE_ACCESS_TOKEN: 7ac..........
2024-11-21 10:36:58.406 | INFO     | __main__:gitee:48 - Release created successfully!
2024-11-21 10:36:58.407 | INFO     | __main__:gitee:49 - {'id': 450710, 'tag_name': 'v1.0.9', 'target_commitish': '28a5383f0cc7103546c805100b4a0d68e40547c8', 'prerelease': False, 'name': 'Version 1.0.9', 'body': 'master', 'author': {'id': 1785140, 'login': 'liumou_site', 'name': '坐公交也用券', 'avatar_url': 'https://foruda.gitee.com/avatar/1673661977951995067/1785140_liumou_site_1673661977.png', 'url': 'https://gitee.com/api/v5/users/liumou_site', 'html_url': 'https://gitee.com/liumou_site', 'remark': '', 'followers_url': 'https://gitee.com/api/v5/users/liumou_site/followers', 'following_url': 'https://gitee.com/api/v5/users/liumou_site/following_url{/other_user}', 'gists_url': 'https://gitee.com/api/v5/users/liumou_site/gists{/gist_id}', 'starred_url': 'https://gitee.com/api/v5/users/liumou_site/starred{/owner}{/repo}', 'subscriptions_url': 'https://gitee.com/api/v5/users/liumou_site/subscriptions', 'organizations_url': 'https://gitee.com/api/v5/users/liumou_site/orgs', 'repos_url': 'https://gitee.com/api/v5/users/liumou_site/repos', 'events_url': 'https://gitee.com/api/v5/users/liumou_site/events{/privacy}', 'received_events_url': 'https://gitee.com/api/v5/users/liumou_site/received_events', 'type': 'User'}, 'created_at': '2024-11-21T10:36:58+08:00', 'assets': [{'browser_download_url': 'https://gitee.com/liumou_site/ip/archive/refs/tags/v1.0.9.zip', 'name': 'v1.0.9.zip'}, {'browser_download_url': 'https://gitee.com/liumou_site/ip/archive/refs/tags/v1.0.9.tar.gz', 'name': 'v1.0.9.tar.gz'}]}

进程已结束,退出代码0

官方接口

Gitee接口

相关推荐
2601_949847756 小时前
bcrypt.js 跨端三方库仓颉适配全流程
linux·运维·ubuntu
m0_639906946 小时前
Linux 操作系统入门:目录结构、服务排查常用命令、Shell 脚本基础
linux·运维·后端
meiying88386 小时前
快递单号批量查询Python脚本:从手动到自动化的完整实现
开发语言·python·自动化
今天重构了吗6 小时前
Linux 上 rm -rf 删错了能找回吗
运维·服务器
KaiwuDB7 小时前
KaiwuDB 分布式集群扩缩容,从原理到完整操作踩坑总结
运维·数据库·分布式
天远Date Lab7 小时前
零信任架构实战:基于天远企业年报信息核验构建自动化商业保理财务与社保穿透评估网关
人工智能·架构·自动化
codezzzsleep7 小时前
学术汇报 PPT 自动化生成实战指南
运维·自动化·powerpoint
古法老登瞎折腾7 小时前
大数据智能运维实战(九):让 AI 查日志——找到异常,也保留上下文
java·大数据·运维·人工智能·spring ai·mcp
讯捷蓝达8 小时前
IBM V3500存储 双控故障、数据怎么恢复?
运维
qq_谁赞成_谁反对8 小时前
甲方IT的成长之路--SG TCP网络优化工具--20261026
运维·服务器·udp·tcp