From a08002fd4468c3913e7a2786c89d7a0571060328 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Mon, 11 Dec 2017 11:44:22 -0600 Subject: [PATCH] Install yarn if needed in javascript jobs Some javascript projects use yarn for dependencies instead of raw npm. If a yarn.lock file is detected in the zuul_work_dir, install yarn and use yarn to install dependencies. Needed-By: I5864e1b2f9952fbc35b08ebdef348c4917564c37 Change-Id: I9ae7dd3b33b2b607c1c5a17632e2aeb12e339d15 --- playbooks/javascript/pre-test.yaml | 1 + playbooks/javascript/pre.yaml | 1 + .../install-javascript-packages/tasks/main.yaml | 17 +++++++++++++++++ roles/install-yarn/README.rst | 8 ++++++++ roles/install-yarn/defaults/main.yaml | 1 + roles/install-yarn/tasks/Debian.yaml | 14 ++++++++++++++ roles/install-yarn/tasks/RedHat.yaml | 8 ++++++++ roles/install-yarn/tasks/main.yaml | 15 +++++++++++++++ roles/install-yarn/tasks/yarn.yaml | 11 +++++++++++ 9 files changed, 76 insertions(+) create mode 100644 roles/install-yarn/README.rst create mode 100644 roles/install-yarn/defaults/main.yaml create mode 100644 roles/install-yarn/tasks/Debian.yaml create mode 100644 roles/install-yarn/tasks/RedHat.yaml create mode 100644 roles/install-yarn/tasks/main.yaml create mode 100644 roles/install-yarn/tasks/yarn.yaml diff --git a/playbooks/javascript/pre-test.yaml b/playbooks/javascript/pre-test.yaml index 0abfab9aa..7956fc181 100644 --- a/playbooks/javascript/pre-test.yaml +++ b/playbooks/javascript/pre-test.yaml @@ -5,6 +5,7 @@ bindep_dir: "{{ zuul_work_dir }}" - test-setup - install-nodejs + - install-yarn # nodejs-test-dependencies requires sudo, # so we cannot inherit pre.yaml from javascript-base. - nodejs-test-dependencies diff --git a/playbooks/javascript/pre.yaml b/playbooks/javascript/pre.yaml index 747029e83..4f6a0547a 100644 --- a/playbooks/javascript/pre.yaml +++ b/playbooks/javascript/pre.yaml @@ -5,5 +5,6 @@ bindep_dir: "{{ zuul_work_dir }}" - test-setup - install-nodejs + - install-yarn - revoke-sudo - install-javascript-packages diff --git a/roles/install-javascript-packages/tasks/main.yaml b/roles/install-javascript-packages/tasks/main.yaml index 2fbcdcd46..3c56b89f9 100644 --- a/roles/install-javascript-packages/tasks/main.yaml +++ b/roles/install-javascript-packages/tasks/main.yaml @@ -1,6 +1,23 @@ +- name: Check for yarn.lock file + stat: + path: "{{ zuul_work_dir }}/yarn.lock" + get_checksum: false + get_mime: false + get_md5: false + register: yarn_lock + +- name: Install yarn dependencies + command: yarn install + environment: + DISPLAY: ':99' + args: + chdir: "{{ zuul_work_dir }}" + when: yarn_lock.stat.exists + - name: Install npm dependencies command: npm install --verbose environment: DISPLAY: ':99' args: chdir: "{{ zuul_work_dir }}" + when: not yarn_lock.stat.exists diff --git a/roles/install-yarn/README.rst b/roles/install-yarn/README.rst new file mode 100644 index 000000000..0720e978a --- /dev/null +++ b/roles/install-yarn/README.rst @@ -0,0 +1,8 @@ +Install yarn from yarnpkg repos + +**Role Variables** + +.. zuul:rolevar:: zuul_work_dir + :default: {{ zuul.project.src_dir }} + + The directory to work in. diff --git a/roles/install-yarn/defaults/main.yaml b/roles/install-yarn/defaults/main.yaml new file mode 100644 index 000000000..9739eb171 --- /dev/null +++ b/roles/install-yarn/defaults/main.yaml @@ -0,0 +1 @@ +zuul_work_dir: "{{ zuul.project.src_dir }}" diff --git a/roles/install-yarn/tasks/Debian.yaml b/roles/install-yarn/tasks/Debian.yaml new file mode 100644 index 000000000..3559acfd5 --- /dev/null +++ b/roles/install-yarn/tasks/Debian.yaml @@ -0,0 +1,14 @@ +- name: Add yarnpkg repository key + apt_key: + url: https://dl.yarnpkg.com/debian/pubkey.gpg + become: yes + +- name: Add yarnpkg apt source repository + apt_repository: + repo: "deb https://dl.yarnpkg.com/debian/ stable main" + state: present + update_cache: yes + become: yes + +- name: Install yarn + include: yarn.yaml diff --git a/roles/install-yarn/tasks/RedHat.yaml b/roles/install-yarn/tasks/RedHat.yaml new file mode 100644 index 000000000..8065b7fdf --- /dev/null +++ b/roles/install-yarn/tasks/RedHat.yaml @@ -0,0 +1,8 @@ +- name: Add yarnpkg repo file + get_url: + url: https://dl.yarnpkg.com/rpm/yarn.repo + dest: /etc/yum.repos.d/yarn.repo + become: yes + +- name: Install yarn + include: yarn.yaml diff --git a/roles/install-yarn/tasks/main.yaml b/roles/install-yarn/tasks/main.yaml new file mode 100644 index 000000000..21e8348c7 --- /dev/null +++ b/roles/install-yarn/tasks/main.yaml @@ -0,0 +1,15 @@ +- name: Check for yarn.lock file + stat: + path: "{{ zuul_work_dir }}/yarn.lock" + get_checksum: false + get_mime: false + get_md5: false + register: yarn_lock + +- name: Install yarn if needed + include: "{{ item }}" + with_first_found: + - "{{ ansible_distribution }}.yaml" + - "{{ ansible_os_family }}.yaml" + when: + - yarn_lock.stat.exists diff --git a/roles/install-yarn/tasks/yarn.yaml b/roles/install-yarn/tasks/yarn.yaml new file mode 100644 index 000000000..b03565d57 --- /dev/null +++ b/roles/install-yarn/tasks/yarn.yaml @@ -0,0 +1,11 @@ +- name: Install yarn from yarnpkg.com + package: + name: yarn + state: latest + become: yes + tags: + # Ignore ANSIBLE0010: We really want latest version + - skip_ansible_lint + +- name: Output yarn version + command: yarn --version