|
| 1 | +name: Manage ARM64 runners |
| 2 | +description: Manage ARM64 runners |
| 3 | +inputs: |
| 4 | + state-bucket: |
| 5 | + required: true |
| 6 | + description: "AWS S3 bucket name for the Terraform state" |
| 7 | + state-region: |
| 8 | + required: true |
| 9 | + description: "AWS region where state bucket has been created" |
| 10 | + role-arn: |
| 11 | + required: true |
| 12 | + description: "AWS IAM role ARN" |
| 13 | + region: |
| 14 | + required: true |
| 15 | + description: "AWS region" |
| 16 | + zone: |
| 17 | + required: true |
| 18 | + description: "AWS availability zone" |
| 19 | + infra-dir: |
| 20 | + required: true |
| 21 | + description: "Directory with infra config" |
| 22 | + action: |
| 23 | + required: true |
| 24 | + description: "Terraform action: plan, apply, destroy" |
| 25 | + ec2-type: |
| 26 | + required: true |
| 27 | + description: "EC2 instance type" |
| 28 | + ec2-ami: |
| 29 | + required: true |
| 30 | + description: "EC2 AMI type" |
| 31 | + label: |
| 32 | + required: true |
| 33 | + description: "Runners label" |
| 34 | + gh-org: |
| 35 | + required: true |
| 36 | + description: "GitHub organization" |
| 37 | + gh-app-id: |
| 38 | + required: true |
| 39 | + description: "GitHub APP ID" |
| 40 | + gh-app-install-id: |
| 41 | + required: true |
| 42 | + description: "GitHub APP install ID" |
| 43 | + gh-app-pem: |
| 44 | + required: true |
| 45 | + description: "GitHub APP private key" |
| 46 | + gh-runners-group: |
| 47 | + required: true |
| 48 | + description: "GitHub self-hosted runners group" |
| 49 | + gh-runners-count: |
| 50 | + required: true |
| 51 | + description: "GitHub virtual runners count" |
| 52 | + ssh-private-key: |
| 53 | + required: true |
| 54 | + description: "SSH private key for the runner access" |
| 55 | + ssh-public-key: |
| 56 | + required: true |
| 57 | + description: "SSH public key for the runner access" |
| 58 | + |
| 59 | +runs: |
| 60 | + using: composite |
| 61 | + steps: |
| 62 | + - name: Install Terraform |
| 63 | + uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # 3.1.2 |
| 64 | + with: |
| 65 | + terraform_version: "1.10.3" |
| 66 | + |
| 67 | + - name: Set up AWS CLI credentials |
| 68 | + uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2 |
| 69 | + with: |
| 70 | + role-to-assume: ${{ inputs.role-arn }} |
| 71 | + aws-region: ${{ inputs.region }} |
| 72 | + |
| 73 | + - name: Lookup EC2 instance FQDN |
| 74 | + if: ${{ inputs.action == 'destroy' }} |
| 75 | + id: ec2-destroy |
| 76 | + shell: bash |
| 77 | + run: | |
| 78 | + fqdn="$(aws ec2 describe-instances --region ${{ inputs.region }} | \ |
| 79 | + jq -r '.Reservations[].Instances[] | select(.State.Name == "running") | select(.Tags != null) | select(.Tags[].Value|test("^${{ inputs.label }}$")) | .PublicDnsName')" |
| 80 | + echo fqdn=$fqdn >> $GITHUB_OUTPUT |
| 81 | +
|
| 82 | + - name: Issue GH token |
| 83 | + if: ${{ inputs.action == 'destroy' }} |
| 84 | + id: gh |
| 85 | + shell: bash |
| 86 | + working-directory: ${{ inputs.infra-dir }} |
| 87 | + run: | |
| 88 | + export GITHUB_ORG="${{ inputs.gh-org }}" |
| 89 | + export GITHUB_APP_ID="${{ inputs.gh-app-id }}" |
| 90 | + export GITHUB_APP_INSTALL_ID="${{ inputs.gh-app-install-id }}" |
| 91 | + export GITHUB_APP_PEM="${{ inputs.gh-app-pem }}" |
| 92 | + token=$(./token.sh) |
| 93 | + echo token=$token >> $GITHUB_OUTPUT |
| 94 | +
|
| 95 | + - name: Delete GH runners |
| 96 | + if: ${{ inputs.action == 'destroy' }} |
| 97 | + uses: appleboy/ssh-action@8faa84277b88b6cd1455986f459aa66cf72bc8a3 # v1.2.1 |
| 98 | + with: |
| 99 | + host: ${{ steps.ec2-destroy.outputs.fqdn }} |
| 100 | + username: ubuntu |
| 101 | + key: ${{ inputs.ssh-private-key }} |
| 102 | + script: | |
| 103 | + echo "GH runners list:" |
| 104 | + /multi-runners/mr.bash list |
| 105 | +
|
| 106 | + for (( i=1; i<=${{ inputs.gh-runners-count }}; i++ )) |
| 107 | + do |
| 108 | + echo "deleting runner-$i ..." |
| 109 | + /multi-runners/mr.bash del --user runner-$i --token ${{ steps.gh.outputs.token }} || true |
| 110 | + done |
| 111 | +
|
| 112 | + - name: Set up Terraform variables |
| 113 | + working-directory: ${{ inputs.infra-dir }} |
| 114 | + shell: bash |
| 115 | + run: | |
| 116 | + cat > terraform.tfvars << EOF |
| 117 | + ssh_key_pair="${{ github.repository }}/${{ inputs.label }}" |
| 118 | + ssh_public_key="${{ inputs.ssh-public-key }}" |
| 119 | + owner="${{ github.repository }}" |
| 120 | + region="${{ inputs.region }}" |
| 121 | + zone="${{ inputs.zone }}" |
| 122 | + gh_app_id="${{ inputs.gh-app-id }}" |
| 123 | + gh_app_install_id="${{ inputs.gh-app-install-id }}" |
| 124 | + gh_org="${{ inputs.gh-org }}" |
| 125 | + gh_group="${{ inputs.gh-runners-group }}" |
| 126 | + gh_runners_count="${{ inputs.gh-runners-count }}" |
| 127 | + gh_label="${{ inputs.label }}" |
| 128 | + ec2_type="${{ inputs.ec2-type }}" |
| 129 | + ec2_ami="${{ inputs.ec2-ami }}" |
| 130 | + gh_app_pem=<<EOT |
| 131 | + ${{ inputs.gh-app-pem }} |
| 132 | + EOT |
| 133 | + EOF |
| 134 | +
|
| 135 | + - name: ${{ inputs.action }} runner |
| 136 | + working-directory: ${{ inputs.infra-dir }} |
| 137 | + shell: bash |
| 138 | + run: | |
| 139 | + make ${{ inputs.action }} STATE_REGION=${{ inputs.state-region }} \ |
| 140 | + STATE_BUCKET=${{ inputs.state-bucket }} \ |
| 141 | + STATE_FILE=${{ inputs.label }} \ |
| 142 | + AUTO_APPROVE=true |
| 143 | +
|
| 144 | + - name: Lookup EC2 instance FQDN |
| 145 | + if: ${{ inputs.action == 'apply' }} |
| 146 | + id: ec2-apply |
| 147 | + shell: bash |
| 148 | + run: | |
| 149 | + for (( i=1; i<=60; i++ )) |
| 150 | + do |
| 151 | + sleep 10 |
| 152 | + id_fqdn="$(aws ec2 describe-instances --region ${{ inputs.region }} | \ |
| 153 | + jq -r '.Reservations[].Instances[] | select(.Tags != null) | select(.Tags[].Value|test("^${{ inputs.label }}$")) | .InstanceId + "," + .PublicDnsName')" || true |
| 154 | + id=$(echo "$id_fqdn" | cut -d "," -f 1) |
| 155 | + fqdn=$(echo "$id_fqdn" | cut -d "," -f 2) |
| 156 | + ready_count="$(aws ec2 describe-instance-status --no-cli-pager --instance-ids $id --region ${{ inputs.region }} | \ |
| 157 | + jq '.InstanceStatuses[] | select(.InstanceStatus.Details[].Status == "passed") | .InstanceId ' | wc -l)" || true |
| 158 | + if [[ $ready_count -eq 1 ]] |
| 159 | + then |
| 160 | + echo "EC2 instance is ready now [id: $id, fqdn: $fqdn]" |
| 161 | + echo fqdn=$fqdn >> $GITHUB_OUTPUT |
| 162 | + exit 0 |
| 163 | + fi |
| 164 | + echo "EC2 instance is not ready yet ..." |
| 165 | + done |
| 166 | +
|
| 167 | + echo "EC2 instance not ready!" |
| 168 | + exit 1 |
0 commit comments