A simple playbook to automate container image building based on Alpine Linux
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.2 KiB

  1. ---
  2. - name: build Alpine application image
  3. hosts: localhost
  4. gather_facts: false
  5. vars:
  6. default_alpine_version: 3.11.5
  7. vars_prompt:
  8. - name: aws_account
  9. prompt: Please specify AWS ECR account ID
  10. private: no
  11. - name: aws_region
  12. prompt: Please specify AWS region
  13. default: "{{ lookup('env', 'AWS_REGION') }}"
  14. private: no
  15. - name: alpine_version
  16. prompt: Please specify Alpine Linux version
  17. default: "{{ default_alpine_version }}"
  18. private: no
  19. - name: package_name
  20. prompt: Please specify package name
  21. private: no
  22. - name: package_version
  23. prompt: Please specify package version
  24. private: no
  25. tasks:
  26. - name: sanity check
  27. assert:
  28. that: package_name | length > 0
  29. fail_msg: no package name provided
  30. - name: obtain package version
  31. block:
  32. - name: obtain package information
  33. uri:
  34. url: "https://pkgs.alpinelinux.org/packages?name={{ package_name }}&branch=v{{ alpine_version.split('.')[0:2] | join('.') }}&arch=x86_64"
  35. return_content: yes
  36. register: pkg_query
  37. - name: extract package version
  38. set_fact:
  39. package_version: "{{ package_info.Version }}"
  40. vars:
  41. response_lines: "{{ ( pkg_query.content | trim ).split('\n') }}"
  42. info_titles: "{{ response_lines | select('search', '<th>') | map('regex_replace', '<th>(.*)</th>', '\\1') | map('trim') | list }}"
  43. info_values: "{{ response_lines | select('search', '<td.*>') | map('regex_replace', '<td(.*)>(.*)</td>', '\\2')| map('trim') | list }}"
  44. package_info: "{{ dict(info_titles | zip(info_values)) }}"
  45. when: package_version | length == 0
  46. - name: configure image version
  47. replace:
  48. path: "{{ lookup('env', 'HOME') }}/alpine-{{ package_name }}/Dockerfile"
  49. regexp: '^FROM alpine:.*$'
  50. replace: "FROM alpine:{{ alpine_version }}"
  51. - name: build image
  52. podman_image:
  53. name: "{{ ecr_registry }}/{{ package_name }}:alpine-{{ alpine_version }}-{{ package_version }}"
  54. path: "{{ lookup('env', 'HOME') }}/alpine-{{ package_name }}/"
  55. vars:
  56. ecr_registry: "{{ aws_account }}.dkr.ecr.{{ aws_region }}.amazonaws.com"