diff --git a/doc/source/js-jobs.rst b/doc/source/js-jobs.rst index a57b7e7aa..47b2cd561 100644 --- a/doc/source/js-jobs.rst +++ b/doc/source/js-jobs.rst @@ -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 diff --git a/doc/source/js-roles.rst b/doc/source/js-roles.rst index ace0ac9ac..81165536e 100644 --- a/doc/source/js-roles.rst +++ b/doc/source/js-roles.rst @@ -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 diff --git a/playbooks/javascript/netlify-publish.yaml b/playbooks/javascript/netlify-publish.yaml new file mode 100644 index 000000000..9d83caf23 --- /dev/null +++ b/playbooks/javascript/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 000000000..87dc288c3 --- /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 000000000..c7fd75748 --- /dev/null +++ b/roles/netlify-publish/defaults/main.yaml @@ -0,0 +1,3 @@ +javascript_content_dir: public +zuul_work_dir: "{{ zuul.project.src_dir }}" +netlify_publish_skip_upload: false diff --git a/roles/netlify-publish/tasks/main.yaml b/roles/netlify-publish/tasks/main.yaml new file mode 100644 index 000000000..6faf5a47a --- /dev/null +++ b/roles/netlify-publish/tasks/main.yaml @@ -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 diff --git a/roles/netlify-publish/templates/config.json.j2 b/roles/netlify-publish/templates/config.json.j2 new file mode 100644 index 000000000..035e33592 --- /dev/null +++ b/roles/netlify-publish/templates/config.json.j2 @@ -0,0 +1,12 @@ +{ + "telemetryDisabled": true, + "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 000000000..d3cd36c35 --- /dev/null +++ b/roles/netlify-publish/templates/state.json.j2 @@ -0,0 +1,3 @@ +{ + "siteId": "{{ netlify_site_id }}" +} diff --git a/zuul-tests.d/js-roles-jobs.yaml b/zuul-tests.d/js-roles-jobs.yaml index e260cb42c..405825f53 100644 --- a/zuul-tests.d/js-roles-jobs.yaml +++ b/zuul-tests.d/js-roles-jobs.yaml @@ -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: diff --git a/zuul.d/js-jobs.yaml b/zuul.d/js-jobs.yaml index d5f8e434f..66a1319be 100644 --- a/zuul.d/js-jobs.yaml +++ b/zuul.d/js-jobs.yaml @@ -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