|
1 | | -# Node.js with Angular |
2 | | -# Build a Node.js project that uses Angular. |
3 | | -# Add steps that analyze code, save build artifacts, deploy, and more: |
4 | | -# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript |
5 | | - |
| 1 | +# Node.js with Angular - staged pipeline (best-practice: npm ci per stage + npm cache) |
6 | 2 | trigger: |
7 | 3 | - main |
8 | 4 |
|
9 | | -pool: |
10 | | - vmImage: ubuntu-latest |
| 5 | +pr: |
| 6 | + branches: |
| 7 | + include: |
| 8 | + - main |
| 9 | + |
| 10 | +variables: |
| 11 | + nodeVersion: '20.x' |
| 12 | + npm_config_cache: $(Pipeline.Workspace)/.npm |
| 13 | + |
| 14 | +stages: |
| 15 | + - stage: Init |
| 16 | + displayName: 'Init' |
| 17 | + jobs: |
| 18 | + - job: connectivity |
| 19 | + displayName: 'Check Connectivity' |
| 20 | + pool: |
| 21 | + vmImage: ubuntu-latest |
| 22 | + steps: |
| 23 | + - task: NodeTool@0 |
| 24 | + inputs: |
| 25 | + versionSpec: '$(nodeVersion)' |
| 26 | + displayName: 'Use Node.js $(nodeVersion)' |
| 27 | + |
| 28 | + - script: | |
| 29 | + # Check GitHub connectivity |
| 30 | + echo "Checking GitHub connectivity..." |
| 31 | + curl -s -o /dev/null -w "GitHub Status: %{http_code}\n" https://api.github.com/ |
| 32 | +
|
| 33 | + # Check npm registry |
| 34 | + echo "Checking npm registry..." |
| 35 | + npm ping |
| 36 | +
|
| 37 | + # Show npm config |
| 38 | + echo "npm configuration:" |
| 39 | + npm config list |
| 40 | +
|
| 41 | + # Verify git remote |
| 42 | + echo "Git remote info:" |
| 43 | + git remote -v |
| 44 | + displayName: 'Check GitHub and npm connectivity' |
| 45 | +
|
| 46 | + - stage: Lint |
| 47 | + displayName: 'Lint' |
| 48 | + dependsOn: Init |
| 49 | + condition: succeeded() |
| 50 | + jobs: |
| 51 | + - job: lint |
| 52 | + displayName: 'Run lint' |
| 53 | + pool: |
| 54 | + vmImage: ubuntu-latest |
| 55 | + steps: |
| 56 | + - task: NodeTool@0 |
| 57 | + inputs: |
| 58 | + versionSpec: '$(nodeVersion)' |
| 59 | + displayName: 'Use Node.js $(nodeVersion)' |
| 60 | + |
| 61 | + - task: Cache@2 |
| 62 | + inputs: |
| 63 | + key: 'npm | "$(Agent.OS)" | package-lock.json' |
| 64 | + path: $(npm_config_cache) |
| 65 | + restoreKeys: | |
| 66 | + npm | "$(Agent.OS)" |
| 67 | + displayName: 'Cache npm' |
| 68 | + |
| 69 | + - script: npm ci |
| 70 | + displayName: 'Install dependencies (npm ci)' |
| 71 | + |
| 72 | + - script: | |
| 73 | + npx -y @angular/cli@$(nodeVersion) version || true |
| 74 | + npm run lint |
| 75 | + displayName: 'Run lint' |
| 76 | +
|
| 77 | + - stage: Test |
| 78 | + displayName: 'Test' |
| 79 | + dependsOn: Init |
| 80 | + condition: succeeded() |
| 81 | + jobs: |
| 82 | + - job: test |
| 83 | + displayName: 'Unit tests' |
| 84 | + pool: |
| 85 | + vmImage: ubuntu-latest |
| 86 | + steps: |
| 87 | + - task: NodeTool@0 |
| 88 | + inputs: |
| 89 | + versionSpec: '$(nodeVersion)' |
| 90 | + |
| 91 | + - task: Cache@2 |
| 92 | + inputs: |
| 93 | + key: 'npm | "$(Agent.OS)" | package-lock.json' |
| 94 | + path: $(npm_config_cache) |
| 95 | + restoreKeys: | |
| 96 | + npm | "$(Agent.OS)" |
| 97 | + displayName: 'Cache npm' |
| 98 | + |
| 99 | + - script: npm ci |
| 100 | + displayName: 'Install dependencies (npm ci)' |
| 101 | + |
| 102 | + - script: | |
| 103 | + # Run unit tests in headless Chrome and produce coverage |
| 104 | + npm run test-headless -- --no-watch --browsers=ChromeHeadless |
| 105 | + displayName: 'Run unit tests (headless)' |
| 106 | +
|
| 107 | + - stage: Build |
| 108 | + displayName: 'Build' |
| 109 | + dependsOn: |
| 110 | + - Lint |
| 111 | + - Test |
| 112 | + condition: succeeded() |
| 113 | + jobs: |
| 114 | + - job: build |
| 115 | + displayName: 'Build and publish' |
| 116 | + pool: |
| 117 | + vmImage: ubuntu-latest |
| 118 | + steps: |
| 119 | + - task: NodeTool@0 |
| 120 | + inputs: |
| 121 | + versionSpec: '$(nodeVersion)' |
| 122 | + |
| 123 | + - task: Cache@2 |
| 124 | + inputs: |
| 125 | + key: 'npm | "$(Agent.OS)" | package-lock.json' |
| 126 | + path: $(npm_config_cache) |
| 127 | + restoreKeys: | |
| 128 | + npm | "$(Agent.OS)" |
| 129 | + displayName: 'Cache npm' |
| 130 | + |
| 131 | + - script: npm ci |
| 132 | + displayName: 'Install dependencies (npm ci)' |
| 133 | + |
| 134 | + - script: npm run build -- --configuration production |
| 135 | + displayName: 'Build (production)' |
11 | 136 |
|
12 | | -steps: |
13 | | -- task: NodeTool@0 |
14 | | - inputs: |
15 | | - versionSpec: '20.x' |
16 | | - displayName: 'Install Node.js' |
| 137 | + - script: | |
| 138 | + mkdir -p "$(Build.ArtifactStagingDirectory)/dist" |
| 139 | + if [ -d dist/text-compare-angular ]; then |
| 140 | + cp -r dist/text-compare-angular/* "$(Build.ArtifactStagingDirectory)/dist/" |
| 141 | + else |
| 142 | + cp -r dist/* "$(Build.ArtifactStagingDirectory)/dist/" |
| 143 | + fi |
| 144 | + displayName: 'Stage build artifacts' |
17 | 145 |
|
18 | | -- script: | |
19 | | - npm install -g @angular/cli |
20 | | - npm install |
21 | | - npm run build |
22 | | - displayName: 'npm install and build' |
| 146 | + - task: PublishPipelineArtifact@1 |
| 147 | + inputs: |
| 148 | + targetPath: '$(Build.ArtifactStagingDirectory)/dist' |
| 149 | + artifact: 'app-dist' |
| 150 | + displayName: 'Publish pipeline artifact' |
0 commit comments