Skip to content

Commit

Permalink
UnixPB: Add latest commit SHA to ansible.log file (#2751)
Browse files Browse the repository at this point in the history
* add latest commit sha to logs

* remove debug

* remove duplicate code and change log path

* add debug to see where ansible's working directory is

* add ignore_errors

* remove debug

* copy one directory up in build workflow

* typo

* debugging

* pass git sha into dockerfile

* pass git sha from build.yml into dockerfile

* remove debug code

* use env instead of args

* forgot to add env to centos6 build

* remove brackets to see if this works

* use different syntax for variable

* remove - from env

* add build-args to build.yml

* add start logs

* change hosts back to original value
  • Loading branch information
Haroon-Khel authored Sep 28, 2022
1 parent 76d51f1 commit de2d533
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 46 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
uses: docker/build-push-action@v2
with:
file: ./ansible/docker/Dockerfile.CentOS6
build-args: git_sha=${{ github.sha }}
tags: adoptopenjdk/centos6_build_image:latest
cache-from: type=registry,ref=adoptopenjdk/centos6_build_image:latest
cache-to: type=inline
Expand All @@ -57,6 +58,7 @@ jobs:
uses: docker/build-push-action@v2
with:
file: ./ansible/docker/Dockerfile.Alpine3
build-args: git_sha=${{ github.sha }}
tags: adoptopenjdk/alpine3_build_image:latest
cache-from: type=registry,ref=adoptopenjdk/alpine3_build_image:latest
cache-to: type=inline
Expand Down
12 changes: 7 additions & 5 deletions ansible/docker/Dockerfile.Alpine3
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
FROM alpine:3.15

ARG git_sha

RUN apk update \
&& apk upgrade \
&& apk add ansible

COPY . /ansible
COPY ../. /infrastructure

RUN echo "localhost ansible_connection=local" > /ansible/hosts
RUN echo "localhost ansible_connection=local" > /infrastructure/ansible/hosts

RUN set -eux; \
cd /ansible; \
ansible-playbook -i hosts ansible/playbooks/AdoptOpenJDK_Unix_Playbook/main.yml --skip-tags="debug,hosts_file,hostname,adoptopenjdk,jenkins,nagios,superuser,docker,swap_file,crontab,nvidia_cuda_toolkit"; \
rm -rf /ansible; apk del ansible
cd /infrastructure; \
ansible-playbook -i ansible/hosts ansible/playbooks/AdoptOpenJDK_Unix_Playbook/main.yml -e "git_sha=$git_sha" --skip-tags="debug,hosts_file,hostname,adoptopenjdk,jenkins,nagios,superuser,docker,swap_file,crontab,nvidia_cuda_toolkit"; \
rm -rf /infrastructure; apk del ansible

ENV \
JDK7_BOOT_DIR="/usr/lib/jvm/jdk8" \
Expand Down
13 changes: 7 additions & 6 deletions ansible/docker/Dockerfile.CentOS6
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM centos:6.9

ARG git_sha
ARG user=jenkins

# Install Python 3
Expand All @@ -9,16 +10,16 @@ RUN sed -i -e 's!mirrorlist!#mirrorlist!g' /etc/yum.repos.d/CentOS-Base.repo; \
yum -y install gcc openssl-devel bzip2-devel sqlite-devel sudo wget python3 epel-release; \
yum -y install ansible

COPY . /ansible
COPY ../. /infrastructure

RUN echo "localhost ansible_connection=local" > /ansible/hosts
RUN echo "localhost ansible_connection=local" > /infrastructure/ansible/hosts

RUN set -eux; \
cd /ansible; \
ansible-playbook -i hosts ansible/playbooks/AdoptOpenJDK_Unix_Playbook/main.yml --skip-tags="debug,hosts_file,hostname,adoptopenjdk,jenkins,nagios,superuser,docker,swap_file,crontab,nvidia_cuda_toolkit"; \
ansible-playbook -i hosts ansible/playbooks/AdoptOpenJDK_Unix_Playbook/main.yml --tags="riscv"
cd /infrastructure; \
ansible-playbook -i ansible/hosts ansible/playbooks/AdoptOpenJDK_Unix_Playbook/main.yml -e "git_sha=$git_sha" --skip-tags="debug,hosts_file,hostname,adoptopenjdk,jenkins,nagios,superuser,docker,swap_file,crontab,nvidia_cuda_toolkit"; \
ansible-playbook -i ansible/hosts ansible/playbooks/AdoptOpenJDK_Unix_Playbook/main.yml -e "git_sha=$git_sha" --tags="riscv"

RUN rm -rf /ansible; yum remove -y ansible; yum clean all
RUN rm -rf /infrastructure; yum remove -y ansible; yum clean all

RUN groupadd -g 1000 ${user}
RUN useradd -c "Jenkins user" -d /home/${user} -u 1000 -g 1000 -m ${user}
Expand Down
4 changes: 4 additions & 0 deletions ansible/playbooks/AdoptOpenJDK_Unix_Playbook/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
# Roles #
#########
roles:
- role: logs
position: "Start"
tags: always
- Debug
- role: Get_Vendor_Files
tags: [vendor_files, adoptopenjdk, jenkins_user, nagios_plugins, superuser]
Expand Down Expand Up @@ -132,4 +135,5 @@
- role: disable_gui
tags: adoptopenjdk
- role: logs
position: "End"
tags: always
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,3 @@
state: present
backup: yes
tags: hosts_file, adoptopenjdk

# This doesn't work as ansible says it can't write to the directory
# Leaving this here in case someoen can make it work in the future
#
#- name: Record playbook completion with time in /var/log/ansiblerun.log
# lineinfile:
# state: present
# create: yes
# path: /var/tmp/ansiblerun.log
# mode: 0644
# line: "{{ ansible_date_time.iso8601_basic_short }} END"
# tags: ansiblerunlog

# Version of the above that works
- name: Record playbook completion with time in /var/log/ansiblerun.log
command: echo $(date +%Y%m%dT%H%M%S) >> /var/log/ansiblerun.log
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
---
# Updates $HOME/ansible.log with the date and time of latest ansible playbook run

- name: Set variables (Linux)
- name: Set Log path
set_fact:
home_path: /home/{{ Jenkins_Username }}
user_group: "{{ Jenkins_Username }}"
when:
- ansible_distribution != "MacOSX"
- ansible_distribution != "Solaris"
log_path: /var/log

- name: Set variables (MacOS)
set_fact:
home_path: /Users/{{ Jenkins_Username }}
user_group: "staff"
when:
- ansible_distribution == "MacOSX"

- name: Set variables (Solaris)
- name: Set variables (Not MacOS)
set_fact:
home_path: /export/home/{{ Jenkins_Username }}
user_group: "{{ Jenkins_Username }}"
user_group: "root"
when:
- ansible_distribution == "Solaris"
- ansible_distribution != "MacOSX"

- name: Check if Jenkins user exists
stat:
path: "{{ home_path }}"
register: home_exists
- name: Get Date and Time
shell: date +%Y-%m-%d\ %H:%M:%S
register: date_output

- name: Get Latest git commit SHA
shell: git rev-parse HEAD
register: git_output
delegate_to: localhost
ignore_errors: yes
when: git_sha is not defined

- name: Set git_output to git_sha
set_fact:
git_sha: "{{ git_output.stdout }}"
when: git_sha is not defined

- name: Update Log File
lineinfile:
owner: "{{ Jenkins_Username }}"
owner: root
group: "{{ user_group }}"
create: yes
path: "{{ home_path }}/ansible.log"
path: "{{ log_path }}/ansible.log"
insertafter: EOF
line: "{{ ansible_date_time.date }} -- {{ ansible_date_time.time }}"
line: "{{ position }} {{ date_output.stdout }} {{ git_sha }}"
become: yes
become_user: root
when: home_exists.stat.exists

0 comments on commit de2d533

Please sign in to comment.