Skip to content

Commit

Permalink
feat: auto-select zone based on availability
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoaldamav committed Nov 2, 2023
1 parent a518c5f commit f1940b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:

- name: Auto Approve
run: |
echo "PR is from a team member, auto approving."
echo "Is not a PR, auto approving."
windows-tests:
needs: [require-approval, get-team-members]
Expand Down
25 changes: 14 additions & 11 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,21 @@ jobs:
REGION=$(echo $ZONE | sed 's/\([a-z]*-[a-z]*[1-9]\)-[a-z]*/\1/')
QUOTAS_JSON=$(gcloud compute regions describe $REGION --project=${{ secrets.GCP_PROJECT_ID }} --format=json)
SSD_QUOTA=$(echo $QUOTAS_JSON | jq '.quotas[] | select(.metric == "SSD_TOTAL_GB") | .limit')
if (( $(echo "$SSD_QUOTA >= 40" | bc -l) )); then
if (( $(echo "$SSD_QUOTA >= 20" | bc -l) )); then
echo "Selected Zone: $ZONE"
echo "ZONE=$ZONE" >> $GITHUB_ENV
break
fi
done
- name: Set default zone
run: |
gcloud config set compute/zone ${{ env.ZONE }}
- name: Create GCP VM with Spot provisioning
run: |
gcloud compute instances create $INSTANCE_NAME \
--project=${{ secrets.GCP_PROJECT_ID }} \
--zone=${{ env.ZONE }} \
--machine-type=e2-small \
--source-machine-image=linux-rs \
--no-restart-on-failure \
Expand All @@ -76,7 +79,7 @@ jobs:
- name: Wait for VM to be ready
run: |
for i in {1..30}; do
if gcloud compute ssh $INSTANCE_NAME --command="echo ready" --zone=${{ env.ZONE }} --ssh-key-file=./gcloud; then
if gcloud compute ssh $INSTANCE_NAME --command="echo ready" --ssh-key-file=./gcloud; then
break
else
echo "Retrying ($i)..."
Expand All @@ -88,41 +91,41 @@ jobs:
run: |
SCRIPT_PATH=scripts/linux-${{ matrix.fs }}.sh
SCRIPT_CONTENT=$(cat $SCRIPT_PATH)
gcloud compute ssh $INSTANCE_NAME --zone=${{ env.ZONE }} \
gcloud compute ssh $INSTANCE_NAME \
--ssh-key-file=./gcloud \
--command="echo '$SCRIPT_CONTENT' > init-script.sh && chmod +x init-script.sh && ./init-script.sh"
- name: Copy Repo to GCP VM
run: |
gcloud compute scp --recurse ../${{ env.REPO_NAME }} ${{ secrets.GCP_USER }}@$INSTANCE_NAME:/mnt/${{ matrix.fs }}/code --zone=${{ env.ZONE }} --ssh-key-file=./gcloud
gcloud compute scp --recurse ../${{ env.REPO_NAME }} ${{ secrets.GCP_USER }}@$INSTANCE_NAME:/mnt/${{ matrix.fs }}/code --ssh-key-file=./gcloud
- name: List files on VM
run: |
gcloud compute ssh ${{ secrets.GCP_USER }}@$INSTANCE_NAME --zone=${{ env.ZONE }} \
gcloud compute ssh ${{ secrets.GCP_USER }}@$INSTANCE_NAME \
--ssh-key-file=./gcloud \
--command="ls -la /mnt/${{ matrix.fs }}/code/${{ env.REPO_NAME }}"
- name: Run Tests
run: |
gcloud compute ssh ${{ secrets.GCP_USER }}@$INSTANCE_NAME --zone=${{ env.ZONE }} \
gcloud compute ssh ${{ secrets.GCP_USER }}@$INSTANCE_NAME \
--ssh-key-file=./gcloud \
--command="export PATH=\$PATH:~/.cargo/bin && cd /mnt/${{ matrix.fs }}/code/${{ env.REPO_NAME }} && cargo test"
# Cleanup
- name: Delete GCP VM if exists
if: always()
run: |
if gcloud compute instances describe $INSTANCE_NAME --zone=${{ env.ZONE }} > /dev/null 2>&1; then
gcloud compute instances delete $INSTANCE_NAME --zone=${{ env.ZONE }} --quiet
if gcloud compute instances describe $INSTANCE_NAME > /dev/null 2>&1; then
gcloud compute instances delete $INSTANCE_NAME --quiet
else
echo "VM does not exist. Skipping deletion."
fi
- name: Delete GCP Disk if exists
if: always()
run: |
if gcloud compute disks describe $DISK_NAME --zone=${{ env.ZONE }} > /dev/null 2>&1; then
gcloud compute disks delete $DISK_NAME --zone=${{ env.ZONE }} --quiet
if gcloud compute disks describe $DISK_NAME > /dev/null 2>&1; then
gcloud compute disks delete $DISK_NAME --quiet
else
echo "Disk does not exist. Skipping deletion."
fi
36 changes: 27 additions & 9 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,30 @@ jobs:
- name: Set default project
run: |
gcloud config set project ${{ secrets.GCP_PROJECT_ID }}
- name: Fetch available zones and set dynamic zone
run: |
sudo apt-get install -y jq
AVAILABLE_ZONES=$(gcloud compute zones list --project=${{ secrets.GCP_PROJECT_ID }} --format="value(name)")
for ZONE in $AVAILABLE_ZONES; do
REGION=$(echo $ZONE | sed 's/\([a-z]*-[a-z]*[1-9]\)-[a-z]*/\1/')
QUOTAS_JSON=$(gcloud compute regions describe $REGION --project=${{ secrets.GCP_PROJECT_ID }} --format=json)
SSD_QUOTA=$(echo $QUOTAS_JSON | jq '.quotas[] | select(.metric == "SSD_TOTAL_GB") | .limit')
if (( $(echo "$SSD_QUOTA >= 100" | bc -l) )); then
echo "Selected Zone: $ZONE"
echo "ZONE=$ZONE" >> $GITHUB_ENV
break
fi
done
- name: Set default zone
run: |
gcloud config set compute/zone ${{ env.ZONE }}
- name: Create GCP VM with Spot provisioning
run: |
gcloud compute instances create $INSTANCE_NAME \
--project=${{ secrets.GCP_PROJECT_ID }} \
--zone=us-east1-b \
--machine-type=e2-medium \
--source-machine-image=windows-rs \
--no-restart-on-failure \
Expand All @@ -61,32 +79,32 @@ jobs:
- name: Copy the repo
run: |
gcloud compute ssh ${{ secrets.GCP_USER }}@$INSTANCE_NAME --zone=us-east1-b --command="powershell -Command \"mkdir D:\\\\code\\\\${{ env.REPO_NAME }}\"" --ssh-key-file=./gcloud
gcloud compute scp --recurse --zone=us-east1-b ../${{ env.REPO_NAME }} ${{ secrets.GCP_USER }}@$INSTANCE_NAME:"D:\code" --ssh-key-file=./gcloud
gcloud compute ssh ${{ secrets.GCP_USER }}@$INSTANCE_NAME --command="powershell -Command \"mkdir D:\\\\code\\\\${{ env.REPO_NAME }}\"" --ssh-key-file=./gcloud
gcloud compute scp --recurse ../${{ env.REPO_NAME }} ${{ secrets.GCP_USER }}@$INSTANCE_NAME:"D:\code" --ssh-key-file=./gcloud
- name: List files in project directory
run:
gcloud compute ssh ${{ secrets.GCP_USER }}@$INSTANCE_NAME --zone=us-east1-b --command="powershell -Command \"cd D:\\\\code\\\\${{ env.REPO_NAME }}; ls\"" --ssh-key-file=./gcloud
gcloud compute ssh ${{ secrets.GCP_USER }}@$INSTANCE_NAME --command="powershell -Command \"cd D:\\\\code\\\\${{ env.REPO_NAME }}; ls\"" --ssh-key-file=./gcloud

- name: Run tests
run:
gcloud compute ssh ${{ secrets.GCP_USER }}@$INSTANCE_NAME --zone=us-east1-b --command="powershell -Command \"cd D:\\\\code\\\\${{ env.REPO_NAME }}; cargo test\"" --ssh-key-file=./gcloud
gcloud compute ssh ${{ secrets.GCP_USER }}@$INSTANCE_NAME --command="powershell -Command \"cd D:\\\\code\\\\${{ env.REPO_NAME }}; cargo test\"" --ssh-key-file=./gcloud

# Cleanup
- name: Delete GCP VM if exists
if: always()
run: |
if gcloud compute instances describe $INSTANCE_NAME --zone=us-east1-b > /dev/null 2>&1; then
gcloud compute instances delete $INSTANCE_NAME --zone=us-east1-b --quiet
if gcloud compute instances describe $INSTANCE_NAME > /dev/null 2>&1; then
gcloud compute instances delete $INSTANCE_NAME --quiet
else
echo "VM does not exist. Skipping deletion."
fi
- name: Delete GCP Disk if exists
if: always()
run: |
if gcloud compute disks describe $DISK_NAME --zone=us-east1-b > /dev/null 2>&1; then
gcloud compute disks delete $DISK_NAME --zone=us-east1-b --quiet
if gcloud compute disks describe $DISK_NAME > /dev/null 2>&1; then
gcloud compute disks delete $DISK_NAME --quiet
else
echo "Disk does not exist. Skipping deletion."
fi

0 comments on commit f1940b9

Please sign in to comment.