User github pages site - auto deploy from another branch on push

Hey guys,

I’m creating a user page with github pages. Therefore I have to put the build files to the master branch and develop the website in my “dev” branch.

I would like to know if there is a ready to use action that triggers whenever I push code in my dev branch. Then it would remove all the files in the master branch run a npm run build command in the dev branch. After that it copies only the build files from the dist folder into the master branch.

Currently I’m doing this on my own and it would be really awesome if there is something I can use to automate this. Yes, I could do this on my own with a shell script and handle the two branches with some git, npm and copy commands but maybe there already is an action I can use for this? :slight_smile:

Thanks in advance!

Hi @mhcomp ,

You need to create a workflow for ‘dev’ branch with ‘push’ event. 

Step1: Remove master branch content and ‘npm run build’ on dev branch to create the build files.

Step2: Push dist folder build files into master branch with actions ‘ad-m/github-push-action’.

Code sample as below:

      - dev

    runs-on: [ubuntu-latest]
      - name: checkout master branch # checkout master branch
        uses: actions/checkout@v2
          persist-credentials: false
      - name: remove all files # remove files.
        run: |
          rm -rf *
      - name: checkout dev branch #checkout dev branch into temp folder.
        uses: actions/checkout@v2
          path: temp
          persist-credentials: false
      - name: run npm build # go to temp folder and run npm build to create files.
        run: |
          cd temp
          npm build
      - name: move files # move the dist files into root dir and remove others.
        run: |
          mv temp/dist/* ./
          rm -rf temp
      - name: Commit files for change # commit changes
        run: |
          git config --local ""
          git config --local "GitHub Action"
          git add .
          git commit -m "Add changes"
      - name: Push changes #push files into master branch
        uses: ad-m/github-push-action@master
          github_token: ${{ secrets.GITHUB_TOKEN }}
          force: true

Hope it helps!