Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit a9901e3

Browse files
authored
Merge pull request #88 from microsoft/nodejs-ops
feat: improve GitOps for Node.js
2 parents 3d7ed23 + 68186d2 commit a9901e3

4 files changed

Lines changed: 77 additions & 17 deletions

File tree

src/main/resources/nubesgen/.github/workflows/gitops.yml.mustache

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ env:
1818
DOTNET_VERSION: '3.1'
1919
APP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
2020
<%/runtimeDotnet%>
21+
<%#runtimeNodejs%>
22+
23+
env:
24+
NODEJS_VERSION: '14'
25+
APP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
26+
<%/runtimeNodejs%>
2127
<%#runtimeDocker%>
2228

2329
env:
@@ -321,33 +327,55 @@ jobs:
321327
<%/applicationTypeFunction%>
322328
<%/runtimeDotnet%>
323329
<%#runtimeNodejs%>
324-
build-and-deploy:
325-
needs: manage-infrastructure
330+
build:
326331
runs-on: ubuntu-20.04
327332
steps:
328333
- name: Checkout code
329334
uses: actions/checkout@v2
330-
- name: Use Node.js
335+
- name: Use Node.js ${{ env.NODEJS_VERSION }}
331336
uses: actions/setup-node@v2
332337
with:
333-
node-version: '14'
338+
node-version: ${{ env.NODEJS_VERSION }}
334339
- name: Cache NPM packages
335340
uses: actions/cache@v2
336341
with:
337342
path: ~/.npm
338343
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
339344
restore-keys: |
340345
${{ runner.os }}-node-
341-
- name: Install NPM
346+
- name: Install NPM packages
342347
run: npm ci
343348
- name: Build the application
344-
run: npm run build
349+
run: npm run build --if-present
345350
- name: Run tests
346-
run: npm run test
351+
run: npm test --if-present
352+
- name: Package application
353+
run: |
354+
PKG_FILE=$(npm pack ${{ env.APP_PACKAGE_PATH }} | tail -n1)
355+
tar -xvf $PKG_FILE
356+
cp -f ${{ env.APP_PACKAGE_PATH }}/package-lock.json package/
357+
cd package
358+
npm ci --production
359+
zip -r package.zip *
360+
- name: Temporarily save package
361+
uses: actions/upload-artifact@v2
362+
with:
363+
name: package
364+
path: './package'
365+
retention-days: 1
366+
deploy:
367+
needs: [manage-infrastructure, build]
368+
runs-on: ubuntu-20.04
369+
steps:
347370
- name: Set environment variables
348371
run: |
349372
TAG_NAME=${GITHUB_REF#refs/*/}
350373
echo "ENVIRONMENT=${TAG_NAME:4}" >> $GITHUB_ENV
374+
- name: Get built package
375+
uses: actions/download-artifact@v2
376+
with:
377+
name: package
378+
path: ./package
351379
- name: Login to Azure
352380
uses: azure/login@v1
353381
with:
@@ -357,13 +385,13 @@ jobs:
357385
uses: azure/webapps-deploy@v2
358386
with:
359387
app-name: app-<% applicationName %>-${{ env.ENVIRONMENT }}-001
360-
package: '.'
388+
package: './package/package.zip'
361389
<%/applicationTypeAppService%>
362390
<%#applicationTypeFunction%>
363391
- name: 'Deploy to Azure Functions'
364392
uses: Azure/functions-action@v1
365393
with:
366394
app-name: func-<% applicationName %>-${{ env.ENVIRONMENT }}-001
367-
package: '.'
395+
package: './package/package.zip'
368396
<%/applicationTypeFunction%>
369397
<%/runtimeNodejs%>

src/main/resources/nubesgen/terraform/modules/app-service/main.tf.mustache

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ resource "azurerm_app_service" "application" {
9999

100100
app_settings = {
101101
"WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false"
102+
{{#runtimeNodejs}}
103+
"WEBSITE_RUN_FROM_PACKAGE" = "1"
104+
"WEBSITE_NODE_DEFAULT_VERSION" = "~14"
105+
{{/runtimeNodejs}}
102106
{{#runtimeDocker}}
103107
"DOCKER_REGISTRY_SERVER_URL" = "https://${azurerm_container_registry.container-registry.name}.azurecr.io"
104108
"DOCKER_REGISTRY_SERVER_USERNAME" = azurerm_container_registry.container-registry.admin_username

src/test/resources/nubesgen/app-service-nodejs/.github/workflows/gitops.yml

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ on:
1212
branches:
1313
- 'env-*'
1414

15+
env:
16+
NODEJS_VERSION: '14'
17+
APP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
18+
1519
jobs:
1620
manage-infrastructure:
1721
runs-on: ubuntu-20.04
@@ -53,33 +57,55 @@ jobs:
5357
-auto-approve \
5458
-var="environment=$ENVIRONMENT"
5559
working-directory: '${{ github.workspace }}/terraform'
56-
build-and-deploy:
57-
needs: manage-infrastructure
60+
build:
5861
runs-on: ubuntu-20.04
5962
steps:
6063
- name: Checkout code
6164
uses: actions/checkout@v2
62-
- name: Use Node.js
65+
- name: Use Node.js ${{ env.NODEJS_VERSION }}
6366
uses: actions/setup-node@v2
6467
with:
65-
node-version: '14'
68+
node-version: ${{ env.NODEJS_VERSION }}
6669
- name: Cache NPM packages
6770
uses: actions/cache@v2
6871
with:
6972
path: ~/.npm
7073
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
7174
restore-keys: |
7275
${{ runner.os }}-node-
73-
- name: Install NPM
76+
- name: Install NPM packages
7477
run: npm ci
7578
- name: Build the application
76-
run: npm run build
79+
run: npm run build --if-present
7780
- name: Run tests
78-
run: npm run test
81+
run: npm test --if-present
82+
- name: Package application
83+
run: |
84+
PKG_FILE=$(npm pack ${{ env.APP_PACKAGE_PATH }} | tail -n1)
85+
tar -xvf $PKG_FILE
86+
cp -f ${{ env.APP_PACKAGE_PATH }}/package-lock.json package/
87+
cd package
88+
npm ci --production
89+
zip -r package.zip *
90+
- name: Temporarily save package
91+
uses: actions/upload-artifact@v2
92+
with:
93+
name: package
94+
path: './package'
95+
retention-days: 1
96+
deploy:
97+
needs: [manage-infrastructure, build]
98+
runs-on: ubuntu-20.04
99+
steps:
79100
- name: Set environment variables
80101
run: |
81102
TAG_NAME=${GITHUB_REF#refs/*/}
82103
echo "ENVIRONMENT=${TAG_NAME:4}" >> $GITHUB_ENV
104+
- name: Get built package
105+
uses: actions/download-artifact@v2
106+
with:
107+
name: package
108+
path: ./package
83109
- name: Login to Azure
84110
uses: azure/login@v1
85111
with:
@@ -88,4 +114,4 @@ jobs:
88114
uses: azure/webapps-deploy@v2
89115
with:
90116
app-name: app-nubesgen-testapp-app-service-nodejs-${{ env.ENVIRONMENT }}-001
91-
package: '.'
117+
package: './package/package.zip'

src/test/resources/nubesgen/app-service-nodejs/terraform/modules/app-service/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ resource "azurerm_app_service" "application" {
4040

4141
app_settings = {
4242
"WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false"
43+
"WEBSITE_RUN_FROM_PACKAGE" = "1"
44+
"WEBSITE_NODE_DEFAULT_VERSION" = "~14"
4345

4446
# These are app specific environment variables
4547
}

0 commit comments

Comments
 (0)