|
| 1 | +name: Node.js CI/CD to Azure |
| 2 | + |
| 3 | +# Teaching example for GitHub Actions with Azure deployment |
| 4 | + |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: [ main, master ] |
| 8 | + paths: |
| 9 | + - 'nodeapp-1/**' |
| 10 | + - '.github/workflows/node-azure-deploy.yml' |
| 11 | + pull_request: |
| 12 | + branches: [ main, master ] |
| 13 | + workflow_dispatch: |
| 14 | + inputs: |
| 15 | + environment: |
| 16 | + description: 'Environment to deploy to' |
| 17 | + required: true |
| 18 | + default: 'development' |
| 19 | + type: choice |
| 20 | + options: |
| 21 | + - development |
| 22 | + - staging |
| 23 | + - production |
| 24 | + |
| 25 | +env: |
| 26 | + NODE_VERSION: '18.x' |
| 27 | + AZURE_WEBAPP_NAME: 'webapp-az400-demo' |
| 28 | + AZURE_WEBAPP_PACKAGE_PATH: './nodeapp-1' |
| 29 | + |
| 30 | +jobs: |
| 31 | + # ======================================== |
| 32 | + # JOB 1: BUILD AND TEST |
| 33 | + # ======================================== |
| 34 | + build: |
| 35 | + name: Build and Test |
| 36 | + runs-on: ubuntu-latest |
| 37 | + |
| 38 | + steps: |
| 39 | + - name: 📥 Checkout code |
| 40 | + uses: actions/checkout@v4 |
| 41 | + |
| 42 | + - name: 🔧 Setup Node.js ${{ env.NODE_VERSION }} |
| 43 | + uses: actions/setup-node@v4 |
| 44 | + with: |
| 45 | + node-version: ${{ env.NODE_VERSION }} |
| 46 | + cache: 'npm' |
| 47 | + cache-dependency-path: '${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/package-lock.json' |
| 48 | + |
| 49 | + - name: 📦 Install dependencies |
| 50 | + run: npm ci |
| 51 | + working-directory: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }} |
| 52 | + |
| 53 | + - name: 🔒 Security audit |
| 54 | + run: npm audit --audit-level=high |
| 55 | + working-directory: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }} |
| 56 | + continue-on-error: true |
| 57 | + |
| 58 | + - name: 🧹 Lint code |
| 59 | + run: npm run lint || echo "No lint script configured" |
| 60 | + working-directory: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }} |
| 61 | + |
| 62 | + - name: 🧪 Run tests |
| 63 | + run: npm test -- --coverage || npm test |
| 64 | + working-directory: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }} |
| 65 | + |
| 66 | + - name: 📊 Upload coverage reports |
| 67 | + uses: codecov/codecov-action@v3 |
| 68 | + with: |
| 69 | + directory: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}/coverage |
| 70 | + flags: unittests |
| 71 | + name: codecov-umbrella |
| 72 | + fail_ci_if_error: false |
| 73 | + |
| 74 | + - name: 🔨 Build application |
| 75 | + run: npm run build || echo "No build required" |
| 76 | + working-directory: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }} |
| 77 | + |
| 78 | + - name: 📤 Upload artifact for deployment |
| 79 | + uses: actions/upload-artifact@v4 |
| 80 | + with: |
| 81 | + name: node-app |
| 82 | + path: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }} |
| 83 | + retention-days: 5 |
| 84 | + |
| 85 | + # ======================================== |
| 86 | + # JOB 2: DEPLOY TO DEVELOPMENT |
| 87 | + # ======================================== |
| 88 | + deploy-dev: |
| 89 | + name: Deploy to Development |
| 90 | + runs-on: ubuntu-latest |
| 91 | + needs: build |
| 92 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 93 | + environment: |
| 94 | + name: development |
| 95 | + url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} |
| 96 | + |
| 97 | + steps: |
| 98 | + - name: 📥 Download artifact |
| 99 | + uses: actions/download-artifact@v4 |
| 100 | + with: |
| 101 | + name: node-app |
| 102 | + path: ./app |
| 103 | + |
| 104 | + - name: 🔐 Login to Azure |
| 105 | + uses: azure/login@v1 |
| 106 | + with: |
| 107 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 108 | + |
| 109 | + - name: 🚀 Deploy to Azure Web App |
| 110 | + id: deploy-to-webapp |
| 111 | + uses: azure/webapps-deploy@v3 |
| 112 | + with: |
| 113 | + app-name: ${{ env.AZURE_WEBAPP_NAME }}-dev |
| 114 | + package: ./app |
| 115 | + startup-command: 'npm start' |
| 116 | + |
| 117 | + - name: 🔥 Smoke test |
| 118 | + run: | |
| 119 | + sleep 30 |
| 120 | + response=$(curl -s -o /dev/null -w "%{http_code}" https://${{ env.AZURE_WEBAPP_NAME }}-dev.azurewebsites.net) |
| 121 | + if [ $response -eq 200 ]; then |
| 122 | + echo "✅ Smoke test passed!" |
| 123 | + else |
| 124 | + echo "❌ Smoke test failed with status code: $response" |
| 125 | + exit 1 |
| 126 | + fi |
| 127 | +
|
| 128 | + # ======================================== |
| 129 | + # JOB 3: DEPLOY TO STAGING |
| 130 | + # ======================================== |
| 131 | + deploy-staging: |
| 132 | + name: Deploy to Staging |
| 133 | + runs-on: ubuntu-latest |
| 134 | + needs: deploy-dev |
| 135 | + environment: |
| 136 | + name: staging |
| 137 | + url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} |
| 138 | + |
| 139 | + steps: |
| 140 | + - name: 📥 Download artifact |
| 141 | + uses: actions/download-artifact@v4 |
| 142 | + with: |
| 143 | + name: node-app |
| 144 | + path: ./app |
| 145 | + |
| 146 | + - name: 🔐 Login to Azure |
| 147 | + uses: azure/login@v1 |
| 148 | + with: |
| 149 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 150 | + |
| 151 | + - name: 🚀 Deploy to staging slot |
| 152 | + id: deploy-to-webapp |
| 153 | + uses: azure/webapps-deploy@v3 |
| 154 | + with: |
| 155 | + app-name: ${{ env.AZURE_WEBAPP_NAME }} |
| 156 | + slot-name: staging |
| 157 | + package: ./app |
| 158 | + |
| 159 | + - name: 🧪 Run integration tests |
| 160 | + run: | |
| 161 | + echo "Running integration tests against staging..." |
| 162 | + # Add your integration test commands here |
| 163 | + echo "✅ Integration tests passed!" |
| 164 | +
|
| 165 | + # ======================================== |
| 166 | + # JOB 4: DEPLOY TO PRODUCTION |
| 167 | + # ======================================== |
| 168 | + deploy-prod: |
| 169 | + name: Deploy to Production |
| 170 | + runs-on: ubuntu-latest |
| 171 | + needs: deploy-staging |
| 172 | + environment: |
| 173 | + name: production |
| 174 | + url: ${{ steps.swap-slots.outputs.webapp-url }} |
| 175 | + |
| 176 | + steps: |
| 177 | + - name: 🔐 Login to Azure |
| 178 | + uses: azure/login@v1 |
| 179 | + with: |
| 180 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 181 | + |
| 182 | + - name: 🔄 Swap staging to production |
| 183 | + id: swap-slots |
| 184 | + run: | |
| 185 | + az webapp deployment slot swap \ |
| 186 | + --resource-group rg-az400-demo \ |
| 187 | + --name ${{ env.AZURE_WEBAPP_NAME }} \ |
| 188 | + --slot staging \ |
| 189 | + --target-slot production |
| 190 | + |
| 191 | + echo "webapp-url=https://${{ env.AZURE_WEBAPP_NAME }}.azurewebsites.net" >> $GITHUB_OUTPUT |
| 192 | +
|
| 193 | + - name: 🏷️ Create release tag |
| 194 | + if: success() |
| 195 | + uses: actions/github-script@v7 |
| 196 | + with: |
| 197 | + script: | |
| 198 | + const tag = `v${context.runNumber}`; |
| 199 | + await github.rest.git.createRef({ |
| 200 | + owner: context.repo.owner, |
| 201 | + repo: context.repo.repo, |
| 202 | + ref: `refs/tags/${tag}`, |
| 203 | + sha: context.sha |
| 204 | + }); |
| 205 | + console.log(`✅ Created tag: ${tag}`); |
| 206 | +
|
| 207 | + - name: 📊 Monitor deployment |
| 208 | + run: | |
| 209 | + echo "Monitoring production deployment..." |
| 210 | + # Add Application Insights queries here |
| 211 | + echo "✅ Production deployment healthy!" |
| 212 | +
|
| 213 | +# ======================================== |
| 214 | +# REUSABLE WORKFLOW: SECURITY SCAN |
| 215 | +# ======================================== |
| 216 | + security-scan: |
| 217 | + name: Security Scanning |
| 218 | + runs-on: ubuntu-latest |
| 219 | + if: github.event_name == 'pull_request' |
| 220 | + |
| 221 | + steps: |
| 222 | + - name: 📥 Checkout code |
| 223 | + uses: actions/checkout@v4 |
| 224 | + |
| 225 | + - name: 🔍 Run CodeQL Analysis |
| 226 | + uses: github/codeql-action/analyze@v2 |
| 227 | + with: |
| 228 | + languages: javascript |
| 229 | + |
| 230 | + - name: 🛡️ Run Dependabot scan |
| 231 | + uses: github/super-linter@v5 |
| 232 | + env: |
| 233 | + DEFAULT_BRANCH: main |
| 234 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 235 | + VALIDATE_JAVASCRIPT_ES: true |
| 236 | + VALIDATE_JSON: true |
| 237 | + VALIDATE_YAML: true |
0 commit comments