【问题标题】:Travis CI - How to push into master branch?Travis CI - 如何推入主分支?
【发布时间】:2020-04-11 21:13:41
【问题描述】:

我有一个连接到 GitHub 的 Travis CI 项目,它尝试更新 Github 存储库中的内容并将它们推送回 GitHub,包括 master 和 gh-pages 分支。

然而,虽然我的 travis-ci 日志文件显示一切正常,但我只看到 gh-pages 分支更新,但没有看到 master 分支。

我的 travis.yml 文件是:

language: node_js
node_js: stable

language: python
python: 3.6

# Travis-CI Caching
cache:
  directories:
    - node_modules
    - pip

# S: Build Lifecycle
install:
  - npm install
  - npm install -g gulp
  - python -m pip install requests
  - python -m pip install bs4
  - python -m pip install lxml

before_script:
  - cd archive_builder
  - python build_archive.py
  - cd ..

script:
  - gulp dist

after_script:
  - cd dist
  - git init
  - git config user.name "my git name"
  - git config user.email "my git email"
  - git add -A
  - git commit -m "travis -- update gh-page"
  - git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:gh-pages
  - sh ../purgeCF.sh $CF_ZONE $CF_KEY $CF_EMAIL

  - cd ..
  - git add -A
  - git commit -m "travis -- update master files"
  - git push --quiet "https://${GH_TOKEN}@${GH_REF}" HEAD:master

# E: Build LifeCycle

branches:
  only:
    - master
env:
 global:
   - GH_REF: github.com/mygitname/myprojectname.git

在这个脚本中,我首先使用 gulp 更新和构建网站源文件,将它们存储到“dist”文件夹中。然后我将“dist”中的内容推送到我的 gh-pages 分支,并将其他所有内容推送到我的主分支。

凭据存储为 Travis 的安全密钥,应该可以正常工作。

为了推送“dist/”,我在“dist/”下创建了一个新的“.git/”并将其强制推送为新的。

要推送其他所有内容,我无法这样做,因为根存储库已经包含“.git”文件夹,我不想丢失以前的提交。它应该可以工作。

感谢您的帮助。

【问题讨论】:

    标签: github travis-ci


    【解决方案1】:

    我发现大多数文章或答案都在谈论如何部署到 gh-pages 分支,大​​多数方法对我不起作用,我在 travis 上调试了几天这个问题,我会列出关于如何在 travis 上推送到 master 分支的关键步骤

    例如下面是我的文档库脚本,travis 会自动更新 readme.md。

    1. 生成github token,可以参考文章https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/

    2. 设置环境变量

      GH_REF githu.com/clownvary/docs.git你的仓库地址

      GITHUB_API_KEY ***********您在步骤 1 中生成的令牌

    3. 脚本

      os: osx
      language: node_js
      cache:
        directories:
          - node_modules
      node_js:
        - 'lts/*'
      before_install:
        - git pull
        - brew install tree
      install:
        - npm install
      script:
        - npm run updateReadme
      after_success:
        - git config user.email "travis@travis.org"
        - git config user.name "travis" # this email and name can be set anything you like
        - git add README.md
        - git commit --allow-empty -m "updated README.md"
        - git push https://clownvary:${GITHUB_API_KEY}@${GH_REF} HEAD:master  #clownvary is my username on github, you need to use yourself , do not use travis or others.
      

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      虽然 @gary wang 方法有效,但还有更简单的方法可以直接推送到 GitHub 主分支。

      只需在 deploy 部分下添加 target_branch 变量,并将其分配给 ma​​ster

      有关 Travis CI GitHub 页面部署的文档:https://docs.travis-ci.com/user/deployment/pages/

      .travis.yml的示例内容:

      language: node_js
      ...
      ...
      ...
      deploy:
        provider: pages
        skip_cleanup: true
        keep_history: true
        github_token: $github_token  # Your GitHub token set in Travis CI console
        target_branch: master        # Add this line - To push into GitHub master branch
        on:
          branch: staging            # Your GitHub repo default branch
      

      此方法经过测试并按预期工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-25
        • 2015-11-14
        • 1970-01-01
        • 2013-01-07
        • 1970-01-01
        • 2015-09-05
        • 1970-01-01
        • 2018-05-24
        相关资源
        最近更新 更多