【问题标题】:Github Actions - How to Build and Deploy on push to masterGithub Actions - 如何在推送到 master 时构建和部署
【发布时间】:2022-01-13 14:17:17
【问题描述】:

我有一个包含 api 文档的 github 存储库。在与 master 合并时,我想使用 github 操作来做两件事:

  1. “构建” - 使用 python 脚本更新 index.html 文件
  2. “部署” - 将 repo 的内容同步到 S3

这是(简化的)回购结构:

.github/
  |-- workflows/
    |-- main.yml
    |-- build.py
dist/
  |-- index.html

build.py 是 Python 脚本,它将通过替换 dist/index.html 中的一些变量来“构建”模板。 dist 文件夹的内容随后会使用此 github 操作同步到 AWS S3:

name: Upload Website

on:
  push:
    branches:
    - master

jobs:

  # updates index.html:      
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Run a one-line script
      run: python ./.github/workflows/build.py
    ...
    - name: commit changed files
      run: git commit -m "Auto adding config files"
    - name: fetch from master
      run: git fetch origin master
    - name: push code to master
      run: git push origin HEAD:master

  # deploys latest version to s3:
  deploy:
    runs-on: ubuntu-latest
    steps:
      uses: actions/checkout@v1
    - name: Deploy static site to S3 bucket
      run: aws s3 sync ./dist <BUCKET DESTINATION>

部署部分本身可以正常工作,但我在构建部分遇到问题。这会导致无限循环吗?该操作设置为在推送到 master 时运行,但该操作还将更新的模板推送到 master。如果是,我该如何避免这种情况?

我正在关注 this 构建部分的示例。

【问题讨论】:

  • 构建部件的目的是什么?

标签: github github-actions


【解决方案1】:

如果推送到主分支,则会发生无限循环。构建部分将运行。

在构建部分,它会再次推送到 master,因此发生无限循环。

从构建阶段移除这部分:

- name: commit changed files
  run: git commit -m "Auto adding config files"
- name: fetch from master
  run: git fetch origin master
- name: push code to master
  run: git push origin HEAD:master

【讨论】:

  • 我明白了——所以这仍然会更新主分支中的文件?会反映在github上吗?
  • 我没有看到 master 分支上的变化 - 也许一个好方法是使用第二个分支进行部署?
  • 你可以把HEAD:master换成其他的,再试一次,那部分的目的是什么
猜你喜欢
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 2018-02-19
  • 2020-03-25
相关资源
最近更新 更多