Skip to content

Commit

Permalink
Separate building & publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
pavetok committed Feb 22, 2024
1 parent 7903279 commit 61ee684
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 168 deletions.
40 changes: 34 additions & 6 deletions app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>dep-copy</id>
<phase>package</phase>
<id>deps-context</id>
<phase>pre-integration-test</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
Expand All @@ -78,8 +78,8 @@
</configuration>
</execution>
<execution>
<id>lib-copy</id>
<phase>package</phase>
<id>lib-context</id>
<phase>pre-integration-test</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
Expand All @@ -90,8 +90,8 @@
</configuration>
</execution>
<execution>
<id>app-copy</id>
<phase>package</phase>
<id>app-context</id>
<phase>pre-integration-test</phase>
<goals>
<goal>copy</goal>
</goals>
Expand All @@ -108,6 +108,34 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>app-image</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>docker</executable>
<arguments>
<argument>build</argument>
<argument>${project.build.directory}/image-context</argument>
<argument>--file</argument>
<argument>${project.basedir}/Dockerfile</argument>
<argument>--tag</argument>
<argument>app/${project.artifactId}:godel</argument>
<argument>--label</argument>
<argument>image.key=app/${project.artifactId}/godel</argument>
<argument>--build-arg</argument>
<argument>JAVA_RELEASE=${maven.compiler.release}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down
4 changes: 4 additions & 0 deletions app/sepuling-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4 changes: 4 additions & 0 deletions app/sepuling-kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
70 changes: 4 additions & 66 deletions baker/ansible/binaries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,7 @@
hosts: app
run_once: true
tasks:
- name: Capture cids
ansible.builtin.command:
cmd: git hash-object --stdin
stdin: |
{{ lookup('ansible.builtin.pipe', 'git write-tree --prefix=lib') }}
{{ lookup('ansible.builtin.pipe', 'git write-tree --prefix=app/' ~ app) }}
register: image_cid
changed_when: false
loop: "{{ product.apps.values()|flatten|map(attribute='binary') }}"
loop_control:
loop_var: app
tags: [always]
- name: Declare tags
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'))
|map('reverse'))
}}
tags: [always]
- name: Capture statuses
ansible.builtin.command:
cmd: docker {{ docker_entity }} inspect {{ image_ns }}/{{ app.image }}:{{ image_tags[app.binary] }}
register: image_status
changed_when:
- image_status.rc != 0
failed_when: false
loop: "{{ product.apps.values()|flatten }}"
loop_control:
loop_var: app
label: "{{ app.binary }}"
tags: [always]
- name: Status commands
ansible.builtin.debug:
msg: "{{ image_status.results|map(attribute='cmd')|map('join', ' ') }}"
when: image_status is changed
tags: [always]
- name: Create binaries
ansible.builtin.command:
cmd: >-
mvn
--no-snapshot-updates
--fail-fast
--batch-mode
--threads 2
--projects {{
image_status.results
| select('changed')
| map(attribute='app.binary')
| map('regex_replace', '^', ':')
| join(',')
}}
--also-make
clean
{{ maven_phase | default('package') }}
--define maven.compiler.release={{ dev[devenv].jdk.release }}
{% if dev[devenv].kotlin is defined %}
--define kotlin.version={{ dev[devenv].kotlin.version }}
{% endif %}
chdir: "{{ project.dir }}"
strip_empty_ends: false
changed_when: true
when: image_status is changed
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/app/build-and-check.yaml
vars:
maven_phase: package
103 changes: 44 additions & 59 deletions baker/ansible/images.yaml
Original file line number Diff line number Diff line change
@@ -1,72 +1,57 @@
---
- import_playbook: binaries.yaml # noqa: name[play]
vars:
maven_phase: "{{ 'install' if binary_repo == 'local' else 'deploy' }}"
tags: [binaries, deps]

- name: Images
- name: Building
hosts: db
run_once: true
tasks:
- name: Capture statuses
ansible.builtin.command:
cmd: >-
docker {{ docker_entity }} inspect
{{ image_ns }}/{{ product.databases[storage].image }}:{{ product.databases[storage].cid }}
register: image_status
changed_when: image_status.rc != 0
failed_when: false
loop: "{{ product.schemas.keys() }}"
loop_control:
loop_var: storage
- name: Status commands
ansible.builtin.debug:
msg: "{{ image_status.results|map(attribute='cmd')|map('join', ' ') }}"
when: image_status is changed
- name: Create images
ansible.builtin.include_role:
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/db/status.yaml
tags: [always]
- ansible.builtin.import_role: # noqa: name[missing]
name: image
tasks_from: clean
vars:
image_tag: "{{ product.databases[storage].cid }}"
image_name: "{{ image_ns }}/{{ product.databases[storage].image }}"
image_home: "{{ project.dir }}/db/{{ storage }}"
image_key: "{{ product.databases[storage].image }}"
image_push: "{{ image_repo != 'local' }}"
loop: "{{ image_status.results|select('changed')|map(attribute='storage') }}"
loop_control:
loop_var: storage
image_keys: >-
{{ image_status.results
| select('changed')
| map(attribute='db.image')
| map('regex_replace', '$', '/' ~ devenv)
}}
when: image_status is changed
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/db/build.yaml
when: image_status is changed

- name: Images
- name: Building & Checking
hosts: app
run_once: true
tasks:
- name: Analyze coverage
ansible.builtin.command:
cmd: >-
mvn
--no-snapshot-updates
--batch-mode
--projects tool
clean
antrun:run@coverage
chdir: "{{ project.dir }}"
strip_empty_ends: false
changed_when: true
when: image_status is changed
- name: Create images
ansible.builtin.include_role:
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/app/status.yaml
tags: [always]
- ansible.builtin.import_role: # noqa: name[missing]
name: image
tasks_from: clean
vars:
image_tag: "{{ image_tags[app.binary] }}"
image_name: "{{ image_ns }}/{{ app.image }}"
image_home: "{{ project.dir }}/app/{{ app.binary }}"
image_context: target/image-context
image_key: "{{ app.image }}/{{ devenv }}"
image_push: "{{ image_repo != 'local' }}"
image_args:
JAVA_RELEASE: "{{ dev[devenv].jdk.release }}"
loop: "{{ image_status.results|select('changed')|map(attribute='app') }}"
loop_control:
loop_var: app
label: "{{ app.binary }}"
image_keys: >-
{{ image_status.results
| select('changed')
| map(attribute='app.image')
| map('regex_replace', '$', '/' ~ devenv)
}}
when: image_status is changed
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/app/build-and-check.yaml
vars:
maven_phase: "{{ 'install' if binary_repo == 'local' else 'deploy' }}"
when: image_status is changed

- name: Publishing
hosts: image
run_once: true
tasks:
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/db/publish.yaml
when: hostvars.db.image_status is changed
- ansible.builtin.import_tasks: # noqa: name[missing]
file: tasks/app/publish.yaml
when: hostvars.app.image_status is changed
3 changes: 2 additions & 1 deletion baker/ansible/inventory/aliases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ stack:
hosts:
gear:
product:
all:
image:
hosts:
db:
app:
all:
vars:
ansible_connection: local
22 changes: 22 additions & 0 deletions baker/ansible/roles/image/tasks/clean.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
- name: Enforce variables
ansible.builtin.assert:
quiet: true
that:
- image_keys is defined

- name: Kill containers
ansible.builtin.shell:
cmd: docker container rm -f $(docker ps -aq --filter label=image.key={{ item }})
strip_empty_ends: false
changed_when: false
failed_when: false
loop: "{{ image_keys }}"

- name: Remove images
ansible.builtin.shell:
cmd: docker image rm -f $(docker images -q --filter label=image.key={{ item }})
strip_empty_ends: false
changed_when: false
failed_when: false
loop: "{{ image_keys }}"
33 changes: 5 additions & 28 deletions baker/ansible/roles/image/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -1,44 +1,21 @@
---
- name: Check vars
- name: Enforce variables
ansible.builtin.assert:
quiet: true
that:
- image_name is defined
- image_home is defined
- image_key is defined

- name: Kill containers
ansible.builtin.shell:
cmd: docker container rm -f $(docker ps -aq --filter label=image.key={{ image_key }})
strip_empty_ends: false
changed_when: false
failed_when: false
no_log: true

- name: Remove outdated
ansible.builtin.shell:
cmd: docker image rm -f $(docker images -q --filter label=image.key={{ image_key }})
strip_empty_ends: false
changed_when: false
failed_when: false
no_log: true

- name: Build fresh
- name: Build image
ansible.builtin.command:
cmd: >-
docker build {{ image_context }} --file Dockerfile
--tag {{ image_name }}:{{ image_tag }}
docker build {{ image_context }}
--file {{ image_home }}/Dockerfile
--label image.key={{ image_key }}
--tag {{ image_name }}:{{ image_tag }}
{% for key, value in image_args.items() %}
--build-arg {{ key }}={{ value }}
{% endfor %}
chdir: "{{ image_home }}"
strip_empty_ends: false
changed_when: true

- name: Push built
ansible.builtin.command:
cmd: docker image push {{ image_name }}:{{ image_tag }}
strip_empty_ends: false
changed_when: true
when: image_push|bool
6 changes: 3 additions & 3 deletions baker/ansible/stacks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
file: tasks/gear/status.yaml
tags: [always]

- name: Testing
- name: Building & Checking
hosts: stack
run_once: true
vars:
Expand All @@ -37,7 +37,7 @@
hostvars.product.stack_status is changed or
hostvars.gear.stack_status is changed
- name: Packaging
- name: Publishing
hosts: product
run_once: true
tasks:
Expand All @@ -46,7 +46,7 @@
tags: [package]
when: stack_status is changed

- name: Packaging
- name: Publishing
hosts: gear
run_once: true
tasks:
Expand Down
Loading

0 comments on commit 61ee684

Please sign in to comment.