From 03771be85070e77c164c4181e8913dd69af17481 Mon Sep 17 00:00:00 2001 From: "Kakkoth Vayalambron, Shibu" Date: Thu, 13 Jun 2024 14:53:39 -0700 Subject: [PATCH] RDKE-61: Open container for RDK CI Reason for change: Container with common tools for unit and integration test execution as part of CI process. This would help in shift left test and TDD development. --- .github/workflows/docker-publish.yml | 37 +++++++++ .github/workflows/verify-build.yml | 32 ++++++++ Dockerfile | 48 +++++++++++ gtest-json-result-push.py | 115 +++++++++++++++++++++++++++ 4 files changed, 232 insertions(+) create mode 100644 .github/workflows/docker-publish.yml create mode 100644 .github/workflows/verify-build.yml create mode 100644 Dockerfile create mode 100755 gtest-json-result-push.py diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..08fd2a3 --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,37 @@ +name: Build and Push amd64 Docker Image to GHCR + +on: + push: + branches: + - main + + tags: + - '[0-9]+.[0-9]+.[0-9]+' + +jobs: + build-and-push: + runs-on: ubuntu-latest + if : ${{ github.event_name == 'push' && github.ref_type == 'tag' }} + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to GHCR + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GHCR_TOKEN }} + + - name: Build and push multi-platform Docker image + uses: docker/build-push-action@v4 + with: + context: . + platforms: linux/amd64, linux/arm64 + push: true + tags: | + ghcr.io/${{ github.repository }}:${{ github.ref_name }} + ghcr.io/${{ github.repository }}:latest diff --git a/.github/workflows/verify-build.yml b/.github/workflows/verify-build.yml new file mode 100644 index 0000000..e8a4902 --- /dev/null +++ b/.github/workflows/verify-build.yml @@ -0,0 +1,32 @@ +name: Build and Push amd64 Docker Image to GHCR + +on: + pull_request: + branches: + - main, develop + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to GHCR + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GHCR_TOKEN }} + + - name: Build and push multi-platform Docker image + uses: docker/build-push-action@v4 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: false + tags: | + ghcr.io/${{ github.repository }}:${{ github.sha }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..798bd13 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,48 @@ +# syntax=docker/dockerfile:1 + +FROM ubuntu:22.04 +ARG DEBIAN_FRONTEND=noninteractive + +LABEL org.opencontainers.image.source https://github.com/rdkcentral/docker-rdk-ci +LABEL org.opencontainers.image.authors="RDK Engineers" +LABEL org.opencontainers.image.description="RDK CI Docker Image" +LABEL org.opencontainers.image.architectures="amd64, arm64" + +# Add instructions to install autotools +RUN apt-get update && apt-get install -y build-essential \ + wget openssl tar vim git git-lfs \ + libtool autotools-dev automake zlib1g-dev ninja-build meson \ + libglib2.0-dev python3-distutils libcurl4-openssl-dev \ + libmsgpack-dev libsystemd-dev libssl-dev libcjson-dev python3-pip libsqlite3-dev \ + libgtest-dev libgmock-dev libjansson-dev libbsd-dev tcl-dev \ + libboost-all-dev libwebsocketpp-dev libcunit1 libcunit1-dev libunwind-dev \ + gdb valgrind lcov clang g++ wget gperf ruby-full + +# Common python packages +RUN pip3 install xmltodict requests jsonref + +RUN gem install ceedling + +# Commands that needs to be executed on container +RUN mkdir -p WORK_DIR +RUN cd WORK_DIR +RUN wget https://github.com/Kitware/CMake/releases/download/v3.17.3/cmake-3.17.3.tar.gz +RUN tar -zxvf cmake-3.17.3.tar.gz + +RUN cd cmake-3.17.3/ && ./bootstrap && make && make install + +# Clean up all source and build artifacts +RUN cd .. && rm -rf WORK_DIR && rm -rf cmake-3.17.3.tar.gz && rm -rf cmake-3.17.3 + +# Install gtest libraries +RUN cd /usr/src/googletest/googlemock/ && mkdir build && cmake .. && make && make install + +RUN mkdir -p /home/mount + +# Trim down the docker image size +RUN rm -rf /var/lib/apt/lists/* + +# Install tools for publishing test results to automation +COPY gtest-json-result-push.py /usr/local/bin/gtest-json-result-push.py + +CMD [ "/bin/bash" ] diff --git a/gtest-json-result-push.py b/gtest-json-result-push.py new file mode 100755 index 0000000..1f25b88 --- /dev/null +++ b/gtest-json-result-push.py @@ -0,0 +1,115 @@ +#!/usr/bin/python3 + +#################################################################################### +# If not stated otherwise in this file or this component's Licenses.txt file the +# following copyright and licenses apply: +# +# Copyright 2024 RDK Management +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#################################################################################### + + +# This utility depends on the xmltodict, json and requests python library + +import sys +import xmltodict +import json +import requests +import os + + +from requests.auth import HTTPBasicAuth + +# function to read json file as input and return minified json data as string +def json_string(xml_file_path, git_folder_path): + + # Change current working directory to the script directory + os.chdir(git_folder_path) + commit_id = os.popen('git log --pretty="format:%H" -n 1').read() + #The below command will work on the local docker container to get the component name + #component_name=os.popen('git remote -v | grep fetch | cut -d "/" -f2 | cut -d "." -f1').read() + + #The below command will work on the github action container to get the component name + component_name=os.popen('git remote -v | grep fetch | cut -d "/" -f5 | cut -d " " -f1').read() + developer=os.popen('git log --pretty="format:%an" -n 1').read() + jira_ticket=os.popen('git log --pretty="format:%s" -n 1 | cut -d ":" -f1').read() + + print("commid_id : " + commit_id) + print("component_name : " + component_name) + print("developer : " + developer) + print("jira_ticket message: " + jira_ticket) + + json_root = { + "commit_id": commit_id.strip(), + "component_name": component_name.strip(), + "developer": developer.strip(), + "jira_ticket": jira_ticket.strip() + } + + with open(xml_file_path) as json_file: + # minify the json message + json_data = json.dumps(json.loads(json_file.read())) + json_root["test_cases_results"] = json.loads(json_data) + + + return json.dumps(json_root) + +# function to http post json data to a url +def post_json(json_data, url): + + + # Get username and password from env variable + username = os.environ.get('AUTOMATICS_UNAME') + password = os.environ.get('AUTOMATICS_PWD') + passcode = os.environ.get('AUTOMATICS_PASSCODE') + passvalue = "Basic " + passcode + headers = { + 'Content-type': 'application/json', + 'Authorization': passvalue + + } + + response = requests.post(url, data=json_data, headers=headers) + print(response) + +# Loop through all the xml files in the folder and call the xml_to_json function +# to convert xml to json and then call the post_json function to post the json data to a url +def post_json_to_url(json_folder_path, url, git_folder_path): + + try : + + for filename in os.listdir(json_folder_path): + if filename.endswith(".json"): + json_file_path = os.path.join(json_folder_path, filename) + json_data = json_string(json_file_path, git_folder_path) + print(json_data) + post_json(json_data, url) + + except FileNotFoundError as e: + print(f"Test results are not found in the specified {git_folder_path} folder.") + print(f"Please check the folder path and try again. Error: {e}") + +def main(): + + # Check if the script is called with the correct number of arguments + if len(sys.argv) != 4: + print("Usage: python3 gtest-xml-json-coverter.py ") + sys.exit(1) + + + print("Converting xml to json") + post_json_to_url(sys.argv[1], sys.argv[2], sys.argv[3]) + +if __name__ == "__main__": + main()