feat(ensure-nodejs-fork): skip installing Node if already installed
This should help speed up builds.
Change-Id: Ia1548d32b001e9bffda93f4cfc83086a044ab484
diff --git a/roles/ensure-nodejs-fork/tasks/install-nodejs.yaml b/roles/ensure-nodejs-fork/tasks/install-nodejs.yaml
new file mode 100644
index 0000000..c97152f
--- /dev/null
+++ b/roles/ensure-nodejs-fork/tasks/install-nodejs.yaml
@@ -0,0 +1,64 @@
+- name: Update apt cache
+ apt:
+ update_cache: yes
+ become: yes
+
+- name: Install prereqs
+ package:
+ name: apt-transport-https
+ state: present
+ become: yes
+
+- name: Pin nodejs installs to nodesource
+ copy:
+ src: 00-nodesource.pref
+ dest: /etc/apt/preferences.d/00-nodesource.pref
+ mode: 0644
+ become: yes
+
+- name: Add all repositories
+ include_role:
+ name: ensure-package-repositories
+ vars:
+ repositories_keys:
+ - url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
+ repositories_list:
+ - repo: deb-src https://deb.nodesource.com/node_{{ node_version }}.x {{ ansible_distribution_release }} main
+ - repo: deb https://deb.nodesource.com/node_{{ node_version }}.x {{ ansible_distribution_release }} main
+ when: node_version | int < 16
+
+- name: Add all repositories
+ include_role:
+ name: ensure-package-repositories
+ vars:
+ repositories_keys:
+ - url: https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key
+ repositories_list:
+ - repo: deb https://deb.nodesource.com/node_{{ node_version }}.x nodistro main
+ when: node_version | int >= 16
+
+# Use template so that we can easily update this in the future to be able to
+# use a mirror location.
+- name: Pin NodeJS to nodesource apt repository
+ become: yes
+ template:
+ dest: /etc/apt/preferences.d/nodejs.pref
+ group: root
+ mode: 0644
+ owner: root
+ src: nodejs.pref.j2
+
+- name: Install NodeJS from nodesource
+ package:
+ name: nodejs
+ state: latest
+ become: yes
+ tags:
+ # Ignore ANSIBLE0010: We really want latest version
+ - skip_ansible_lint
+
+- name: Output node version
+ command: node --version
+
+- name: Output npm version
+ command: npm --version
diff --git a/roles/ensure-nodejs-fork/tasks/main.yaml b/roles/ensure-nodejs-fork/tasks/main.yaml
index 0437edc..0e6f5a9 100644
--- a/roles/ensure-nodejs-fork/tasks/main.yaml
+++ b/roles/ensure-nodejs-fork/tasks/main.yaml
@@ -2,67 +2,16 @@
assert:
that: node_version is defined
-- name: Update apt cache
- apt:
- update_cache: yes
- become: yes
-
-- name: Install prereqs
- package:
- name: apt-transport-https
- state: present
- become: yes
-
-- name: Pin nodejs installs to nodesource
- copy:
- src: 00-nodesource.pref
- dest: /etc/apt/preferences.d/00-nodesource.pref
- mode: 0644
- become: yes
-
-- name: Add all repositories
- include_role:
- name: ensure-package-repositories
- vars:
- repositories_keys:
- - url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
- repositories_list:
- - repo: deb-src https://deb.nodesource.com/node_{{ node_version }}.x {{ ansible_distribution_release }} main
- - repo: deb https://deb.nodesource.com/node_{{ node_version }}.x {{ ansible_distribution_release }} main
- when: node_version | int < 16
-
-- name: Add all repositories
- include_role:
- name: ensure-package-repositories
- vars:
- repositories_keys:
- - url: https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key
- repositories_list:
- - repo: deb https://deb.nodesource.com/node_{{ node_version }}.x nodistro main
- when: node_version | int >= 16
-
-# Use template so that we can easily update this in the future to be able to
-# use a mirror location.
-- name: Pin NodeJS to nodesource apt repository
- become: yes
- template:
- dest: /etc/apt/preferences.d/nodejs.pref
- group: root
- mode: 0644
- owner: root
- src: nodejs.pref.j2
-
-- name: Install NodeJS from nodesource
- package:
- name: nodejs
- state: latest
- become: yes
- tags:
- # Ignore ANSIBLE0010: We really want latest version
- - skip_ansible_lint
-
-- name: Output node version
+- name: Check installed Node version
command: node --version
+ register: node_installed_version
+ failed_when: false
-- name: Output npm version
- command: npm --version
+- name: Install NodeJS when it is not installed with the required version
+ include_tasks: install-nodejs.yaml
+ when:
+ - node_installed_version.rc != 0 or
+ (node_version | string | trim) !=
+ (node_installed_version.stdout
+ | regex_replace('^v(?P<majorVersion>[^.]+).*$', '\\g<majorVersion>')
+ | trim)