如何配置Github Actions让两台EC2实例分别运行生产、开发环境流水线
解决方案
步骤1:为两台自托管Runner配置专属环境标签
你可以通过两种方式给Runner添加环境标签:
- 注册Runner时直接指定:注册生产环境EC2上的Runner时,在注册命令末尾加
--labels production参数;注册开发环境EC2上的Runner时加--labels development参数 - 已注册的Runner修改标签:进入GitHub仓库的「Settings」→「Actions」→「Runners」,分别找到两台EC2对应的Runner实例,编辑添加对应环境标签即可
步骤2:修改生产环境流水线配置
把production.yml里的runs-on字段修改为同时匹配自托管和生产标签,确保只有生产EC2的Runner会接收到生产流水线任务:
name: Live backend on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: [self-hosted, production] # 修改为匹配双标签 strategy: matrix: node-version: [14.x] steps: - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: rm package-lock.json - run: npm i - run: pm2 restart app.js
步骤3:编写开发环境流水线配置
新增的development.yml配置参考如下,指定运行在带development标签的自托管Runner上,触发规则可以根据你的开发分支策略调整,比如示例中绑定dev分支的推送事件:
name: Dev backend on: push: branches: [ dev ] # 可根据实际情况修改为你使用的开发分支名 pull_request: branches: [ dev ] jobs: build: runs-on: [self-hosted, development] # 匹配开发环境Runner标签 strategy: matrix: node-version: [14.x] steps: - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: rm package-lock.json - run: npm i - run: pm2 restart app.js
实现原理
GitHub Actions的runs-on字段会匹配同时满足所有指定标签的Runner,通过给不同环境的Runner加上独有的环境标签,就能实现两台EC2分别接收对应环境的流水线任务,不会出现调度错乱的问题。
内容的提问来源于stack exchange,提问作者margherita pizza
相关产品推荐
相关产品推荐

