|
13 | 13 | name={{ artefact_dir }} |
14 | 14 | state=directory |
15 | 15 |
|
16 | | -- name: Obtain the current tag, (or version if untagged) of this repository. |
17 | | - shell: | |
18 | | - TAG=$(git name-rev --tags --name-only HEAD | cut -f1 -d^) |
19 | | - if [ "$(echo $TAG | tr -d [:space:])" == "undefined" ]; then |
20 | | - git rev-parse --short HEAD |
21 | | - else |
22 | | - echo $TAG |
23 | | - fi |
| 16 | +- name: Obtain current tag of this repository, if any. |
| 17 | + shell: git name-rev --tags --name-only HEAD | cut -f1 -d^ |
24 | 18 | args: |
25 | 19 | chdir: "{{ container_dir }}" |
26 | 20 | executable: /bin/bash |
27 | | - register: current_version |
| 21 | + register: tag |
28 | 22 |
|
29 | | -- name: Set artefact name. |
| 23 | +- name: Obtain current version hash of this repository. |
| 24 | + shell: git rev-parse --short HEAD |
| 25 | + args: |
| 26 | + chdir: "{{ container_dir }}" |
| 27 | + executable: /bin/bash |
| 28 | + register: version |
| 29 | + |
| 30 | +# Alternatively, we can obtain the branch in detached-head state using the |
| 31 | +# environment variable GIT_BRANCH if it is set. This helps out Jenkins. |
| 32 | +- name: Obtain current branch of this repository. |
| 33 | + shell: if [ -z "$GIT_BRANCH" ]; then git symbolic-ref HEAD | grep -o [^/]*$; else echo "$GIT_BRANCH" | grep -o [^/][a-zA-Z0-9-]*$; fi |
| 34 | + args: |
| 35 | + chdir: "{{ container_dir }}" |
| 36 | + executable: /bin/bash |
| 37 | + register: branch |
| 38 | + |
| 39 | +# Exporting container filesystem as tarball. |
| 40 | + |
| 41 | +- name: Set artefact name with tag, if we could find one. |
| 42 | + set_fact: |
| 43 | + artefact_name: "{{ container_name }}_{{ tag.stdout }}.tar" |
| 44 | + when: tag.stdout != "undefined" |
| 45 | + |
| 46 | +- name: Set artefact name with version hash, if we could not find a tag. |
30 | 47 | set_fact: |
31 | | - artefact_name: "{{ container_name }}_{{ current_version.stdout }}.tar" |
| 48 | + artefact_name: "{{ container_name }}_{{ version.stdout }}.tar" |
| 49 | + when: tag.stdout == "undefined" |
32 | 50 |
|
33 | 51 | - name: Export the container filesystem as an artefact. |
34 | 52 | command: docker export --output={{ artefact_dir }}{{ artefact_name }} {{ container_name }} |
35 | 53 | args: |
36 | 54 | creates: "{{ artefact_dir }}{{ artefact_name }}" |
37 | 55 |
|
38 | | -- name: Obtain the current tag (or branch if untagged) of this repository. |
39 | | - shell: | |
40 | | - TAG=$(git name-rev --tags --name-only HEAD | cut -f1 -d^) |
41 | | - if [ "$(echo $TAG | tr -d [:space:])" == "undefined" ]; then |
42 | | - git describe --all | grep -o [^/]*$ |
43 | | - else |
44 | | - echo $TAG |
45 | | - fi |
46 | | - args: |
47 | | - chdir: "{{ container_dir }}" |
48 | | - executable: /bin/bash |
49 | | - register: current_branch |
| 56 | +# Storing container with tag=branch. |
| 57 | + |
| 58 | +- name: Define the name used for the output Docker image using the branch. |
| 59 | + set_fact: |
| 60 | + output_image_name_branch: virtualmicromagnetics/{{ container_name }}:{{ branch.stdout }} |
50 | 61 |
|
51 | | -- name: Define the name used for the output Docker image |
| 62 | +- name: Create a Docker image from our provisioned container. |
| 63 | + command: docker commit {{ container_name }} {{ output_image_name_branch }} |
| 64 | + |
| 65 | +- name: Push the Docker image with tag equal to branch. |
| 66 | + command: docker push {{ output_image_name_branch }} |
| 67 | + when: container_push |
| 68 | + |
| 69 | +# Storing container with tag=tag. |
| 70 | + |
| 71 | +- name: Define the name used for the output Docker image using the tag. |
52 | 72 | set_fact: |
53 | | - output_image_name: virtualmicromagnetics/{{ container_name }}:{{ current_branch.stdout }} |
| 73 | + output_image_name_tag: virtualmicromagnetics/{{ container_name }}:{{ tag.stdout }} |
| 74 | + when: tag.stdout != "undefined" |
54 | 75 |
|
55 | 76 | - name: Create a Docker image from our provisioned container. |
56 | | - command: docker commit {{ container_name }} {{ output_image_name }} |
| 77 | + command: docker commit {{ container_name }} {{ output_image_name_tag }} |
| 78 | + when: tag.stdout != "undefined" |
| 79 | + |
| 80 | +- name: Push the Docker image with tag equal to tag. |
| 81 | + command: docker push {{ output_image_name_tag }} |
| 82 | + when: (container_push == "true") and (tag.stdout != "undefined") |
57 | 83 |
|
58 | | -- name: Push the Docker image. |
59 | | - command: docker push {{ output_image_name }} |
| 84 | +# Remove the evidence. |
60 | 85 |
|
61 | 86 | - name: Destroy the container. |
62 | 87 | command: vagrant destroy --force |
|
0 commit comments