Search code examples
gitgithubgithub-actions

How to deploy codes to different branch of same repo in github actions?


I need to build some files in one branch and, push the newly formed and updated files to another branch of the same repo using github actions. I have tried this way but it always fails at commit to staging branch

name: Deployment

on:
  push:
    branches: [ develop ]
  pull_request:
    branches: [ develop ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Build files and folders
      run: |
        ./build.sh
        rm -rf cache
        mkdir logs
        cd logs
        ls>output.txt
    - name: commit to staging 
      run: |
        git checkout staging
        git add *
        git commit -m "new push to staging"
        git push

Error

/home/runner/work/_temp/c305681-ab7e-42f8-b39c-8ef35264cff.sh: line 1: github: command not found
Error: Process completed with exit code 127

Solution

  • By default actions/checkout fetches only a single commit and no other branches. You need to fetch the branch first using

    git fetch origin staging:staging
    

    before checking it out.