diff --git a/templates/azure/github-runner/files/github-runner.service b/templates/azure/github-runner/files/github-runner-start.service similarity index 61% rename from templates/azure/github-runner/files/github-runner.service rename to templates/azure/github-runner/files/github-runner-start.service index 0a2b604..9d17ae6 100644 --- a/templates/azure/github-runner/files/github-runner.service +++ b/templates/azure/github-runner/files/github-runner-start.service @@ -1,11 +1,11 @@ [Unit] -Description=github-runner-script +Description=github-runner-start After=network.target [Service] Type=oneshot +WorkingDirectory=/etc/github-runner ExecStart=/etc/github-runner/github-runner.sh --action=REGISTER -ExecStop=/etc/github-runner/github-runner.sh --action=REMOVE [Install] WantedBy=multi-user.target \ No newline at end of file diff --git a/templates/azure/github-runner/files/github-runner-stop.service b/templates/azure/github-runner/files/github-runner-stop.service new file mode 100644 index 0000000..42224bf --- /dev/null +++ b/templates/azure/github-runner/files/github-runner-stop.service @@ -0,0 +1,12 @@ +[Unit] +Description=github-runner-stop +After=network.target + +[Service] +Type=oneshot +WorkingDirectory=/etc/github-runner +RemainAfterExit=true +ExecStop=/etc/github-runner/github-runner.sh --action=REMOVE + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/templates/azure/github-runner/files/github-runner.sh b/templates/azure/github-runner/files/github-runner.sh index 08532b5..ecbd1be 100755 --- a/templates/azure/github-runner/files/github-runner.sh +++ b/templates/azure/github-runner/files/github-runner.sh @@ -91,6 +91,8 @@ if [[ "${ACTION}" == "REGISTER" ]]; then elif [[ "${ACTION}" == "REMOVE" ]]; then GITHUB_RUNNER_OUTPUT=$(${GITHUB_RUNNER_BINARY} --value-source AZURE_KEYVAULT --azure-keyvault-name ${AZURE_KEYVAULT_NAME} --organization-kvsecret ${GITHUB_ORGANIZATION_KVSECRET} --app-id-kvsecret ${GITHUB_APP_ID_KVSECRET} --installation-id-kvsecret ${GITHUB_INSTALLATION_ID_KVSECRET} --private-key-kvsecret ${GITHUB_PRIVATE_KEY_KVSECRET} --azure-auth ${AZURE_AUTHENTICATION_METHOD} --output JSON --token-type REMOVE) GITHUB_RUNNER_REMOVE_TOKEN=$(echo ${GITHUB_RUNNER_OUTPUT} | jq -r .token) + set +e ${GITHUB_RUNNER_SERVICE_SCRIPT} uninstall + set -e runuser -l ghrunner -c "${GITHUB_RUNNER_CONFIG_SCRIPT} remove --unattended --token ${GITHUB_RUNNER_REMOVE_TOKEN}" fi diff --git a/templates/azure/github-runner/files/github-runner.yaml b/templates/azure/github-runner/files/github-runner.yaml index 0915b80..ebba520 100644 --- a/templates/azure/github-runner/files/github-runner.yaml +++ b/templates/azure/github-runner/files/github-runner.yaml @@ -116,17 +116,30 @@ group: root mode: "0744" - - name: Copy github-runner.service + - name: Copy github-runner-start.service copy: - src: github-runner.service - dest: /etc/systemd/system/github-runner.service + src: github-runner-start.service + dest: /etc/systemd/system/github-runner-start.service owner: root group: root mode: "0644" - - name: Enable github-runner.service + - name: Enable github-runner-start.service systemd: - name: github-runner.service + name: github-runner-start.service + enabled: yes + + - name: Copy github-runner-stop.service + copy: + src: github-runner-stop.service + dest: /etc/systemd/system/github-runner-stop.service + owner: root + group: root + mode: "0644" + + - name: Enable github-runner-stop.service + systemd: + name: github-runner-stop.service enabled: yes - name: Download github-runner diff --git a/templates/azure/github-runner/github-runner.json b/templates/azure/github-runner/github-runner.json index afc3590..7336c7d 100644 --- a/templates/azure/github-runner/github-runner.json +++ b/templates/azure/github-runner/github-runner.json @@ -7,7 +7,7 @@ "type": "azure-arm", "use_azure_cli_auth": true, "managed_image_resource_group_name": "{{user `AZURE_RESOURCE_GROUP_NAME`}}", - "managed_image_name": "azp-agent-{{isotime | clean_resource_name}}", + "managed_image_name": "github-runner-{{isotime | clean_resource_name}}", "build_resource_group_name": "{{user `AZURE_RESOURCE_GROUP_NAME`}}", "os_type": "Linux", "image_publisher": "Canonical",