Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add node:16 dynamic detection from package.json. if not defined node:14 is used #32

Merged
merged 5 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/tekton-pipelines/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.21
version: 0.1.23

maintainers:
- url: https://www.saritasa.com/
Expand Down
2 changes: 1 addition & 1 deletion charts/tekton-pipelines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ saritasa-tekton-pipelines

## `chart.version`

![Version: 0.1.21](https://img.shields.io/badge/Version-0.1.21-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square)
![Version: 0.1.23](https://img.shields.io/badge/Version-0.1.23-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square)

## Maintainers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ spec:
- name: source
workspace: source

- name: detect-nodejs-version
taskRef:
name: detect-nodejs-version
resources:
inputs:
- name: app
resource: app
params:
- name: default_version
value: "14"
runAfter:
- set-env-vars

- name: {{ .pipeline.buildTaskName }}
taskRef:
name: {{ .pipeline.buildTaskName }}
Expand Down Expand Up @@ -85,13 +98,15 @@ spec:
value: "$(params.source_subpath)"
- name: platform_dir
value: "$(params.platform_dir)"
- name: node_version
value: "$(tasks.detect-nodejs-version.results.node-version)"
- name: environment
value: "$(params.environment)"
workspaces:
- name: source
workspace: source
runAfter:
- set-env-vars
- detect-nodejs-version

- name: kustomize
taskRef:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ spec:
description: the name of the persistent app cache image.
default: ""

- name: node_version
type: string
description: nodejs version of the image used to build static
default: "14"

- name: platform_dir
type: string
description: the name of the platform directory.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: detect-nodejs-version
namespace: {{ .Release.Namespace }}
spec:
description: >-
Nodejs version detected from package.json

resources:
inputs:
- name: app
type: git

params:
- name: default_version
type: string
description: nodejs default version to be used if not detected in package.json

results:
- name: node-version
description: Nodejs version detected from package.json

steps:
- name: get-nodejs-version
image: cfmanteiga/alpine-bash-curl-jq
imagePullPolicy: {{ .Values.imagePullPolicy }}
workingDir: $(resources.inputs.app.path)
script: |
#!/usr/bin/env bash
set -Eeo pipefail

if [ ! -f package.json ]; then
echo "no package.json file found"
echo -n "$(params.default_version)" > $(results.node-version.path)
exit 0
fi

# if no engine is defined we use node:14 as the default image
node_ver=$(cat package.json | jq -r ".engines.node // 14" | sed 's/[\^>~=]//g')

# store in the results
# which we will use as the image:"node:version" in build task
# later in the pipeline
echo -n "$node_ver" > $(results.node-version.path)
echo "detected node version: $node_ver"
27 changes: 20 additions & 7 deletions charts/tekton-pipelines/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ buildpacks:
# @default -- see values.yaml for the default values of it
buildTaskSteps:
- name: build-static
image: node:16
image: node:$(params.node_version)
imagePullPolicy: IfNotPresent
resources: {}
workingDir: $(resources.inputs.app.path)
Expand All @@ -66,8 +66,12 @@ buildpacks:

[ -f package.json ] && {
echo "install node.js dependencies"
npm ci --silent
(npm run build:$(params.environment) && rm -rf node_modules && echo "remove node_modules dir") || (echo "unable to build $(params.environment)" && exit 1)

# set cache
npm config set cache $(workspaces.source.path)/.npm --global
npm ci
dmitry-mightydevops marked this conversation as resolved.
Show resolved Hide resolved

(npm run build:$(params.environment) && rm -rf node_modules && echo "remove node_modules dir") || (echo "unable to build $(params.environment) exit_code: $?" && exit 1)
cp -rf {project.toml,nginx.conf,buildpack.yml,nginx.d,.nginx.d,nginx.*,httpd.conf,.http.d,httpd.d} $(params.source_subpath) 2>/dev/null
chown -R $(params.user_id):$(params.group_id) $(params.source_subpath)
} || echo "No package.json found"
Expand Down Expand Up @@ -100,15 +104,20 @@ buildpacks:
# @default -- see values.yaml for the default values of it
buildTaskSteps:
- name: build-static
image: node:16
image: node:$(params.node_version)
imagePullPolicy: IfNotPresent
resources: {}
workingDir: $(resources.inputs.app.path)
script: |
#!/bin/bash
set -e
[ -f package.json ] && {
npm ci --silent
echo "install node.js dependencies"

# set cache
npm config set cache $(workspaces.source.path)/.npm --global
npm ci

npm run build:$(params.environment) || echo "unable to build $(params.environment)"
rm -rf node_modules && echo "remove node_modules dir"
} || echo "No package.json found"
Expand Down Expand Up @@ -221,7 +230,7 @@ buildpacks:
echo "semantic version updated"

- name: build-static
image: node:16
image: node:$(params.node_version)
imagePullPolicy: IfNotPresent
resources: {}
workingDir: $(resources.inputs.app.path)
Expand All @@ -248,7 +257,11 @@ buildpacks:
# if that folder has package.json then run build:ENV task
[ -f package.json ] && {
echo "install node.js dependencies"
npm ci --silent

# set cache
npm config set cache $(workspaces.source.path)/.npm --global
npm ci

(npm run build:$(params.environment) && rm -rf node_modules && echo "remove node_modules dir") || (echo "unable to build $(params.environment)" && exit 1)
chown -R "$(params.user_id):$(params.group_id)" "$(workspaces.source.path)"
chown -R "$(params.user_id):$(params.group_id)" "$(resources.inputs.app.path)"
Expand Down