diff --git a/.github/workflows/do-destroy-runner.yaml b/.github/workflows/do-destroy-runner.yaml new file mode 100644 index 0000000..dd0d72d --- /dev/null +++ b/.github/workflows/do-destroy-runner.yaml @@ -0,0 +1,39 @@ +name: Destroy Digital Ocean runners + +on: + workflow_call: + inputs: + name: + description: "Droplet name thats will be removed" + required: true + type: string + + secrets: + access-token: + description: 'A token passed from the caller workflow' + required: true + + do-access-token: + description: 'An API token to access DigitalOcean with' + required: true + +jobs: + spin-up: + name: Destroy ${{ inputs.name }} + runs-on: ubuntu-latest + + steps: + # Install Doctl + - name: Install doctl + uses: digitalocean/action-doctl@v2 + with: + token: ${{ secrets.do-access-token }} + + # Create the VM + - name: Delete vm ${{ inputs.name }} + run: doctl compute droplet delete ${{ inputs.name }} -f + + - name: Delete the runner + run: | + RUNNER_ID=$(curl -s -X GET https://api.github.com/repos/${{ github.repository }}/actions/runners -H "accept: application/json" -H "authorization: token ${{ secrets.access-token }}" | jq -er '.runners[] | select(.name == "${{ inputs.name }}").id') + curl -s -X DELETE https://api.github.com/repos/${{ github.repository }}/actions/runners/$RUNNER_ID -H "accept: application/json" -H "authorization: token ${{ secrets.access-token }}" diff --git a/.github/workflows/do-make-runner.yaml b/.github/workflows/do-make-runner.yaml new file mode 100644 index 0000000..8d3174f --- /dev/null +++ b/.github/workflows/do-make-runner.yaml @@ -0,0 +1,92 @@ +name: Make Digital Ocean runners + +on: + workflow_call: + inputs: + name: + description: "Droplet name that will be created" + required: true + type: string + + image: + description: "Droplet image" + required: false + default: ubuntu-22-04-x64 + type: string + + region: + description: "Droplet region" + required: false + default: fra1 + type: string + + size: + description: "Droplet size cpu/ram" + required: false + default: gd-4vcpu-16gb + type: string + + label: + description: "Droplet labels" + required: true + type: string + + secrets: + access-token: + description: 'A token passed from the caller workflow' + required: true + + do-access-token: + description: 'An API token to access DigitalOcean with' + required: true + +jobs: + spin-up: + name: Make ${{ inputs.name }} + runs-on: ubuntu-latest + outputs: + instance-name: ${{ steps.get-name.outputs.name }} + + steps: + # Install Doctl + - name: Install doctl + uses: digitalocean/action-doctl@v2 + with: + token: ${{ secrets.do-access-token }} + + - name: Get instance name + id: get-name + run: echo "name=${{ inputs.name }}" >> $GITHUB_OUTPUT + + # Template for cloud-init install vagrant and virtualbox + - name: Template out file + run: | + cat << EOF > ./user-data + #!/bin/bash + apt -y update + apt -y install jq curl unzip + + useradd -m gha-runner + echo "gha-runner ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/gha-runner + export RUNNER_CFG_PAT="${{ secrets.access-token }}" + cd /home/gha-runner + curl -s https://raw.githubusercontent.com/actions/runner/main/scripts/create-latest-svc.sh | bash -s -- -s ${{ github.repository }} -u gha-runner -l ${{ inputs.label }} + EOF + + # Create the VM + - name: Create vm for ${{ inputs.name }} + run: doctl compute droplet create ${{ inputs.name }} --image ${{ inputs.image }} --region ${{ inputs.region }} --size ${{ inputs.size }} --wait --user-data-file ./user-data > /dev/null + + - name: Wait for VM to come online + run: | + sleep 30 + for ATTEMPT in {1..10}; + do + echo "#### Attempt $ATTEMPT ####" + if curl -s -X GET https://api.github.com/repos/${{ github.repository }}/actions/runners -H "accept: application/json" -H "authorization: token ${{ secrets.access-token }}" | jq -er '.runners[] | select(.name == "${{ inputs.name }}") | has("status")'; + then + exit 0 + fi + sleep 30 + done + exit 1 diff --git a/README.md b/README.md index 77e0bea..a14ec7c 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,10 @@ jobs: Action for checking helm charts for compliance with the rules for formatting yaml files and for compliance with the configured rules for kubernetes manifests. -### k8s Deprecated recources validator +### Kubernetes Deprecated recources validator Action for check deprecated api and other resources in k8s yaml manifests + +### DigitalOcean make/destoy self-hosted runners + +Action for make new Digital Ocean droplet with provided parameters, connect it like a self-hosted runner to repo, after job finished - remove self-hosted runner and destroy the droplet