From 8236df604f7284b2a6f6104c468bf20d80b5fc97 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Sun, 12 Jul 2020 08:46:18 -0500 Subject: [PATCH] Add job for publishing to netlify Change-Id: I56707516a00d7b7a2869bde53d025e01cb1376cf --- .zuul.yaml | 90 +++++++++++++++++-- playbooks/netlify-publish.yaml | 3 + roles/netlify-publish/README.rst | 24 +++++ roles/netlify-publish/defaults/main.yaml | 2 + roles/netlify-publish/tasks/main.yaml | 36 ++++++++ .../netlify-publish/templates/config.json.j2 | 12 +++ roles/netlify-publish/templates/state.json.j2 | 3 + 7 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 playbooks/netlify-publish.yaml create mode 100644 roles/netlify-publish/README.rst create mode 100644 roles/netlify-publish/defaults/main.yaml create mode 100644 roles/netlify-publish/tasks/main.yaml create mode 100644 roles/netlify-publish/templates/config.json.j2 create mode 100644 roles/netlify-publish/templates/state.json.j2 diff --git a/.zuul.yaml b/.zuul.yaml index 66dc4a8..7f87b46 100644 --- a/.zuul.yaml +++ b/.zuul.yaml @@ -46,25 +46,102 @@ iodo9pXJsbXDL2xN4uun13sYBy7g/pUf6W2EAGR84BwxezRdLQYeTZy2xhcA+o8nLmmYR 1UJblPsI89YTH8x5rkxLJLBb22LIiNZzWikULd54XpqR9egV5AAwYsainkv+w8= +- secret: + name: inaugust-netlify-auth + data: + token: !encrypted/pkcs1-oaep + - MN4u8YTagu+mZXCbL7SwF0Z1Iee7zuh5m7LhwCzd0ueRVsbVHHbORBVJb+RH3O83bki1h + qD5db5NsIoIgQ4IXvJ/Xc4dG0geTULZfLmDbjPA9l8SW3iraTmYjEIL+qgg+7QWaNFTno + AttKYa3IeJo5A61T6hHz9ijS+5PhKuGwW29Kh/TpFu7HPr+7JRJzhi4n9RAmSYohGiUTM + ZqEmCNVGrU6AQV1CixBli5dwsCSpbYCsA4gohyNSVlHDBnoWAYLGf0kGHisKtJCLeBSpn + lFi7jzm7UYm6UFfjeTUF86C0hyPuK3NtkF3WYv+qUH72DGuYdSVD8daYWlAEZg8ptv+I8 + N8tyjrCmCSFjTk8IpLF0B8D9zPAxqnMPGgbB4UI31RDlbxqTZCVRrZzUdmigCkitsOhuA + 9d2JVGISif8sAm8mESlni9KXstYst8yFVSS/Y52h8FLp5Ee/CCHa7StQOtI2jaVOOdzdW + mHaRPY91FVGtqw+XDLZbPeaPQlt0Q9XZ7qFB/g0rTtl9eWlQ1KrYjC75LZ7ykqNnm8SNV + 0tyrHzRLcignZGyy8LT10x9NGba9r+dToAsVmdHcSzo11WwohRXanzZy6C9/4eeBST+7j + fnFOj7uNfNKo2TpmE3yaFNxDW1RumnlsmR970M0PmUVEVO3owAoAgaJolzTLhA= + - job: name: inaugust-build-website - parent: build-javascript-content + parent: build-javascript-deployment success-url: "http://site.{build.uuid}.{tenant.name}.zuul-preview.opendev.org/" vars: - npm_command: package - node_version: 10 + js_build_command: package + javascript_content_dir: dist - job: name: inaugust-publish-website - parent: nodejs-npm + parent: build-javascript-deployment vars: - node_version: 10 - npm_command: package + js_build_command: package + javascript_content_dir: dist post-run: playbooks/publish.yaml secrets: - secret: inaugust-secret name: ssh_key +- job: + name: inaugust-publish-javascript-to-netlify + parent: build-javascript-deployment + description: | + Builds javascript site and deploys to netlify. + + Responds to these variables: + + .. zuul:jobvar:: js_build_command + :default: build + + Command to pass to the javascript package manager.. + + .. zuul:jobvar:: js_build_tool + :default: autodetected + + Command to use for running the package manager, such as npm or yarn. + + .. zuul:jobvar:: node_version + :default: 14 + + The version of Node to use. + + .. zuul:jobvar:: zuul_work_dir + :default: {{ zuul.project.src_dir }} + + Path to operate in. + + .. zuul:jobvar:: javascript_content_dir + :default: public + + Directory, relative to zuul_work_dir, holding build content. + + .. zuul:jobvar:: netlify_site_id + + Site id for the site to publish. This can be found on the site + general settings page as ``API Id``. + + .. zuul:rolevar:: netlify_auth + :type: dict + + Complex argument which contains the netlify authentication credentials. + This is expected to come from a secret. + + .. zuul:rolevar:: token + + API token to use to publish the content. Instructions for creating + a token can be found at + https://docs.netlify.com/cli/get-started/#obtain-a-token-in-the-netlify-ui + post-run: playbooks/netlify-publish.yaml + +- job: + name: inaugust-publish-netlify + parent: inaugust-publish-javascript-to-netlify + vars: + netlify_site_id: 61580c77-b0fe-42c7-acec-c92d4cd1729c + javascript_content_dir: dist + js_build_command: package + secrets: + - secret: inaugust-netlify-auth + name: netlify_auth + - project: check: jobs: @@ -75,3 +152,4 @@ post: jobs: - inaugust-publish-website + - inaugust-publish-netlify diff --git a/playbooks/netlify-publish.yaml b/playbooks/netlify-publish.yaml new file mode 100644 index 0000000..9d83caf --- /dev/null +++ b/playbooks/netlify-publish.yaml @@ -0,0 +1,3 @@ +- hosts: all + roles: + - netlify-publish diff --git a/roles/netlify-publish/README.rst b/roles/netlify-publish/README.rst new file mode 100644 index 0000000..87dc288 --- /dev/null +++ b/roles/netlify-publish/README.rst @@ -0,0 +1,24 @@ +Publish built site to netlify + +**Role Variables** + +.. zuul:rolevar:: netlify_site_id + + Site id for the site to publish. This can be found on the site + general settings page as ``API Id``. + +.. zuul:rolevar:: netlify_auth + + Complex argument which contains the netlify authentication credentials. + This is expected to come from a secret. + + .. zuul:rolevar:: token + + API token to use to publish the content. Instructions for creating + a token can be found at + https://docs.netlify.com/cli/get-started/#obtain-a-token-in-the-netlify-ui + +.. zuul:rolevar:: zuul_work_dir + :default: {{ zuul.project.src_dir }} + + The project directory. diff --git a/roles/netlify-publish/defaults/main.yaml b/roles/netlify-publish/defaults/main.yaml new file mode 100644 index 0000000..fa01828 --- /dev/null +++ b/roles/netlify-publish/defaults/main.yaml @@ -0,0 +1,2 @@ +javascript_content_dir: public +zuul_work_dir: "{{ zuul.project.src_dir }}" diff --git a/roles/netlify-publish/tasks/main.yaml b/roles/netlify-publish/tasks/main.yaml new file mode 100644 index 0000000..c24c973 --- /dev/null +++ b/roles/netlify-publish/tasks/main.yaml @@ -0,0 +1,36 @@ +- name: Ensure user .netfliy dir + file: + state: directory + mode: 0700 + path: "{{ ansible_user_dir }}/.netlify" + +- name: Write netlify config file + template: + src: config.json.j2 + mode: 0600 + dest: "{{ ansible_user_dir }}/.netlify/config.json" + +- name: Ensure repo .netfliy dir + file: + state: directory + path: "{{ zuul_work_dir }}/.netlify" + +- name: Write repo netlify config file + template: + src: state.json.j2 + dest: "{{ zuul_work_dir }}/.netlify/state.json" + +- name: Check if netlify cli is installed + stat: + path: "{{ zuul_work_dir }}/node_modules/.bin/netlify" + register: has_netlify_cli + +- name: Set netlify cli command + set_fact: + netlify_cli: "{{ has_netlify_cli.stat.exists | ternary('npx netlify', 'npx -p netlify-cli netlify') }}" + +- name: Run netlify deployment + command: + cmd: "{{ netlify_cli }} deploy --prod -d {{ javascript_content_dir }}" + chdir: "{{ zuul_work_dir }}" + when: not netlify_publish_skip_upload diff --git a/roles/netlify-publish/templates/config.json.j2 b/roles/netlify-publish/templates/config.json.j2 new file mode 100644 index 0000000..013b586 --- /dev/null +++ b/roles/netlify-publish/templates/config.json.j2 @@ -0,0 +1,12 @@ +{ + "telemetryDisabled": false, + "userId": "zuul", + "users": { + "zuul": { + "id": "zuul", + "auth": { + "token": "{{ netlify_auth.token }}" + } + } + } +} diff --git a/roles/netlify-publish/templates/state.json.j2 b/roles/netlify-publish/templates/state.json.j2 new file mode 100644 index 0000000..d3cd36c --- /dev/null +++ b/roles/netlify-publish/templates/state.json.j2 @@ -0,0 +1,3 @@ +{ + "siteId": "{{ netlify_site_id }}" +}