简体   繁体   中英

Replace one branch with current branch using GitHub Actions

I am using GitHub Actions to compile some code in my master branch and then I want it to push all of the files to another branch gh-pages so that GitHub Pages can generate a site. I can't seem to get Actions to move the content of master to gh-pages .

name: Build & Jekyll

on:
  push:
    branches:
      - master

jobs:
  build:
    # needs: nothing
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Git stuff
        run: |
          git config user.email "email@email.com"
          git config user.name "Name"
          git add .
          git commit -m "message"
          git push origin master:gh-pages

I get this error in the Actions console when I push a commit to master (which triggers the Actions):

HEAD detached at 19e90c4
nothing to commit, working tree clean
##[error]Process completed with exit code 1.

I don't understand why because I don't get any errors if I use those same git commands in Terminal.

I added a line in the Actions run command under 'Git stuff' echo 'Hello, world.' >test.txt echo 'Hello, world.' >test.txt and it instead gave these errors:

[detached HEAD 5edf030] message
  1 file changed, 1 insertion(+)
 create mode 100644 test.txt
error: src refspec master does not match any
error: failed to push some refs to 'https://github.com/iwiedenm/ultimate-jekyll'

SOLUTION (from rmunn's answer):

name: Build & Jekyll
on:
  push:
    branches:
      - master
jobs:
  build:
    # needs: nothing
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Git stuff
        run: |
          git config user.email "your@email.com"
          git config user.name "Jonsnow"
          git add .
          git commit -m "message"
          git remote set-url origin https://USERNAME:${{secrets.ACCESS_TOKEN}}@github.com/USERNAME/REPO.git
          git remote -v
          git checkout -b gh-pages
          git push origin HEAD:gh-pages --force

Don't forget that USERNAME is your GitHub username, ACCESS_TOKEN is a GitHub access token secret set in your repo's secrets with write permission on repos, and REPO is the repository name. You can also rename gh-pages if you'd like.

Disclaimer: I haven't gotten into the GitHub actions beta yet, so although I think this answer is correct, I can't test it.

The GitHub checkout action currently leaves the repo in a detached HEAD state , so there's no master branch for it to push. You should be able to push HEAD:gh-pages instead of master:gh-pages .

Of course, the workflow example in your question makes no changes to the working directory, so git add . has nothing to add to the index, so git commit will not create an empty commit, and then there's nothing to push. But once you have some changes (either because your compile step is working, or because you've put in a step that does echo 'Hello, world.' >> test.txt -- note the double arrows so that you will be appending to the file, otherwise this test would only work once since the second time you'd be committing the same contents and Git would skip the commit), then you should find that pushing HEAD does what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM