0

I did everything according to all the tutorials and articles on how to deploy a Vue app to GitHub, but when I run npm run deploy, I keep getting this error: error: src refspec main does not match any, and all the articles I could find online are about this issue being caused for the master branch, and the solutions are all to just create a main branch, but I do have a main branch, and I do have it on my remote too.

Why do I keep getting this error? I have a vue.config.js file, and inside, I have this:

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true
})

module.exports = {
  publicPath: process.env.NODE_ENV === 'production'
  ? '/todo-list/'
  : '/'
}

The first part was there by default when the app was created, so I haven't removed it.

I also have a deploy.sh file, and in it, I have this:

#!/usr/bin/env sh
# abort on errors
set -e
# build
npm run build
# navigate into the build output directory
cd dist
# if you are deploying to a custom domain
# echo 'www.example.com' > CNAME
git init
git add -A
git commit -m 'deploy'
git push -f git@github.com:myname/todo-list.git main:gh-pages
cd -

The only thing I don't have is gh-pages branch, but that's because:

  1. I though this file is supposed to create one automatically for me with the files from the /dist folder.
  2. If I create one from the main branch, it will just contain all the files from main, and as far as I know, that's not supposed to happen, because the gh-pages is supposed to contain only the build for production files.

What am I doing wrong?

kissu
  • 40,416
  • 14
  • 65
  • 133
happy_story
  • 1
  • 1
  • 7
  • 17
  • You can deploy to pages using actions since recently so there is no need to build offline and abuse git to store the artifacts on a special branch. – mousetail Sep 16 '22 at 07:14
  • @mousetail There's just one little problem with that. I don't know how to do it! Care to explain? – happy_story Sep 16 '22 at 07:16
  • No, that's too big for this site. Github has example projects for most mainstream libraries though including vue. – mousetail Sep 16 '22 at 07:17
  • @mousetail You're not really helping me. – happy_story Sep 16 '22 at 07:20
  • I tried to deploy a Vue app with Vue router recently to GH pages. TLDR: it's quite tricky, hacky, suboptimal and far too complex for something that simple in 2022. My recommendation? Ship your app on Netlify or Vercel, you will get far better results + more features + still for free. Don't waste your time trying to deploy down there. – kissu Sep 16 '22 at 07:27
  • @kissu I tried deploying it to Netlify, but.. 1) for some reason Netlify deployment isn't working right now. I keep getting this error `We’re having some trouble connecting you to Netlify`, and 2) I need to have the code on Github, because I need to show it to colleagues, and plus, Github is what we use in the company. But yeah, I hate Github too :D – happy_story Sep 16 '22 at 07:30
  • Netlify is working properly as of right now: https://www.netlifystatus.com/ So it may be an issue with your account or alike. Try to maybe clean your cookies/try in private window. Your code will be on Github, just the deployed final build will be on Netlify. When you create a PR, you will have access to the preview environment too, without even needing to give access to the Netlify account. As I told, Netlify is just a 10x more powerful solution with basically no downside compared to GH pages. Otherwise, you can also try Vercel. – kissu Sep 16 '22 at 07:34
  • @kissu I think the issue with Netlify is the CORS policy. It keeps getting blocked. I don't remember having this issue before when deploying static apps to Netlify. Any idea how to fix this on Vue? – happy_story Sep 16 '22 at 08:04
  • The error about `src refspec main does not match any` is literally a trivial Git duplicate (which I guess you also noted). I'm going with [this one](https://stackoverflow.com/q/65173291/1256452) because it has one of my long-ish answers. What's not clear is why `npm run deploy` is doing this: it seems like npm should figure out the correct source branch, or the article you're reading should have warned you about the master/main thing, or something ... and you say you *have* a `main`! – torek Sep 16 '22 at 08:06
  • CORS issues are not related to the hosting platform and you could have that pretty much anywhere when working with a frontend + API. What you need to do is to configure your backend/backoffice to allow the frontend to consume it as [explained here](https://stackoverflow.com/a/72211930/8816585). – kissu Sep 16 '22 at 08:08
  • Total guess: `npm run deploy` creates a new Git repository, fails to fix its master/main issue, and then tries to `git push origin main:gh-pages` (as in that script you showed) so the fact that *you* have a `main` isn't relevant, it's the deploy script screwing it up. – torek Sep 16 '22 at 08:10
  • Script that you could just ignore and move along since it's causing so much issues anyway. We're in 2022, enough time wasted trying to make GH-pages work IMHO. Shipping an SPA is simple nowadays. – kissu Sep 16 '22 at 08:11
  • @kissu I don't have a backend. I only have one static page. I don't make any fetch requests, but then again, Vue does, right? – happy_story Sep 16 '22 at 08:20
  • @torek Any ideas how to fix it? – happy_story Sep 16 '22 at 08:20
  • If you have a modern-enough Git, you can configure it to create new repositories with branch name `main` instead of `master`. If not, find the broken script and kill fix it. – torek Sep 16 '22 at 08:22
  • Where is your CORS issue coming from so? Check your network tab. "I don't make any fetch requests, but then again, Vue does", what does this sentence mean? Do you use fetch/axios to call some API? CORS issue so (because there is a backend on the other side of your query). If you don't, then you will need to provide us more details on exactly what is not working. Also, a [repro] or even a public github repo is also welcome to fix that faster. – kissu Sep 16 '22 at 08:25
  • @kissu `what does this sentence mean?` it means that Vue makes fetch requests to their server every time the DOM has been updated , so that could also be blocked by CORS. No, I don't make any fetch requests myself. But don't worry about it, I already fixed it! I found out how to upload it to both Github pages, and Github actions too. Here's the deployed app: https://tishoyanchev.github.io/todo-list/ – happy_story Sep 16 '22 at 11:57

0 Answers0