type
Post
status
Published
date
Jul 16, 2022
slug
zbbxv0
summary
本文记录了作者在搭建 Midway 项目过程中的一些经验和技巧,同时也介绍了如何利用 Github Actions 来实现持续集成和部署。Midway 是一个基于 Egg.js 和 TypeScript 的 Node.js 开发框架,能够有效提高开发效率和代码质量。本文也提供了一些代码示例和流程图,希望能够对 Node.js 开发者有所帮助。
category
学习笔记
tags
Node
Midway
创建时间
Apr 7, 2023 07:15 PM
更新时间
Apr 15, 2023 06:14 AM
password
icon
Task List
引言
在语雀云端写作 Hexo+Github Actions+COS 持续集成中我需要一个 Node 项目来作为中转站替换原来的腾讯云函数,调用
Github Actions
的接口触发构建流程,这里记录下搭建过程。初始化 Midway
我基本都是按照官方文档来搭建的,初始化的过程也很简单,初始化之后把不需要用到的文件删除,基本不用配置就可以直接就可以写代码了。
接口流程

代码编写
GithubController
import { Controller, Inject, Post } from "@midwayjs/decorator"; import { Context } from "egg"; import { GithubService } from "../service/github"; @Controller("/github") export class GithubController { @Inject() ctx: Context; @Inject() githubService: GithubService; @Post("/action/:repo/:event_type") async deploy() { const { repo, event_type } = this.ctx.params; return await this.githubService.action(repo, event_type); } }
根据语雀的webhooks 介绍,语雀的回调函数是一个 Post 接口

所以可以有以下两种处理方法传参数
- 将需要的参数拼接在调用链接上,通过
@Query()
拿到参数
- 利用动态路由传参数,通过
this.ctx.params
拿到参数
GithubService
import { Provide } from "@midwayjs/decorator"; import axios from "axios"; @Provide() export class GithubService { /** * 触发Github Actions * @param repo * @param event_type */ async action(repo: string, event_type: string): Promise<any> { try { const res = await axios.post( `https://api.github.com/repos/LetTTGACO/${repo}/dispatches`, { event_type }, { headers: { Accept: "*/*", Authorization: "token Github访问Token", }, } ); if (res.status === 204) { return "This is OK!"; } } catch (e) { return e.message; } } }
Done!
大功告成,接下来就是构建和部署阶段了,详情请看