gitgithubgithub-actionssveltekitsveltekit-adapter-static

Workflow to build a SvelteKit SSG site into a different branch fails


I'm trying to get a GitHub Actions workflow to work with my SvelteKit site but keep running into an error. I am building a website using SvelteKit and SSG and I want to put my built site into a production branch in the same repo as the source code.

Here is my workflow:

name: Build and Deploy

on:
  push:
    branches: [ "sveltekit" ]
  workflow_dispatch:
  
permissions:
  contents: write
  
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
        with:
          ref: sveltekit

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 18.17.0

      - name: Install Dependencies
        run: npm install

      - name: Build
        run: npm run build

      - name: Deploy to production branch
        run: |
          git worktree add prod-branch production
          cp -r build/* /prod-branch/
          cd prod-branch
          git add .
          git commit -m "Deploying build artifacts to production"
          git push origin production

This successfully builds the project, and even creates the /prod-branch/ folder, but when it tries to check out the production branch into the /prod-branch/ folder, it errors out with

fatal: invalid reference: production
Error: Process completed with exit code 128.

This occurs on the "Deploy to production branch" step. The branch has already been created (local and remote) so I'm not sure why this would happen.


Solution

  • Finally got this to work using the following workflow:

    name: Build and Deploy
    
    # Controls when the workflow will run
    on:
      # Triggers the workflow on push or pull request events but only for the "main" branch
      push:
        paths-ignore:
        - '.github/**'
        branches: [ "sveltekit" ]
      
      # Allows you to run this workflow manually from the Actions tab
      workflow_dispatch:
      
    permissions:
      contents: write
      
    # A workflow run is made up of one or more jobs that can run sequentially or in parallel
    jobs:
      build-and-deploy:
        # The type of runner that the job will run on
        runs-on: ubuntu-latest
    
        # Steps represent a sequence of tasks that will be executed as part of the job
        steps:
          # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
          - uses: actions/checkout@v3
            with:
              ref: sveltekit
              
          - name: Setup Node.js
            uses: actions/setup-node@v4
            with:
              node-version: 18.17.0
    
          # Install Dependencies
          - name: Install Dependencies
            run: npm install
    
          # Build
          - name: Build
            run: npm run build
              
          # Deploy to Production branch
          - name: Deploy to production branch
            uses: peaceiris/actions-gh-pages@v3
            with:
              github_token: ${{ secrets.GITHUB_TOKEN }}
              publish_dir: ./build
              publish_branch: production