|
| 1 | +--- |
| 2 | +- name: GitHub Actions Runner | Install dependencies |
| 3 | + shell: |
| 4 | + cmd: "{{ gh_runner_path }}/bin/installdependencies.sh" |
| 5 | + chdir: "{{ gh_runner_path }}" |
| 6 | + become: true |
| 7 | + when: |
| 8 | + # Only install dependencies if the installation directory contains changes, |
| 9 | + # which is indicated in this case by the `unarchive` task. |
| 10 | + - gh_runner_path_unarchived is defined |
| 11 | + - gh_runner_path_unarchived.changed |
| 12 | + tags: |
| 13 | + - install |
| 14 | + |
| 15 | +# Check if the registration token was used before. config.sh executions aren't |
| 16 | +# idempotent, if called twice with the very same token and URL it will fail |
| 17 | +# asking to remove the host before configuring it. |
| 18 | +# |
| 19 | +# ["Cannot configure the runner because it is already configured. To |
| 20 | +# reconfigure the runner, run 'config.cmd remove' or './config.sh remove' first."] |
| 21 | +- name: GitHub Actions Runner | Check if this host+URL was already registered |
| 22 | + stat: |
| 23 | + path: "{{ gh_runner_path }}/hosts/{{ gh_runner_config_token | hash('sha256') }}" |
| 24 | + register: is_this_host_registered |
| 25 | + tags: |
| 26 | + - configure |
| 27 | + |
| 28 | +- name: GitHub Actions Runner | Search for abandoned credentials |
| 29 | + stat: |
| 30 | + path: "{{ gh_runner_path }}/.credentials" |
| 31 | + register: abandoned_credentials |
| 32 | + |
| 33 | + # Search only if the host is not registered, which is a clear indicator that |
| 34 | + # any credentials inside that path are from previous executions, since the |
| 35 | + # registration token is not matching any state file. |
| 36 | + when: not is_this_host_registered.stat.exists |
| 37 | + |
| 38 | +- name: GitHub Actions Runner | Remove previous configuration before proceeding |
| 39 | + shell: |
| 40 | + cmd: >- |
| 41 | + {{ gh_runner_path }}/config.sh remove \ |
| 42 | + --token {{ gh_runner_config_token }} |
| 43 | + chdir: "{{ gh_runner_path }}" |
| 44 | + |
| 45 | + when: |
| 46 | + # So, if abandoned credentials are found AND this host is not registered, we |
| 47 | + # need to remove it before proceeding, otherwise the `config.sh` command to |
| 48 | + # register the node will fail asking to remove it (which we are proactively |
| 49 | + # doing here). |
| 50 | + - not is_this_host_registered.stat.exists |
| 51 | + - abandoned_credentials is defined |
| 52 | + - abandoned_credentials.stat.exists |
| 53 | + |
| 54 | + tags: |
| 55 | + - configure |
| 56 | + |
| 57 | +- name: GitHub Actions Runner | Configure Runner |
| 58 | + shell: |
| 59 | + cmd: >- |
| 60 | + {{ gh_runner_path }}/config.sh \ |
| 61 | + --unattended --replace \ |
| 62 | + --url {{ gh_runner_config_url }} \ |
| 63 | + --token {{ gh_runner_config_token }} \ |
| 64 | + --name {{ ansible_hostname }} \ |
| 65 | + --work {{ gh_runner_workspace_path }} \ |
| 66 | + --labels {{ gh_runner_config_labels | join(',') }} |
| 67 | + chdir: "{{ gh_runner_path }}" |
| 68 | + register: config_host_command |
| 69 | + |
| 70 | + when: not is_this_host_registered.stat.exists |
| 71 | + tags: |
| 72 | + - configure |
| 73 | + |
| 74 | +- name: GitHub Actions Runner | Check if path for state files exists |
| 75 | + stat: |
| 76 | + path: "{{ gh_runner_path }}/hosts" |
| 77 | + register: registered_hosts_path |
| 78 | + tags: |
| 79 | + - configure |
| 80 | + |
| 81 | +- name: GitHub Actions Runner | Create hosts directory |
| 82 | + file: |
| 83 | + path: "{{ gh_runner_path }}/hosts" |
| 84 | + state: directory |
| 85 | + owner: "{{ ansible_user_id }}" |
| 86 | + mode: 0755 |
| 87 | + become: true |
| 88 | + when: not registered_hosts_path.stat.exists |
| 89 | + tags: |
| 90 | + - configure |
| 91 | + |
| 92 | +# Create a state file to notify next Ansible executions that a given |
| 93 | +# registration token was already used. i.e. the is_this_host_registered variable |
| 94 | +# registered and used in the credential above. |
| 95 | +- name: GitHub Actions Runner | Create state file |
| 96 | + file: |
| 97 | + path: "{{ gh_runner_path }}/hosts/{{ gh_runner_config_token | hash('sha256') }}" |
| 98 | + state: touch |
| 99 | + mode: 0400 |
| 100 | + when: |
| 101 | + - config_host_command is defined |
| 102 | + - config_host_command is success |
| 103 | + become: true |
| 104 | + tags: |
| 105 | + - configure |
| 106 | + |
| 107 | +- name: GitHub Actions Runner | Install service |
| 108 | + shell: |
| 109 | + cmd: "{{ gh_runner_path }}/svc.sh install" |
| 110 | + chdir: "{{ gh_runner_path }}" |
| 111 | + become: true |
| 112 | + when: not is_this_host_registered.stat.exists |
| 113 | + tags: |
| 114 | + - install |
| 115 | + |
| 116 | +- name: GitHub Actions Runner | Start service |
| 117 | + shell: |
| 118 | + cmd: "{{ gh_runner_path }}/svc.sh start" |
| 119 | + chdir: "{{ gh_runner_path }}" |
| 120 | + become: true |
| 121 | + tags: |
| 122 | + - configure |
0 commit comments