diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index d15bbd13..db8190f1 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -1,5 +1,5 @@ name: Android SDK CI -on: [push, pull_request] +on: [pull_request] # Important Information: # -It's running in ci.antmedia.cloud and Linux hardware acceleration is enabled. @@ -16,22 +16,113 @@ on: [push, pull_request] # - For reactivecircus/android-emulator-runner@v2.27.0 , ANDROID_SDK_ROOT env should be set # in later versions, ANDROID_HOME should be set # -# - Important: In the actions-runner directory, add the following lines to the .env directory. +# - Important: In the actions-runner directory, add the following lines to the .env directory and .bashrc # ANDROID_HOME=/home/ubuntu/android # ANDROID_SDK_ROOT=/home/ubuntu/android # JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/ # +env: + GITHUB_TOKEN: ${{ secrets.G_TOKEN }} + REPO: ${{ github.repository }} + OWNER: ${{ github.repository_owner }} + OS_USERNAME: ${{ secrets.OS_USERNAME }} + OS_PASSWORD: ${{ secrets.OS_PASSWORD }} + OS_PROJECT_NAME: ${{ secrets.OS_PROJECT_NAME }} + OS_AUTH_URL: ${{ secrets.OS_AUTH_URL }} + OS_REGION_NAME: ${{ secrets.OS_REGION_NAME }} + OS_TENANT_ID: ${{ secrets.OS_TENANT_ID }} + OS_TENANT_NAME: ${{ secrets.OS_TENANT_NAME }} + OS_API_VERSION: ${{ secrets.OS_API_VERSION }} + OS_USER_DOMAIN_NAME: ${{ secrets.OS_USER_DOMAIN_NAME }} + INSTANCE_TYPE: "c3-16" + IMAGE_ID: "Ubuntu 24.04" jobs: + + setup-runner: + runs-on: ubuntu-latest + outputs: + server_id: ${{ steps.set-server-id.outputs.server_id }} + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Install Dependencies + run: | + sudo apt-get update + sudo apt-get install -y python3-pip python3-dev libffi-dev libssl-dev + sudo pip3 install python-openstackclient + + - name: server-id + id: set-server-id + run: | + REPO=$(echo "$REPO" | cut -d'/' -f2) + SERVER_ID="ci-$REPO" + echo "server_id=$SERVER_ID" >> $GITHUB_OUTPUT + + - name: Configure OpenStack CLI and Create Instance + run: | + echo "Setting up OpenStack CLI environment variables..." + echo "GITHUB_TOKEN=${GITHUB_TOKEN}" >> $GITHUB_ENV + sed -i "s/^GITHUB_TOKEN=.*$/GITHUB_TOKEN=${GITHUB_TOKEN}/" user_data.sh + sed -i "s|RUNNER_ORG=\"[^\"]*\"|RUNNER_ORG=\"$REPO\"|g" user_data.sh + SERVER_ID="${{ steps.set-server-id.outputs.server_id }}" + echo $SERVER_ID + openstack server create --flavor "$INSTANCE_TYPE" --image "$IMAGE_ID" --key-name ovh --security-group default --user-data user_data.sh --network Ext-Net $SERVER_ID + echo "Server creation initiated." + + STATUS=$(openstack server show $SERVER_ID -f value -c status) + + echo "Current server status: $STATUS" + while [[ "$STATUS" != "ACTIVE" && "$STATUS" != "ERROR" ]]; do + echo "Waiting for server to be ACTIVE. Current status: $STATUS" + sleep 10 + STATUS=$(openstack server show $SERVER_ID -f value -c status) + done + + if [[ "$STATUS" == "ERROR" ]]; then + echo "Server creation failed." + exit 1 + fi + + - name: Check runner status and wait if offline + id: check_status + run: | + RUNNER_STATUS=$(curl -s -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $GITHUB_TOKEN" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + https://api.github.com/repos/$REPO/actions/runners | jq -r '.runners[0].status') + echo "Initial Runner status is: $RUNNER_STATUS" + + while [[ "$RUNNER_STATUS" != "online" ]]; do + echo "Runner is $RUNNER_STATUS. Waiting for 10 seconds..." + sleep 10 + RUNNER_STATUS=$(curl -s -L \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $GITHUB_TOKEN" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + https://api.github.com/repos/$REPO/actions/runners | jq -r '.runners[0].status') + echo "Runner status is: $RUNNER_STATUS" + done + + echo "::set-output name=runner_status::$RUNNER_STATUS" + + - name: Cancel workflow if runner is still offline + if: steps.check_status.outputs.runner_status == 'offline' + run: | + exit 1 + unit-tests: - runs-on: ovh36 + needs: setup-runner + runs-on: self-hosted steps: - uses: actions/checkout@v3 - - name: Set up JDK 11 + - name: Set up JDK 17 uses: actions/setup-java@v3 with: - java-version: 11 + java-version: 17 distribution: 'adopt' - name: Enable KVM run: | @@ -61,6 +152,9 @@ jobs: - name: Run Multitrack Conference Test Server run: python3 multitrack-conference-server.py & + - name: Username + run : echo "Username:" `whoami` + - name: Run Tests and Generate Report uses: reactivecircus/android-emulator-runner@v2.32.0 with: @@ -113,3 +207,43 @@ jobs: SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }} SIGNING_KEY: ${{ secrets.SIGNING_KEY }} + clean: + needs: [setup-runner, unit-tests] + runs-on: ubuntu-latest + if: always() + steps: + - name: Checkout repository + uses: actions/checkout@v2 + - name: Delete runner + if: ${{ always() }} + run: | + echo "GITHUB_TOKEN=${GITHUB_TOKEN}" >> $GITHUB_ENV + RUNNER_ID=$(curl -s -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $GITHUB_TOKEN" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + https://api.github.com/repos/$REPO/actions/runners | jq -r '.runners[0].id') + + echo "Deleting runner with ID: $RUNNER_ID" + curl -X DELETE -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer $GITHUB_TOKEN" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + https://api.github.com/repos/$REPO/actions/runners/$RUNNER_ID + + echo "Runner deleted successfully." + - name: Install Dependencies + if: ${{ always() }} + run: | + sudo apt-get update + sudo apt-get install -y python3-pip python3-dev libffi-dev libssl-dev + sudo pip3 install python-openstackclient + - name: Delete CI Instance + if: ${{ always() }} + run: | + SERVER_ID="${{ needs.setup-runner.outputs.server_id }}" + echo "server id" $SERVER_ID + if [ -n "$SERVER_ID" ]; then + openstack server delete $SERVER_ID + echo $SERVER_ID + else + echo "SERVER_ID is empty, skipping server delete." + fi diff --git a/user_data.sh b/user_data.sh new file mode 100644 index 00000000..798d1ab5 --- /dev/null +++ b/user_data.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +sudo apt-get update && apt-get install jq curl unzip openjdk-17-jdk -y -qq + +GITHUB_TOKEN="" +echo $GITHUB_TOKEN > /tmp/token.txt +RUNNER_VERSION="2.317.0" +SDK_VERSION="11076708" +RUNNER_ORG="" +RUNNER_TOKEN=$(curl -s -L -X POST -H "Accept: application/vnd.github+json" -H "Authorization: Bearer $GITHUB_TOKEN" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/$RUNNER_ORG/actions/runners/registration-token | jq -r .token) +USER="ubuntu" + +# Install Runner +su - $USER -c " +mkdir -p actions-runner +cd actions-runner +curl -o actions-runner-linux-x64-$RUNNER_VERSION.tar.gz -L https://github.com/actions/runner/releases/download/v$RUNNER_VERSION/actions-runner-linux-x64-$RUNNER_VERSION.tar.gz +tar xzf ./actions-runner-linux-x64-$RUNNER_VERSION.tar.gz +cat <> ~/actions-runner/.env +ANDROID_HOME=/home/$USER/android +ANDROID_SDK_ROOT=/home/$USER/android +JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ +EOF" + +su - $USER -c " +/home/$USER/actions-runner/config.sh --url https://github.com/$RUNNER_ORG --token $RUNNER_TOKEN --unattended" + +cd /home/$USER/actions-runner/ +./svc.sh install $USER +./svc.sh start + +# Install Android SDK +su - $USER -c " +wget https://dl.google.com/android/repository/commandlinetools-linux-"$SDK_VERSION"_latest.zip +mkdir -p ~/android/cmdline-tools/latest +mkdir -p ~/android/tmp +unzip ~/commandlinetools-linux-"$SDK_VERSION"_latest.zip -d ~/android/tmp/ +mv ~/android/tmp/cmdline-tools/* ~/android/cmdline-tools/latest/ +echo $HOME >> /tmp/id.txt +whoami >> /tmp/id.txt +cat <> ~/.bashrc +export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/ +export ANDROID_HOME=/home/$USER/android/ +export PATH=/home/$USER/android/tools:\${PATH} +export PATH=/home/$USER/android/emulator:\${PATH} +export PATH=/home/$USER/android/platform-tools:\${PATH} +EOF"