|
| 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 | + WORKING_DIRECTORY: 'nodeapp-1' |
| 30 | + |
| 31 | +jobs: |
| 32 | + # ======================================== |
| 33 | + # JOB 1: BUILD AND TEST |
| 34 | + # ======================================== |
| 35 | + build: |
| 36 | + name: Build and Test |
| 37 | + runs-on: ubuntu-latest |
| 38 | + |
| 39 | + steps: |
| 40 | + - name: 📥 Checkout code |
| 41 | + uses: actions/checkout@v4 |
| 42 | + |
| 43 | + - name: 🔧 Setup Node.js ${{ env.NODE_VERSION }} |
| 44 | + uses: actions/setup-node@v4 |
| 45 | + with: |
| 46 | + node-version: ${{ env.NODE_VERSION }} |
| 47 | + cache: 'npm' |
| 48 | + cache-dependency-path: '${{ env.WORKING_DIRECTORY }}/package-lock.json' |
| 49 | + |
| 50 | + - name: 📦 Install dependencies |
| 51 | + run: npm ci |
| 52 | + working-directory: ${{ env.WORKING_DIRECTORY }} |
| 53 | + |
| 54 | + - name: 🔒 Security audit |
| 55 | + run: npm audit --audit-level=high |
| 56 | + working-directory: ${{ env.WORKING_DIRECTORY }} |
| 57 | + continue-on-error: true |
| 58 | + |
| 59 | + - name: 🧹 Lint code |
| 60 | + run: npm run lint |
| 61 | + working-directory: ${{ env.WORKING_DIRECTORY }} |
| 62 | + |
| 63 | + - name: 🧪 Run tests with Mocha |
| 64 | + run: npm test |
| 65 | + working-directory: ${{ env.WORKING_DIRECTORY }} |
| 66 | + |
| 67 | + - name: 📊 Upload test results |
| 68 | + uses: actions/upload-artifact@v4 |
| 69 | + if: always() |
| 70 | + with: |
| 71 | + name: mochawesome-report |
| 72 | + path: ${{ env.WORKING_DIRECTORY }}/mochawesome-report |
| 73 | + retention-days: 7 |
| 74 | + |
| 75 | + - name: 📤 Upload artifact for deployment |
| 76 | + uses: actions/upload-artifact@v4 |
| 77 | + with: |
| 78 | + name: node-app |
| 79 | + path: ${{ env.WORKING_DIRECTORY }} |
| 80 | + retention-days: 5 |
| 81 | + |
| 82 | + # ======================================== |
| 83 | + # JOB 2: DEPLOY TO DEVELOPMENT |
| 84 | + # ======================================== |
| 85 | + deploy-dev: |
| 86 | + name: Deploy to Development |
| 87 | + runs-on: ubuntu-latest |
| 88 | + needs: build |
| 89 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 90 | + environment: |
| 91 | + name: development |
| 92 | + url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} |
| 93 | + |
| 94 | + steps: |
| 95 | + - name: 📥 Download artifact |
| 96 | + uses: actions/download-artifact@v4 |
| 97 | + with: |
| 98 | + name: node-app |
| 99 | + path: ./app |
| 100 | + |
| 101 | + - name: 🔐 Login to Azure |
| 102 | + uses: azure/login@v1 |
| 103 | + with: |
| 104 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 105 | + |
| 106 | + - name: 🚀 Deploy to Azure Web App |
| 107 | + id: deploy-to-webapp |
| 108 | + uses: azure/webapps-deploy@v3 |
| 109 | + with: |
| 110 | + app-name: ${{ env.AZURE_WEBAPP_NAME }}-dev |
| 111 | + package: ./app |
| 112 | + startup-command: 'npm start' |
| 113 | + |
| 114 | + - name: 🔥 Smoke test |
| 115 | + run: | |
| 116 | + sleep 30 |
| 117 | + response=$(curl -s -o /dev/null -w "%{http_code}" https://${{ env.AZURE_WEBAPP_NAME }}-dev.azurewebsites.net) |
| 118 | + if [ $response -eq 200 ]; then |
| 119 | + echo "✅ Smoke test passed!" |
| 120 | + else |
| 121 | + echo "❌ Smoke test failed with status code: $response" |
| 122 | + exit 1 |
| 123 | + fi |
| 124 | +
|
| 125 | + # ======================================== |
| 126 | + # JOB 3: DEPLOY TO STAGING |
| 127 | + # ======================================== |
| 128 | + deploy-staging: |
| 129 | + name: Deploy to Staging |
| 130 | + runs-on: ubuntu-latest |
| 131 | + needs: deploy-dev |
| 132 | + environment: |
| 133 | + name: staging |
| 134 | + url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} |
| 135 | + |
| 136 | + steps: |
| 137 | + - name: 📥 Checkout for tests |
| 138 | + uses: actions/checkout@v4 |
| 139 | + |
| 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 | + cd ${{ env.WORKING_DIRECTORY }} |
| 162 | + npm ci |
| 163 | + export TEST_URL=https://${{ env.AZURE_WEBAPP_NAME }}-staging.azurewebsites.net |
| 164 | + npm test |
| 165 | + continue-on-error: true |
| 166 | + |
| 167 | + # ======================================== |
| 168 | + # JOB 4: DEPLOY TO PRODUCTION |
| 169 | + # ======================================== |
| 170 | + deploy-prod: |
| 171 | + name: Deploy to Production |
| 172 | + runs-on: ubuntu-latest |
| 173 | + needs: deploy-staging |
| 174 | + environment: |
| 175 | + name: production |
| 176 | + url: ${{ steps.swap-slots.outputs.webapp-url }} |
| 177 | + |
| 178 | + steps: |
| 179 | + - name: 🔐 Login to Azure |
| 180 | + uses: azure/login@v1 |
| 181 | + with: |
| 182 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 183 | + |
| 184 | + - name: 🔄 Swap staging to production |
| 185 | + id: swap-slots |
| 186 | + run: | |
| 187 | + az webapp deployment slot swap \ |
| 188 | + --resource-group rg-az400-demo \ |
| 189 | + --name ${{ env.AZURE_WEBAPP_NAME }} \ |
| 190 | + --slot staging \ |
| 191 | + --target-slot production |
| 192 | + |
| 193 | + echo "webapp-url=https://${{ env.AZURE_WEBAPP_NAME }}.azurewebsites.net" >> $GITHUB_OUTPUT |
| 194 | +
|
| 195 | + - name: 🏷️ Create release tag |
| 196 | + if: success() |
| 197 | + uses: actions/github-script@v7 |
| 198 | + with: |
| 199 | + script: | |
| 200 | + const tag = `v${context.runNumber}`; |
| 201 | + await github.rest.git.createRef({ |
| 202 | + owner: context.repo.owner, |
| 203 | + repo: context.repo.repo, |
| 204 | + ref: `refs/tags/${tag}`, |
| 205 | + sha: context.sha |
| 206 | + }); |
| 207 | + console.log(`✅ Created tag: ${tag}`); |
| 208 | +
|
| 209 | +# ======================================== |
| 210 | +# REUSABLE WORKFLOW: SECURITY SCAN |
| 211 | +# ======================================== |
| 212 | + security-scan: |
| 213 | + name: Security Scanning |
| 214 | + runs-on: ubuntu-latest |
| 215 | + if: github.event_name == 'pull_request' |
| 216 | + |
| 217 | + steps: |
| 218 | + - name: 📥 Checkout code |
| 219 | + uses: actions/checkout@v4 |
| 220 | + |
| 221 | + - name: 🔍 Run Dependabot scan |
| 222 | + uses: github/super-linter@v5 |
| 223 | + env: |
| 224 | + DEFAULT_BRANCH: main |
| 225 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 226 | + VALIDATE_JAVASCRIPT_ES: true |
| 227 | + VALIDATE_JSON: true |
| 228 | + VALIDATE_YAML: true |
0 commit comments