Add Bazel build and ensure roles

* Build role requires build target to be specified via a variable
* Ensure role checks for a specific version of Bazel and downloads
  and installs if missing; defaults to v3.1.0 and downloading from
  bazelbuild Github, but these can be overrriden using variables
* Also installs dependencies for Bazel; only supports Ubuntu/Debian
  at present, but includes scope to extend for other platforms
* Includes test playbook and job

Change-Id: I83f198aaf20c2b3664bea6fc05edd3b4fca13a4f
This commit is contained in:
Paul Albertella 2020-04-24 13:30:47 +01:00
parent 5b37cabf41
commit ad7297199d
13 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,5 @@
Build Roles
============
.. zuul:autorole:: bazel-build
.. zuul:autorole:: ensure-bazel

View File

@ -10,6 +10,7 @@ Roles
general-roles
log-roles
afs-roles
build-roles
cloud-roles
container-roles
deprecated-roles

View File

@ -0,0 +1,14 @@
Build project using Bazel.
**Role Variables**
.. zuul:rolevar:: bazel_build_target
Build target to specify when invoking Bazel. See
`Bazel docs <https://docs.bazel.build/versions/master/guide.html#target-patterns>`_
for details
.. zuul:rolevar:: zuul_work_dir
:default: {{ zuul.project.src_dir }}
Directory where project will be built.

View File

@ -0,0 +1 @@
zuul_work_dir: "{{ zuul.project.src_dir }}"

View File

@ -0,0 +1,9 @@
- name: Require bazel_build_target variable
fail:
msg: bazel_build_target is required for this role
when: bazel_build_target is not defined
- name: Build bazel
command: bazel build {{ bazel_build_target }}
args:
chdir: '{{ zuul_work_dir }}'

View File

@ -0,0 +1,13 @@
Download and install Bazel, if the specified version is not already present.
**Role Variables**
.. zuul:rolevar:: bazel_version
:default: '3.1.0'
The version of Bazel required.
.. zuul:rolevar:: bazel_release_url
:default: 'https://github.com/bazelbuild/bazel/releases/download'
The base URL to use when downloading Bazel releases.

View File

@ -0,0 +1,4 @@
---
bazel_version: '3.1.0'
bazel_release_url: 'https://github.com/bazelbuild/bazel/releases/download'
install_bazel_if_missing: true

View File

@ -0,0 +1,20 @@
- name: Install Bazel dependencies
become: true
package:
name:
- pkg-config
- zip
- g++
- zlib1g-dev
- unzip
- python3
state: present
- name: Install bazel on Debian
become: true
shell: |
set -ex
{{ bazel_installer_tempdir.path }}/bazel-{{ bazel_version }}-installer-linux-x86_64.sh
bazel version
args:
executable: /bin/bash

View File

@ -0,0 +1,5 @@
- name: Warn about unsupported distribution
debug:
msg: >
WARNING: Installation of Bazel on {{ ansible_distribution }} is not
supported by this role yet.

View File

@ -0,0 +1,29 @@
- name: Create temp directory
tempfile:
state: directory
register: bazel_installer_tempdir
- name: Get installer checksum
uri:
url: "{{ bazel_release_url }}/{{ bazel_version }}/bazel-{{ bazel_version }}-installer-linux-x86_64.sh.sha256"
return_content: true
register: bazel_installer_checksum
- debug: msg="Checksum is {{ bazel_installer_checksum.content.split(' ')[0] }}"
- name: Download bazel installer
get_url:
url: "{{ bazel_release_url }}/{{ bazel_version }}/bazel-{{ bazel_version }}-installer-linux-x86_64.sh"
dest: "{{ bazel_installer_tempdir.path }}/bazel-{{ bazel_version }}-installer-linux-x86_64.sh"
mode: 0755
checksum: "sha256:{{ bazel_installer_checksum.content.split(' ')[0] }}"
- debug: msg="Distribution is {{ ansible_distribution }}"
- debug: msg="OS family is {{ ansible_os_family }}"
- name: Install bazel and platform-specific dependencies
include: "{{ item }}"
with_first_found:
- "{{ ansible_distribution }}.yaml"
- "{{ ansible_os_family }}.yaml"
- "default.yaml"

View File

@ -0,0 +1,12 @@
- name: Check version of bazel
shell: bazel --version | grep -Eo '([0-9]+\.)+[0-9]+'
ignore_errors: yes
register: installed_bazel_version
- debug: msg="Current installed Bazel version is {{ installed_bazel_version.stdout }}"
- name: Download and install Bazel if needed
include_tasks: install-bazel.yaml
when:
- install_bazel_if_missing
- installed_bazel_version.stdout != bazel_version

View File

@ -0,0 +1,24 @@
- name: Test correct version of bazel installed
hosts: all
roles:
- role: ensure-bazel
post_tasks:
- name: Confirm correct version installed
shell: bazel --version | grep -Eo '([0-9]+\.)+[0-9]+'
ignore_errors: yes
register: confirmed_bazel_version
failed_when: bazel_version != confirmed_bazel_version.stdout
- name: Test bazel not installed when install_bazel_if_missing is false
hosts: all
roles:
- role: ensure-bazel
vars:
bazel_version: '1.2.1'
install_bazel_if_missing: False
post_tasks:
- name: Confirm version not installed
shell: bazel --version | grep -Eo '([0-9]+\.)+[0-9]+'
ignore_errors: yes
register: confirmed_bazel_version
failed_when: bazel_version == confirmed_bazel_version.stdout

View File

@ -0,0 +1,16 @@
- job:
name: zuul-jobs-test-ensure-bazel
description: Test the ensure-bazel role
run: test-playbooks/build-roles/ensure-bazel.yaml
# -* AUTOGENERATED *-
# The following project section is autogenerated by
# tools/update-test-platforms.py
# Please re-run to generate new job lists
- project:
check:
jobs: &id001
- zuul-jobs-test-ensure-bazel
gate:
jobs: *id001