- hosts: localhost
  tasks:
    - name: Check execution context
      when: "zuul.tag is not defined"
      fail:
        msg: "This playbook must be run in a tag-based pipeline (e.g., 'release')."
    - name: Write root marker
      include_role:
        name: write-root-marker
      vars:
        root_marker_dir: "{{ zuul.executor.log_root }}/docs"
    - name: Select target configuration
      set_fact:
        target_dict: "{{ afs.targets.tag }}"
    - name: Set target path
      set_fact:
        target_dir: "{{ target_dict.path.format(zuul=zuul) }}"
    - name: Adjust target path
      when: "target_dict.regex is defined"
      set_fact:
        target_dir: "{{ target_dir | regex_replace(target_dict.regex.pattern, target_dict.regex.sub) }}"
    - name: Get an AFS token
      include_role:
        name: create-afs-token
    - name: Create publication directory
      file:
        path: "{{ target_dir }}"
        state: directory
        mode: "0755"
    - name: Upload to AFS
      include_role:
        name: upload-afs-roots
      vars:
        afs_source: "{{ zuul.executor.log_root }}/docs/"
        afs_target: "{{ target_dir }}"
    - name: Destroy AFS token
      include_role:
        name: destroy-afs-token