如何通过CircleCI部署Angular应用到GitHub Pages?执行ngh报错退出码1
Hey there, let's work through that frustrating "Exited with code 1" error you're hitting when using angular-cli-ghpages (ngh) to deploy your Angular app to GitHub Pages via CircleCI. I've debugged this exact issue a few times, so here are the most likely fixes and debugging steps:
1. Make sure CircleCI has proper repo permissions
- First up, confirm CircleCI has write access to your GitHub repository. Head to your CircleCI project settings, find the "Permissions" section, and ensure both "Checkout code" and "Write to repo" are enabled.
- If you're using a Personal Access Token (PAT) for authentication, double-check that the token has the
reposcope (includingpublic_repoandrepo:status) and that it's correctly set as an environment variable in CircleCI (usually namedGH_TOKENorGITHUB_TOKEN).
2. Validate your build and ngh command setup
- Before running
ngh, you must first build your Angular app for production with the correctbase-href(this matches your GitHub Pages repo name). For example, if your repo isusername/my-angular-app, your build command should look like:ng build --prod --base-href="/my-angular-app/" - When executing
ngh, specify the exact directory where your build output lives (you can find this path inangular.jsonunderprojects.[your-app-name].architect.build.options.outputPath). Add--no-silentor--verboseto get detailed logs:npx angular-cli-ghpages --dir=dist/your-app-name --verbose
3. Fix Git configuration gaps in CircleCI
- CircleCI's default checkout step doesn't set up Git user credentials, which
nghneeds to commit changes to thegh-pagesbranch. Add these commands before runningnghto configure Git:git config --global user.name "CircleCI" git config --global user.email "circleci@example.com" - Ensure your repo has a
gh-pagesbranch. If it doesn't, create it locally first and push it to GitHub, or letnghcreate it automatically (this requires write permissions).
4. Check for dependency version conflicts
- Outdated versions of
angular-cli-ghpagescan cause compatibility issues with newer Angular CLI versions. Update to the latest version:npm install angular-cli-ghpages@latest --save-dev - Verify that your Angular CLI version aligns with the
angular-cli-ghpagesdocs (Angular 15+ works best with the latestangular-cli-ghpagesreleases).
Full CircleCI Config Example
Here's a complete .circleci/config.yml that incorporates all these fixes:
version: 2.1 jobs: build-and-deploy: docker: - image: cimg/node:18.17.0 # Use a Node version compatible with your Angular app steps: - checkout - run: npm install - run: ng build --prod --base-href="/my-angular-app/" - run: | git config --global user.name "CircleCI" git config --global user.email "circleci@example.com" - run: npx angular-cli-ghpages --dir=dist/my-angular-app --verbose workflows: deploy-to-gh-pages: jobs: - build-and-deploy: filters: branches: only: main # Trigger deployment only from the main branch
If after trying these steps you still get the error, the --verbose flag will show exactly where the command is failing—whether it's a missing directory, Git authentication issue, or something else.
内容的提问来源于stack exchange,提问作者Nazar Kalytiuk




