我使用gitlab-ci进行自动化测试。 现在我扩展它以允许通过ansible playbook在数字海洋飞沫上部署评论应用程序。
这工作得非常好,但是我需要从ansible到.gitlab-ci得到一个变量 - 我无法找到它的方法。
.gitlab-ci.yml
Deploy for Review: before_script: [] stage: review script: 'cd /home/playbooks/oewm/deployment && ansible-playbook -i inventories/review --extra-vars "do_name=$CI_PIPELINE_ID api_git_branch=$CI_BUILD_REF_NAME" digitalocean.yml' environment: name: review/$CI_BUILD_REF_NAME url: http://$IP_FROM_ANSIBLE on_stop: "Stop Review" only: - branches when: manual tags: - deploy剧本中的相关部分:
- name: Create DO Droplet delegate_to: localhost local_action: module: digital_ocean state=present command=droplet name=review-{{ do_name }} api_token={{ do_token }} region_id={{ do_region }} image_id={{ do_image }} size_id={{ do_size }} ssh_key_ids={{ do_ssh }} wait_timeout=500 register: my_droplet - name: print info about droplet delegate_to: localhost local_action: module: debug msg="ID is {{ my_droplet.droplet.id }} IP is {{ my_droplet.droplet.ip_address }}"那么我怎么能得到gitlab-ci的Droplet ID和IP? (以后的Stop操作需要ID,要向开发人员查看IP)
I use gitlab-ci for the automated tests. Now i extended it to allow for review apps deployed on digitalocean droplets via an ansible playbook.
This is working very well, but i need to get a variable from ansible to the .gitlab-ci - i can't find a way todo it.
.gitlab-ci.yml
Deploy for Review: before_script: [] stage: review script: 'cd /home/playbooks/oewm/deployment && ansible-playbook -i inventories/review --extra-vars "do_name=$CI_PIPELINE_ID api_git_branch=$CI_BUILD_REF_NAME" digitalocean.yml' environment: name: review/$CI_BUILD_REF_NAME url: http://$IP_FROM_ANSIBLE on_stop: "Stop Review" only: - branches when: manual tags: - deploythe relevant parts from the playbook:
- name: Create DO Droplet delegate_to: localhost local_action: module: digital_ocean state=present command=droplet name=review-{{ do_name }} api_token={{ do_token }} region_id={{ do_region }} image_id={{ do_image }} size_id={{ do_size }} ssh_key_ids={{ do_ssh }} wait_timeout=500 register: my_droplet - name: print info about droplet delegate_to: localhost local_action: module: debug msg="ID is {{ my_droplet.droplet.id }} IP is {{ my_droplet.droplet.ip_address }}"So how can i get the droplet ID and IP to gitlab-ci? (The ID is needed for the later Stop action, the IP to be viewed to the developer)
最满意答案
Ansible是一个YAML配置的脚本工具本身,可能几乎完全自动化脚本环境本身。 为什么不在某处写一个名为“./ip_address.sh”的文件,然后在你的gitlab CI中包含.sh?
所有这一切的最高级别,在.gitlab-ci.yml会有这样的:
script: - ./run_ansible.sh ./out/run_file_generated_from_ansible.sh - . ./out/run_file_generated_from_ansible.sh - echo $IP_FROM_ANSIBLE environment: name: review/$CI_BUILD_REF_NAME url: http://$IP_FROM_ANSIBLE on_stop: "Stop Review"编写上面的两个shell脚本留给读者练习。 魔法发生在Ansible“playbook”里面,它实际上只是一个脚本,你用文件名“./out/run_file_generate_from_ansible.sh”“将变量导出到磁盘”。
你没有说清楚的是你需要在Gitlab-CI中使用该变量做什么以及最终结果,以及接下来会发生什么。 所以上面,我只是展示了一种可以通过临时磁盘文件(IP地址)“导出”的方法。
您可以将该导出值保存为工件并将其捕获到其他阶段,因此,如果将所有这些工具放在名为./out的目录中,然后在其中声明工件语句,则可以在各阶段之间传递这种“工件导出”。 gitlab-ci.yml。
Ansible is a YAML-configured scripting tool itself and probably nearly turing complete automation scripting environment itself. Why not have it write a file called "./ip_address.sh" somewhere, and then dot-include that .sh into your gitlab CI?
The very top level of all this, in .gitlab-ci.yml would have this:
script: - ./run_ansible.sh ./out/run_file_generated_from_ansible.sh - . ./out/run_file_generated_from_ansible.sh - echo $IP_FROM_ANSIBLE environment: name: review/$CI_BUILD_REF_NAME url: http://$IP_FROM_ANSIBLE on_stop: "Stop Review"Writing the two shell scripts above is left as an exercise to the reader. The magic happens inside the Ansible "playbook" which is really just a script, where YOU "export a variable to disk" with filename "./out/run_file_generate_from_ansible.sh".
What you didn't make clear is what you need to do in Gitlab-CI with that variable and where it ends up, and what happens next. So above, I'm just showing a way you could "export" via a temporary on-disk-file, an IP address.
You could save that exported value as an artifact and capture it in other stages as well, so such "artifact-exports" can be passed among stages, if you put them all in a directory called ./out and then declare an artifacts statement in gitlab-ci.yml.
更多推荐
发布评论