ci(copybara): set up Zuul to sync with dotfiles-external

GitOrigin-RevId: 5d313e1f2dd57b3c9c8ee317a545559ab9a16c8f
diff --git a/.chezmoiignore b/.chezmoiignore
index 5081718..5a7f088 100644
--- a/.chezmoiignore
+++ b/.chezmoiignore
@@ -1,9 +1,12 @@
 LICENSE
 LICENSE.public
+playbooks
 README.md
 README.public.md
+roles
 .copybara
 .gitreview
+.zuul.yaml
 
 {{ if ne .chezmoi.os "darwin" }}
 .gitignore_global
diff --git a/.copybara/README.md b/.copybara/README.md
index 4c57d54..c0e7a9c 100644
--- a/.copybara/README.md
+++ b/.copybara/README.md
@@ -4,11 +4,8 @@
 ## How to test the Copybara script
 
 1. Clone the private and public repos in your computer.
-2. Create a `test_repos_config.sky` file with the following contents:
-  ```sky
-test_origin_url = "file:///path/to/private/dotfiles"
-test_destination_url = "file:///path/to/dotfiles-external"
-```
+2. Create a `test_repos_config.bara.sky` based in the template
+`test_repos_config.bara.sky.template`.
 3. Run `sh export-commits-test.sh`.
 
 [copybara]: https://github.com/google/copybara
diff --git a/.copybara/test_repos_config.bara.sky.template b/.copybara/test_repos_config.bara.sky.template
new file mode 100644
index 0000000..1281fc2
--- /dev/null
+++ b/.copybara/test_repos_config.bara.sky.template
@@ -0,0 +1,2 @@
+test_origin_url = "file:///path/to/private/dotfiles"
+test_destination_url = "file:///path/to/dotfiles-external"
diff --git a/.zuul.yaml b/.zuul.yaml
new file mode 100644
index 0000000..67b50c1
--- /dev/null
+++ b/.zuul.yaml
@@ -0,0 +1,23 @@
+# Zuul CI configuration file
+# Read more about Zuul at https://zuul-ci.org/docs/zuul/
+
+- job:
+    name: dotfiles-sync-to-external-repo
+    parent: copybara-base
+    pre-run: playbooks/sync-to-external-repo/pre.yaml
+    run: playbooks/sync-to-external-repo/run.yaml
+
+- job:
+    name: dotfiles-test-sync-to-external-repo
+    parent: copybara-test-base
+    run: playbooks/sync-to-external-repo/test.yaml
+    files:
+      - ".copybara/*"
+
+- project:
+    check:
+      jobs:
+        - dotfiles-test-sync-to-external-repo
+    post:
+      jobs:
+        - dotfiles-sync-to-external-repo
diff --git a/playbooks/README.md b/playbooks/README.md
new file mode 100644
index 0000000..f2200be
--- /dev/null
+++ b/playbooks/README.md
@@ -0,0 +1 @@
+Ansible playbook definitions for Zuul. See [//.zuul.yaml](../.zuul.yaml).
diff --git a/playbooks/sync-to-external-repo/pre.yaml b/playbooks/sync-to-external-repo/pre.yaml
new file mode 100644
index 0000000..a79e594
--- /dev/null
+++ b/playbooks/sync-to-external-repo/pre.yaml
@@ -0,0 +1,3 @@
+- hosts: all
+  roles:
+    - role: ensure-build-essential
diff --git a/playbooks/sync-to-external-repo/run.yaml b/playbooks/sync-to-external-repo/run.yaml
new file mode 100644
index 0000000..112932e
--- /dev/null
+++ b/playbooks/sync-to-external-repo/run.yaml
@@ -0,0 +1,3 @@
+- hosts: all
+  roles:
+    - role: export-commits
diff --git a/playbooks/sync-to-external-repo/test.yaml b/playbooks/sync-to-external-repo/test.yaml
new file mode 100644
index 0000000..f358015
--- /dev/null
+++ b/playbooks/sync-to-external-repo/test.yaml
@@ -0,0 +1,3 @@
+- hosts: all
+  roles:
+    - role: validate-copybara-config
diff --git a/roles/README.md b/roles/README.md
new file mode 100644
index 0000000..1e743f0
--- /dev/null
+++ b/roles/README.md
@@ -0,0 +1 @@
+Ansible role definitions for Zuul. See [//.zuul.yaml](../.zuul.yaml).
diff --git a/roles/export-commits/tasks/main.yaml b/roles/export-commits/tasks/main.yaml
new file mode 100644
index 0000000..8dfb6c5
--- /dev/null
+++ b/roles/export-commits/tasks/main.yaml
@@ -0,0 +1,9 @@
+- name: "Run Copybara migration"
+  ansible.builtin.shell:
+    cmd: "sh export-commits.sh"
+    chdir: "{{ zuul.project.src_dir }}/.copybara"
+  register: result
+  # Exit status 4 means NO_OP:
+  # https://github.com/google/copybara/blob/55c4904442bcf905fe03c2297488b90428b472a7/java/com/google/copybara/util/ExitCode.java#L42
+  failed_when: result.rc not in [0, 4]
+  retries: 3
diff --git a/roles/validate-copybara-config/tasks/main.yaml b/roles/validate-copybara-config/tasks/main.yaml
new file mode 100644
index 0000000..346550e
--- /dev/null
+++ b/roles/validate-copybara-config/tasks/main.yaml
@@ -0,0 +1,10 @@
+- name: "Create test_repos_config.sky"
+  ansible.builtin.copy:
+    src: "{{ zuul.project.src_dir }}/.copybara/test_repos_config.bara.sky.template"
+    dest: "{{ zuul.project.src_dir }}/.copybara/test_repos_config.bara.sky"
+    remote_src: true
+    force: true
+- name: "Validate Copybara conf file"
+  ansible.builtin.shell:
+    cmd: "copybara validate copy.bara.sky"
+    chdir: "{{ zuul.project.src_dir }}/.copybara"