From c0f1a1c2ee00e4131db2484c8cc00b2bc4dc1263 Mon Sep 17 00:00:00 2001 From: Haitao Pan Date: Sun, 12 Apr 2026 19:05:17 +0800 Subject: [PATCH] Deploy billing-service from build artifact --- deploy_billing_service.yml | 38 ++++++++++- .../vhosts/billing-service/defaults/main.yml | 6 +- roles/vhosts/billing-service/tasks/main.yml | 65 ++----------------- .../templates/billing-service.env.j2 | 1 + 4 files changed, 45 insertions(+), 65 deletions(-) diff --git a/deploy_billing_service.yml b/deploy_billing_service.yml index 000a0d4..cb5efc4 100644 --- a/deploy_billing_service.yml +++ b/deploy_billing_service.yml @@ -3,9 +3,12 @@ gather_facts: true become: true vars: - billing_service_source_dir: >- - {{ lookup('ansible.builtin.env', 'BILLING_SERVICE_SOURCE_DIR') - | default(playbook_dir ~ '/../billing-service', true) }} + billing_service_binary_artifact: >- + {{ lookup('ansible.builtin.env', 'BILLING_SERVICE_BINARY_ARTIFACT') + | default('', true) }} + billing_service_image_ref: >- + {{ lookup('ansible.builtin.env', 'BILLING_SERVICE_IMAGE_REF') + | default('', true) }} billing_service_exporter_base_url: >- {{ lookup('ansible.builtin.env', 'EXPORTER_BASE_URL') | default('http://127.0.0.1:8080', true) }} @@ -39,6 +42,35 @@ {{ lookup('ansible.builtin.env', 'INITIAL_BALANCE') | default('0', true) }} pre_tasks: + - name: Validate BILLING_SERVICE_BINARY_ARTIFACT is present + ansible.builtin.assert: + that: + - billing_service_binary_artifact | length > 0 + fail_msg: "BILLING_SERVICE_BINARY_ARTIFACT must be exported before running this playbook." + success_msg: "BILLING_SERVICE_BINARY_ARTIFACT found" + - name: Validate BILLING_SERVICE_BINARY_ARTIFACT exists + ansible.builtin.stat: + path: "{{ billing_service_binary_artifact }}" + register: billing_service_binary_artifact_stat + delegate_to: localhost + become: false + run_once: true + - name: Assert BILLING_SERVICE_BINARY_ARTIFACT exists on controller + ansible.builtin.assert: + that: + - billing_service_binary_artifact_stat.stat.exists + - billing_service_binary_artifact_stat.stat.isreg + fail_msg: "BILLING_SERVICE_BINARY_ARTIFACT must point to an existing binary artifact." + success_msg: "BILLING_SERVICE_BINARY_ARTIFACT exists" + delegate_to: localhost + become: false + run_once: true + - name: Validate BILLING_SERVICE_IMAGE_REF is present + ansible.builtin.assert: + that: + - billing_service_image_ref | length > 0 + fail_msg: "BILLING_SERVICE_IMAGE_REF must be exported before running this playbook." + success_msg: "BILLING_SERVICE_IMAGE_REF found" - name: Validate DATABASE_URL is present ansible.builtin.assert: that: diff --git a/roles/vhosts/billing-service/defaults/main.yml b/roles/vhosts/billing-service/defaults/main.yml index c54a1cc..7e24334 100644 --- a/roles/vhosts/billing-service/defaults/main.yml +++ b/roles/vhosts/billing-service/defaults/main.yml @@ -1,11 +1,9 @@ --- -billing_service_source_dir: "{{ playbook_dir }}/../billing-service" billing_service_app_dir: "/opt/billing-service" -billing_service_go_version: "1.25.1" -billing_service_go_root: "/usr/local/go" -billing_service_go_bin: "{{ billing_service_go_root }}/bin/go" billing_service_binary_name: "billing-service" billing_service_binary_path: "/usr/local/bin/{{ billing_service_binary_name }}" +billing_service_binary_artifact: "" +billing_service_image_ref: "" billing_service_service_name: "billing-service" billing_service_service_description: "billing-service service" billing_service_user: "root" diff --git a/roles/vhosts/billing-service/tasks/main.yml b/roles/vhosts/billing-service/tasks/main.yml index dccdd95..9e188f6 100644 --- a/roles/vhosts/billing-service/tasks/main.yml +++ b/roles/vhosts/billing-service/tasks/main.yml @@ -1,45 +1,3 @@ ---- -- name: Map Go architecture - ansible.builtin.set_fact: - billing_service_go_arch: >- - {{ 'arm64' if ansible_architecture in ['aarch64', 'arm64'] else 'amd64' }} - -- name: Ensure billing-service build prerequisites are installed - ansible.builtin.apt: - name: - - curl - - ca-certificates - - tar - - rsync - state: present - update_cache: true - -- name: Check installed Go version - ansible.builtin.command: "{{ billing_service_go_bin }} version" - register: billing_service_go_version_check - changed_when: false - failed_when: false - -- name: Download Go toolchain archive - ansible.builtin.get_url: - url: "https://go.dev/dl/go{{ billing_service_go_version }}.linux-{{ billing_service_go_arch }}.tar.gz" - dest: "/tmp/go{{ billing_service_go_version }}.linux-{{ billing_service_go_arch }}.tar.gz" - mode: "0644" - when: billing_service_go_version_check.rc != 0 or ('go' ~ billing_service_go_version) not in billing_service_go_version_check.stdout - -- name: Remove previous Go installation - ansible.builtin.file: - path: "{{ billing_service_go_root }}" - state: absent - when: billing_service_go_version_check.rc != 0 or ('go' ~ billing_service_go_version) not in billing_service_go_version_check.stdout - -- name: Install Go toolchain - ansible.builtin.unarchive: - src: "/tmp/go{{ billing_service_go_version }}.linux-{{ billing_service_go_arch }}.tar.gz" - dest: "/usr/local" - remote_src: true - when: billing_service_go_version_check.rc != 0 or ('go' ~ billing_service_go_version) not in billing_service_go_version_check.stdout - - name: Create billing-service directories ansible.builtin.file: path: "{{ item }}" @@ -51,22 +9,13 @@ - "{{ billing_service_app_dir }}" - "{{ billing_service_env_dir }}" -- name: Sync billing-service source tree - ansible.posix.synchronize: - src: "{{ billing_service_source_dir }}/" - dest: "{{ billing_service_app_dir }}/" - delete: true - notify: Restart billing-service - -- name: Build billing-service binary - ansible.builtin.command: >- - {{ billing_service_go_bin }} build -buildvcs=false -o {{ billing_service_binary_path }} ./cmd/billing-service - args: - chdir: "{{ billing_service_app_dir }}" - environment: - GOROOT: "{{ billing_service_go_root }}" - PATH: "{{ billing_service_go_root }}/bin:{{ ansible_env.PATH }}" - GOTOOLCHAIN: local +- name: Deploy billing-service binary artifact + ansible.builtin.copy: + src: "{{ billing_service_binary_artifact }}" + dest: "{{ billing_service_binary_path }}" + owner: "{{ billing_service_user }}" + group: "{{ billing_service_group }}" + mode: "0755" notify: Restart billing-service - name: Ensure billing-service binary permissions diff --git a/roles/vhosts/billing-service/templates/billing-service.env.j2 b/roles/vhosts/billing-service/templates/billing-service.env.j2 index 805c914..4cc1340 100644 --- a/roles/vhosts/billing-service/templates/billing-service.env.j2 +++ b/roles/vhosts/billing-service/templates/billing-service.env.j2 @@ -1,3 +1,4 @@ +IMAGE={{ billing_service_image_ref }} EXPORTER_BASE_URL={{ billing_service_exporter_base_url }} EXPORTER_SOURCES_JSON={{ billing_service_exporter_sources_json }} INTERNAL_SERVICE_TOKEN={{ billing_service_internal_service_token }}