So what we're gonna doing is, create a Github workflow and build our Sveltekit app and store that build output to a artifact then deploy from that build. Let's see step-by-step.
I assume you already have a Sveltekit app ready to deploy or test. If you dont, create a new one.
Since Github-Pages is a static site hosting solution, we need to use a static adapter.
bash
npm i -D @sveltejs/adapter-static
And update svelte.config.js with new adapter.
svelte.config.js
import adapter from'@sveltejs/adapter-static';
import { vitePreprocess } from'@sveltejs/vite-plugin-svelte';
/** @type {import('@sveltejs/kit').Config} */const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors// for more information about preprocessorspreprocess: vitePreprocess(),
kit: {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.// If your environment is not supported, or you settled on a specific environment, switch out the adapter.// See https://kit.svelte.dev/docs/adapters for more information about adapters.adapter: adapter(),
},
};
exportdefault config;
There are a couple of ways to do this, but instead doing this manually every time, we're going to utilize Github actions. With a simple workflow, we can trigger a deployment every time a commit is made to the main branch.
NOTE: In this workflow, my Sveltekit app ain't on then root dir, so I'm changing directory to where it is. You can remove it, if your app is in same level as .github folder.
.github/workflows/pages.yml
name:DeploytoGitHubPageson:push:branches:'main'jobs:build:runs-on:ubuntu-lateststeps:-name:Checkoutuses:actions/checkout@v4-name:SetupNode.jsuses:actions/setup-node@v4with:node-version:20cache:npmcache-dependency-path:web/package-lock.json-name:Installdependenciesrun:|
cd web
npm install
-name:buildenv:BASE_PATH:'/${{ github.event.repository.name }}'run:|
cd web
npm run build
-name:UploadArtifactsuses:actions/upload-pages-artifact@v3with:path:'web/build/'deploy:needs:buildruns-on:ubuntu-latestpermissions:pages:writeid-token:writeenvironment:name:github-pagesurl:${{steps.deployment.outputs.page_url}}steps:-name:Deployid:deploymentuses:actions/deploy-pages@v4
Now push this to repository. Congrats, you made your first deployment.