Skip to content

Commit

Permalink
Add self-hosted runner
Browse files Browse the repository at this point in the history
  • Loading branch information
muratugureminoglu authored Jul 30, 2024
1 parent 53e0557 commit c7beb7d
Showing 1 changed file with 135 additions and 9 deletions.
144 changes: 135 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,106 @@ permissions:
pull-requests: write

jobs:
test_repo:
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..."
export OS_USERNAME=$OS_USERNAME
export OS_PASSWORD=$OS_PASSWORD
export OS_PROJECT_NAME=$OS_PROJECT_NAME
export OS_AUTH_URL=$OS_AUTH_URL
export OS_REGION_NAME=$OS_REGION_NAME
export OS_USER_DOMAIN_NAME
export OS_API_VERSION
export OS_TENANT_NAME
export OS_TENANT_ID
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
local_tests:
needs: setup-runner
runs-on: self-hosted
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Minikube
run: |
sudo apt-get update
sudo apt-get install -y conntrack
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
- name: Start Minikube
- name: Stop/Start Minikube - Docker
run: |
sudo usermod -aG docker $USER
newgrp docker
sudo systemctl start docker
echo $USER
sudo usermod -aG docker $USER && newgrp docker
sudo chown $USER /var/run/docker.sock
minikube start --driver=docker
- name: Install Helm
Expand Down Expand Up @@ -87,7 +169,7 @@ jobs:
- name: Stop Minikube
run: minikube stop
release:
needs: test_repo
needs: local_tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand All @@ -103,3 +185,47 @@ jobs:
prerelease: false
files: |
antmedia-${{ steps.version.outputs.version }}.tgz
clean:
needs: [setup-runner, local_tests]
runs-on: ubuntu-latest
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
export OS_USERNAME=$OS_USERNAME
export OS_PASSWORD=$OS_PASSWORD
export OS_PROJECT_NAME=$OS_PROJECT_NAME
export OS_AUTH_URL=$OS_AUTH_URL
export OS_REGION_NAME=$OS_REGION_NAME
export OS_USER_DOMAIN_NAME
export OS_API_VERSION
export OS_TENANT_NAME
export OS_TENANT_ID
openstack server delete $SERVER_ID

0 comments on commit c7beb7d

Please sign in to comment.