Skip to content

Commit

Permalink
Stacks building & publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
pavetok committed Feb 23, 2024
1 parent 61ee684 commit 8ce4301
Show file tree
Hide file tree
Showing 27 changed files with 140 additions and 136 deletions.
17 changes: 6 additions & 11 deletions .github/workflows/beyond-doubt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ on:

env:
IMAGE_NAME: stack/gear
BINARY_REPO: maven.pkg.github.com
IMAGE_REPO: ghcr.io

jobs:
up-to-images:
Expand Down Expand Up @@ -57,8 +55,7 @@ jobs:
${{ env.IMAGE_NAME }}:${{ matrix.env.dev }}
ansible-playbook images.yaml -v
-e devenv=${{ matrix.env.dev }}
-e binary_repo=${{ env.BINARY_REPO }}
-e image_repo=${{ env.IMAGE_REPO }}
-e repo_mode=rw
- uses: mikepenz/[email protected]
if: ${{ !cancelled() && (steps.images.outcome == 'success' || steps.images.outcome == 'failure') }}
with:
Expand Down Expand Up @@ -108,7 +105,6 @@ jobs:
--workdir $(pwd)/baker/ansible
${{ env.IMAGE_NAME }}:${{ matrix.env.dev }}
ansible-playbook stacks.yaml -t build
-e image_repo=${{ env.IMAGE_REPO }}
-e devenv=${{ matrix.env.dev }}
-e purpose=${{ matrix.purpose }}
-e opsenv=${{ matrix.env.ops }}
Expand Down Expand Up @@ -149,7 +145,7 @@ jobs:
with:
name: product-${{ matrix.purpose }}-${{ matrix.env.ops }}
path: stack/product/target/image-context
- name: Run ansible-playbook stacks.yaml -t test
- name: Run ansible-playbook stacks.yaml -t check
run: >-
docker run --rm
--network gear
Expand All @@ -158,8 +154,7 @@ jobs:
--volume /var/run/docker.sock:/var/run/docker.sock
--workdir $(pwd)/baker/ansible
${{ env.IMAGE_NAME }}:${{ matrix.env.dev }}
ansible-playbook stacks.yaml -t test
-e image_repo=${{ env.IMAGE_REPO }}
ansible-playbook stacks.yaml -t check
-e devenv=${{ matrix.env.dev }}
-e purpose=${{ matrix.purpose }}
-e opsenv=${{ matrix.env.ops }}
Expand Down Expand Up @@ -204,19 +199,19 @@ jobs:
with:
name: product-${{ matrix.purpose }}-${{ matrix.env.ops }}
path: stack/product/target/image-context
- name: Run ansible-playbook stacks.yaml -t package
- name: Run ansible-playbook stacks.yaml -t publish
run: >-
docker run --rm
--volume $(pwd):$(pwd)
--volume /var/run/docker.sock:/var/run/docker.sock
--volume ~/.docker/config.json:/root/.docker/config.json:ro
--workdir $(pwd)/baker/ansible
${{ env.IMAGE_NAME }}:${{ matrix.env.dev }}
ansible-playbook stacks.yaml -t package -v
-e image_repo=${{ env.IMAGE_REPO }}
ansible-playbook stacks.yaml -t publish -v
-e devenv=${{ matrix.env.dev }}
-e purpose=${{ matrix.purpose }}
-e opsenv=${{ matrix.env.ops }}
-e repo_mode=rw
stack-reporting:
name: Stack reporting
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/convincing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ jobs:
${{ env.IMAGE_NAME }}:${{ matrix.env.dev }}
ansible-playbook images.yaml -v
-e devenv=${{ matrix.env.dev }}
-e binary_repo=maven.pkg.github.com
-e image_repo=ghcr.io
-e repo_mode=rw
- uses: mikepenz/[email protected]
if: ${{ !cancelled() && (steps.images.outcome == 'success' || steps.images.outcome == 'failure') }}
with:
Expand Down
2 changes: 1 addition & 1 deletion baker/ansible/binaries.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: Binaries
- name: Building & Checking
hosts: app
run_once: true
tasks:
Expand Down
6 changes: 3 additions & 3 deletions baker/ansible/images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
run_once: true
tasks:
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/db/status.yaml
file: tasks/db/capture.yaml
tags: [always]
- ansible.builtin.import_role: # noqa: name[missing]
name: image
Expand All @@ -26,7 +26,7 @@
run_once: true
tasks:
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/app/status.yaml
file: tasks/app/capture.yaml
tags: [always]
- ansible.builtin.import_role: # noqa: name[missing]
name: image
Expand All @@ -42,7 +42,7 @@
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/app/build-and-check.yaml
vars:
maven_phase: "{{ 'install' if binary_repo == 'local' else 'deploy' }}"
maven_phase: "{{ 'deploy' if repo_mode == 'rw' else 'install' }}"
when: image_status is changed

- name: Publishing
Expand Down
8 changes: 4 additions & 4 deletions baker/ansible/inventory/aliases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ sources:
java:
ansible:
github:
stack:
hosts:
gear:
product:
image:
hosts:
db:
app:
stack:
hosts:
gear:
product:
all:
vars:
ansible_connection: local
7 changes: 4 additions & 3 deletions baker/ansible/inventory/group_vars/all/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ project:
org: smecalculus
dir: "{{ playbook_dir }}/../.."

binary_repo: local
image_repo: local
binary_repo: maven.pkg.github.com
image_repo: ghcr.io
repo_mode: n/a # values: rw, ro, n/a

image_ns: "{{ image_repo }}/{{ project.org }}/{{ project.name }}"

docker_entity: "{{ 'image' if image_repo == 'local' else 'manifest' }}"
docker_entity: "{{ 'image' if repo_mode == 'n/a' else 'manifest' }}"
2 changes: 0 additions & 2 deletions baker/ansible/roles/image/defaults/main.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
---
image_tag: latest
image_context: .
image_push: false
image_args: {}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
---
- name: Enforce variables
- name: Check inputs
ansible.builtin.assert:
quiet: true
that:
- image_name is defined
- image_home is defined
- image_key is defined
- image_tag is defined

- name: Build image
ansible.builtin.command:
cmd: >-
docker build {{ image_context }}
--file {{ image_home }}/Dockerfile
--file Dockerfile
--label image.key={{ image_key }}
--tag {{ image_name }}:{{ image_tag }}
--tag {{ image_tag }}
{% for key, value in image_args.items() %}
--build-arg {{ key }}={{ value }}
{% endfor %}
strip_empty_ends: false
chdir: "{{ image_home }}"
changed_when: true
2 changes: 1 addition & 1 deletion baker/ansible/roles/image/tasks/clean.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: Enforce variables
- name: Check inputs
ansible.builtin.assert:
quiet: true
that:
Expand Down
20 changes: 20 additions & 0 deletions baker/ansible/roles/image/tasks/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
- name: Check inputs
ansible.builtin.assert:
quiet: true
that:
- source_tag is defined
- target_tags is defined

- name: Tag images
ansible.builtin.command:
cmd: docker tag {{ source_tag }} {{ item }}
strip_empty_ends: false
loop: "{{ target_tags }}"

- name: Push images
ansible.builtin.command:
cmd: docker push {{ item }}
strip_empty_ends: false
loop: "{{ target_tags }}"
tags: [push, never]
10 changes: 5 additions & 5 deletions baker/ansible/sources.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: Sources
- name: Checking
hosts: java
run_once: true
tasks:
Expand All @@ -19,11 +19,11 @@
strip_empty_ends: false
changed_when: false

- name: Sources
- name: Checking
hosts: ansible
run_once: true
tasks:
- name: Analyze
- name: Linting
ansible.builtin.command:
# list all top level playbooks
cmd: >-
Expand All @@ -33,11 +33,11 @@
strip_empty_ends: false
changed_when: false

- name: Sources
- name: Checking
hosts: github
run_once: true
tasks:
- name: Analyze
- name: Linting
ansible.builtin.command:
cmd: yamllint {{ project.dir }}/.github
strip_empty_ends: false
Expand Down
28 changes: 16 additions & 12 deletions baker/ansible/stacks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
run_once: true
tasks:
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/product/status.yaml
file: tasks/product/capture.yaml
tags: [always]

- name: Capturing
- name: Capturing & Building
hosts: gear
run_once: true
tasks:
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/gear/status.yaml
file: tasks/gear/capture.yaml
tags: [always]
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/gear/build.yaml
tags: [build]
when: hostvars.gear.stack_status is changed

- name: Building & Checking
hosts: stack
hosts: product
run_once: true
vars:
stack_dir: "{{ project.dir }}/stack/product"
Expand All @@ -31,8 +35,8 @@
hostvars.product.stack_status is changed or
hostvars.gear.stack_status is changed
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/product/test.yaml
tags: [test]
file: tasks/product/check.yaml
tags: [check]
when: >-
hostvars.product.stack_status is changed or
hostvars.gear.stack_status is changed
Expand All @@ -42,15 +46,15 @@
run_once: true
tasks:
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/product/package.yaml
tags: [package]
when: stack_status is changed
file: tasks/product/publish.yaml
tags: [publish]
when: hostvars.product.stack_status is changed

- name: Publishing
hosts: gear
run_once: true
tasks:
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/gear/package.yaml
tags: [package]
when: stack_status is changed
file: tasks/gear/publish.yaml
tags: [publish]
when: hostvars.gear.stack_status is changed
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: Capture cids
- name: Capture CID's
ansible.builtin.command:
cmd: git hash-object --stdin
stdin: |
Expand All @@ -16,11 +16,11 @@
ansible.builtin.set_fact:
image_tags: >-
{{ dict(image_cid.results
|map(attribute='stdout')
|map('truncate', 7, True, '')
|map('regex_replace', '^', devenv ~ '-')
|zip(image_cid.results|map(attribute='app.binary'))
|map('reverse'))
| map(attribute='stdout')
| map('truncate', 7, True, '')
| map('regex_replace', '^', devenv ~ '-')
| zip(image_cid.results|map(attribute='app.binary'))
| map('reverse'))
}}
- name: Capture statuses
Expand All @@ -33,9 +33,4 @@
loop: "{{ product.apps.values()|flatten }}"
loop_control:
loop_var: app
label: "{{ app.image }}"

- name: Status commands
ansible.builtin.debug:
msg: "{{ image_status.results|map(attribute='cmd')|map('join', ' ') }}"
when: image_status is changed
label: "{{ app.image }}:{{ image_tags[app.binary] }}"
26 changes: 8 additions & 18 deletions baker/ansible/tasks/app/publish.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
---
- name: Tag images
ansible.builtin.command:
cmd: >-
docker tag
{{ app.image }}:{{ devenv }}
{{ image_ns }}/{{ app.image }}:{{ image_tags[app.binary] }}
strip_empty_ends: false
loop: "{{ image_status.results|select('changed')|map(attribute='app') }}"
- ansible.builtin.include_role: # noqa: name[missing]
name: image
tasks_from: publish
vars:
source_tag: "{{ app.image }}:{{ devenv }}"
target_tags:
- "{{ image_ns }}/{{ app.image }}:{{ hostvars.app.image_tags[app.binary] }}"
loop: "{{ hostvars.app.image_status.results|select('changed')|map(attribute='app') }}"
loop_control:
loop_var: app
label: "{{ app.image }}"

- name: Push images
ansible.builtin.command:
cmd: docker push {{ image_ns }}/{{ app.image }}:{{ image_tags[app.binary] }}
strip_empty_ends: false
loop: "{{ image_status.results|select('changed')|map(attribute='app') }}"
loop_control:
loop_var: app
label: "{{ app.image }}"
tags: [push, never]
9 changes: 4 additions & 5 deletions baker/ansible/tasks/db/build.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
---
- name: Build images
ansible.builtin.include_role:
- ansible.builtin.include_role: # noqa: name[missing]
name: image
tasks_from: build
vars:
image_home: "{{ project.dir }}/{{ db.image }}"
image_name: "{{ db.image }}"
image_key: "{{ db.image }}/{{ devenv }}"
image_tag: "{{ devenv }}"
loop: "{{ image_status.results|select('changed')|map(attribute='db') }}"
image_tag: "{{ db.image }}:{{ devenv }}"
loop: "{{ hostvars.db.image_status.results|select('changed')|map(attribute='db') }}"
loop_control:
loop_var: db
label: "{{ db.image }}"
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,4 @@
loop: "{{ product.databases.values() }}"
loop_control:
loop_var: db
label: "{{ db.image }}"

- name: Status commands
ansible.builtin.debug:
msg: "{{ image_status.results|map(attribute='cmd')|map('join', ' ') }}"
when: image_status is changed
label: "{{ db.image }}:{{ db.cid }}"
Loading

0 comments on commit 8ce4301

Please sign in to comment.