From 807748829cd8523b7a029491b00ea44f9dfa0f38 Mon Sep 17 00:00:00 2001 From: sinedied Date: Mon, 12 Apr 2021 08:18:43 +0000 Subject: [PATCH 1/4] feat: improve GitOps for Node.js --- .../.github/workflows/gitops.yml.mustache | 46 +++++++++++++++---- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/main/resources/nubesgen/.github/workflows/gitops.yml.mustache b/src/main/resources/nubesgen/.github/workflows/gitops.yml.mustache index d490eb795..4ec123be2 100644 --- a/src/main/resources/nubesgen/.github/workflows/gitops.yml.mustache +++ b/src/main/resources/nubesgen/.github/workflows/gitops.yml.mustache @@ -18,6 +18,12 @@ env: DOTNET_VERSION: '3.1' APP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root <%/runtimeDotnet%> +<%#runtimeNodejs%> + +env: + NODEJS_VERSION: '14' + APP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root +<%/runtimeNodejs%> jobs: manage-infrastructure: @@ -338,16 +344,15 @@ jobs: <%/applicationTypeFunction%> <%/runtimeDotnet%> <%#runtimeNodejs%> - build-and-deploy: - needs: manage-infrastructure + build: runs-on: ubuntu-20.04 steps: - name: Checkout code uses: actions/checkout@v2 - - name: Use Node.js + - name: Use Node.js ${{ env.NODEJS_VERSION }} uses: actions/setup-node@v2 with: - node-version: '14' + node-version: ${{ env.NODEJS_VERSION }} - name: Cache NPM packages uses: actions/cache@v2 with: @@ -355,16 +360,39 @@ jobs: key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.os }}-node- - - name: Install NPM + - name: Install NPM packages run: npm ci - name: Build the application - run: npm run build + run: npm run build --if-present - name: Run tests - run: npm run test + run: npm test --if-present + - name: Package application + run: | + PKG_FILE=$(npm pack ${{ env.APP_PACKAGE_PATH }} | tail -n1) + tar -xvf $PKG_FILE + cp -f ${{ env.APP_PACKAGE_PATH }}/package-lock.json package/ + cd package + npm ci --production + zip -r package.zip * + - name: Temporarily save package + uses: actions/upload-artifact@v2 + with: + name: package + path: './package' + retention-days: 1 + deploy: + needs: [manage-infrastructure, build] + runs-on: ubuntu-20.04 + steps: - name: Set environment variables run: | TAG_NAME=${GITHUB_REF#refs/*/} echo "ENVIRONMENT=${TAG_NAME:4}" >> $GITHUB_ENV + - name: Get built package + uses: actions/download-artifact@v2 + with: + name: package + path: ./package - name: Login to Azure uses: azure/login@v1 with: @@ -374,13 +402,13 @@ jobs: uses: azure/webapps-deploy@v2 with: app-name: app-<% applicationName %>-${{ env.ENVIRONMENT }}-001 - package: '.' + package: './package/package.zip' <%/applicationTypeAppService%> <%#applicationTypeFunction%> - name: 'Deploy to Azure Functions' uses: Azure/functions-action@v1 with: app-name: func-<% applicationName %>-${{ env.ENVIRONMENT }}-001 - package: '.' + package: './package/package.zip' <%/applicationTypeFunction%> <%/runtimeNodejs%> From 170198a5c4e099bb318a212d4f8f2824fb891dac Mon Sep 17 00:00:00 2001 From: sinedied Date: Mon, 12 Apr 2021 11:19:42 +0200 Subject: [PATCH 2/4] feat: add run from package for Node.js/app service --- .../nubesgen/terraform/modules/app-service/main.tf.mustache | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/resources/nubesgen/terraform/modules/app-service/main.tf.mustache b/src/main/resources/nubesgen/terraform/modules/app-service/main.tf.mustache index f0244188e..9c4dc1d3f 100644 --- a/src/main/resources/nubesgen/terraform/modules/app-service/main.tf.mustache +++ b/src/main/resources/nubesgen/terraform/modules/app-service/main.tf.mustache @@ -99,6 +99,10 @@ resource "azurerm_app_service" "application" { app_settings = { "WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false" +{{#runtimeNodejs}} + "WEBSITE_RUN_FROM_PACKAGE" = "1" + "WEBSITE_NODE_DEFAULT_VERSION" = "~14" +{{/runtimeNodejs}} {{#runtimeDocker}} "DOCKER_REGISTRY_SERVER_URL" = "https://${azurerm_container_registry.container-registry.name}.azurecr.io" "DOCKER_REGISTRY_SERVER_USERNAME" = azurerm_container_registry.container-registry.admin_username From 8601152faa599ab5c8157dd7bef4f3c3d9568940 Mon Sep 17 00:00:00 2001 From: sinedied Date: Tue, 20 Apr 2021 11:39:56 +0200 Subject: [PATCH 3/4] ci: fix tests --- .../.github/workflows/gitops.yml | 40 +++++++++++++++---- .../terraform/modules/app-service/main.tf | 2 + 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/test/resources/nubesgen/app-service-nodejs/.github/workflows/gitops.yml b/src/test/resources/nubesgen/app-service-nodejs/.github/workflows/gitops.yml index 8de5b918d..7228a50c5 100644 --- a/src/test/resources/nubesgen/app-service-nodejs/.github/workflows/gitops.yml +++ b/src/test/resources/nubesgen/app-service-nodejs/.github/workflows/gitops.yml @@ -12,6 +12,10 @@ on: branches: - 'env-*' +env: + NODEJS_VERSION: '14' + APP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root + jobs: manage-infrastructure: runs-on: ubuntu-20.04 @@ -54,15 +58,14 @@ jobs: -var="environment=$ENVIRONMENT" working-directory: '${{ github.workspace }}/terraform' build-and-deploy: - needs: manage-infrastructure runs-on: ubuntu-20.04 steps: - name: Checkout code uses: actions/checkout@v2 - - name: Use Node.js + - name: Use Node.js ${{ env.NODEJS_VERSION }} uses: actions/setup-node@v2 with: - node-version: '14' + node-version: ${{ env.NODEJS_VERSION }} - name: Cache NPM packages uses: actions/cache@v2 with: @@ -70,16 +73,39 @@ jobs: key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.os }}-node- - - name: Install NPM + - name: Install NPM packages run: npm ci - name: Build the application - run: npm run build + run: npm run build --if-present - name: Run tests - run: npm run test + run: npm run test --if-present + - name: Package application + run: | + PKG_FILE=$(npm pack ${{ env.APP_PACKAGE_PATH }} | tail -n1) + tar -xvf $PKG_FILE + cp -f ${{ env.APP_PACKAGE_PATH }}/package-lock.json package/ + cd package + npm ci --production + zip -r package.zip * + - name: Temporarily save package + uses: actions/upload-artifact@v2 + with: + name: package + path: './package' + retention-days: 1 + deploy: + needs: [manage-infrastructure, build] + runs-on: ubuntu-20.04 + steps: - name: Set environment variables run: | TAG_NAME=${GITHUB_REF#refs/*/} echo "ENVIRONMENT=${TAG_NAME:4}" >> $GITHUB_ENV + - name: Get built package + uses: actions/download-artifact@v2 + with: + name: package + path: ./package - name: Login to Azure uses: azure/login@v1 with: @@ -88,4 +114,4 @@ jobs: uses: azure/webapps-deploy@v2 with: app-name: app-nubesgen-testapp-app-service-nodejs-${{ env.ENVIRONMENT }}-001 - package: '.' + package: './package/package.zip' diff --git a/src/test/resources/nubesgen/app-service-nodejs/terraform/modules/app-service/main.tf b/src/test/resources/nubesgen/app-service-nodejs/terraform/modules/app-service/main.tf index a49b5e17b..887ad6942 100644 --- a/src/test/resources/nubesgen/app-service-nodejs/terraform/modules/app-service/main.tf +++ b/src/test/resources/nubesgen/app-service-nodejs/terraform/modules/app-service/main.tf @@ -40,6 +40,8 @@ resource "azurerm_app_service" "application" { app_settings = { "WEBSITES_ENABLE_APP_SERVICE_STORAGE" = "false" + "WEBSITE_RUN_FROM_PACKAGE" = "1" + "WEBSITE_NODE_DEFAULT_VERSION" = "~14" # These are app specific environment variables } From 68186d2882f2fc089a4e7092a810b75d8b60d024 Mon Sep 17 00:00:00 2001 From: sinedied Date: Tue, 20 Apr 2021 10:08:17 +0000 Subject: [PATCH 4/4] ci: fix tests --- .../nubesgen/app-service-nodejs/.github/workflows/gitops.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/resources/nubesgen/app-service-nodejs/.github/workflows/gitops.yml b/src/test/resources/nubesgen/app-service-nodejs/.github/workflows/gitops.yml index 7228a50c5..bfcdd5fae 100644 --- a/src/test/resources/nubesgen/app-service-nodejs/.github/workflows/gitops.yml +++ b/src/test/resources/nubesgen/app-service-nodejs/.github/workflows/gitops.yml @@ -57,7 +57,7 @@ jobs: -auto-approve \ -var="environment=$ENVIRONMENT" working-directory: '${{ github.workspace }}/terraform' - build-and-deploy: + build: runs-on: ubuntu-20.04 steps: - name: Checkout code @@ -78,7 +78,7 @@ jobs: - name: Build the application run: npm run build --if-present - name: Run tests - run: npm run test --if-present + run: npm test --if-present - name: Package application run: | PKG_FILE=$(npm pack ${{ env.APP_PACKAGE_PATH }} | tail -n1)