Add a job for publishing a site to netlify

Netlify has support for publishing built sites via a CLI tool. Add
support for using that.

Change-Id: Ib47ac48a386e5e93f93455ef1202fc2af970b1c9
This commit is contained in:
Monty Taylor 2020-07-02 08:53:44 -05:00
parent 04f9d83059
commit 9858cb5848
10 changed files with 167 additions and 5 deletions

View File

@ -8,3 +8,4 @@ Javascript Jobs
.. zuul:autojob:: nodejs-run-test-browser
.. zuul:autojob:: nodejs-run-lint
.. zuul:autojob:: nodejs-run-docs
.. zuul:autojob:: publish-javascript-to-netlify

View File

@ -9,6 +9,7 @@ Javascript Roles
.. zuul:autorole:: fetch-javascript-output
.. zuul:autorole:: fetch-javascript-tarball
.. zuul:autorole:: js-package-manager
.. zuul:autorole:: netlify-publish
.. zuul:autorole:: nodejs-test-dependencies
.. zuul:autorole:: npm
.. zuul:autorole:: upload-npm

View File

@ -0,0 +1,3 @@
- hosts: all
roles:
- netlify-publish

View File

@ -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.

View File

@ -0,0 +1,3 @@
javascript_content_dir: public
zuul_work_dir: "{{ zuul.project.src_dir }}"
netlify_publish_skip_upload: false

View File

@ -0,0 +1,42 @@
- name: Ensure user .netlify 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 .netlify 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
- name: Verify netlify command works
command:
cmd: "{{ netlify_cli }} help"
chdir: "{{ zuul_work_dir }}"
when: netlify_publish_skip_upload

View File

@ -0,0 +1,12 @@
{
"telemetryDisabled": true,
"userId": "zuul",
"users": {
"zuul": {
"id": "zuul",
"auth": {
"token": "{{ netlify_auth.token }}"
}
}
}
}

View File

@ -0,0 +1,3 @@
{
"siteId": "{{ netlify_site_id }}"
}

View File

@ -48,11 +48,6 @@
vars:
role_name: nodejs-test-dependencies
# -* AUTOGENERATED *-
# The following project section is autogenerated by
# tox -e update-test-platforms
# Please re-run to generate new job lists
- job:
name: zuul-jobs-test-nodejs-test-dependencies-debian-bookworm
description: Test the installation of nodejs test dependencies on debian-bookworm
@ -113,6 +108,30 @@
- name: ubuntu-noble
label: ubuntu-noble
- job:
name: zuul-jobs-test-netlify-publish
description: Test the netlify-publish role
files:
- roles/netlify-publish/.*
pre-run: playbooks/javascript/pre.yaml
run: test-playbooks/simple-role-test.yaml
vars:
netlify_auth:
token: 61ae27a3127142049eaf8c4ec507f7f4
netlify_site_id: test-site
# We can't actually do a deploy in the role test
netlify_publish_skip_upload: true
role_name: netlify-publish
# We're testing the role, so job version overrides
# in the netlify job aren't relevant. Override here.
node_version: 20
# -* AUTOGENERATED *-
# The following project section is autogenerated by
# tox -e update-test-platforms
# Please re-run to generate new job lists
- project:
check:
jobs: &id001
@ -126,6 +145,7 @@
- zuul-jobs-test-nodejs-test-dependencies-ubuntu-focal
- zuul-jobs-test-nodejs-test-dependencies-ubuntu-jammy
- zuul-jobs-test-nodejs-test-dependencies-ubuntu-noble
- zuul-jobs-test-netlify-publish
gate:
jobs: *id001
periodic-weekly:

View File

@ -95,6 +95,59 @@
Directory, relative to zuul_work_dir, holding build content.
post-run: playbooks/javascript/tarball.yaml
- job:
name: 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: 20
The version of Node to use. Netlify CLI requires at least v18.
.. 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/javascript/netlify-publish.yaml
vars:
node_version: 20
- job:
name: nodejs-run-test
parent: js-build