-
Notifications
You must be signed in to change notification settings - Fork 0
131 lines (114 loc) · 4.94 KB
/
linux.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
name: E2E Linux (Google Cloud)
on:
push:
branches:
- main
workflow_dispatch:
jobs:
linux-e2e:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
fs: ['btrfs', 'xfs', 'ext4']
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Authenticate with Google Cloud
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_CREDENTIALS }}
- name: Setup GCP environment
uses: google-github-actions/setup-gcloud@v1
- name: Generate random instance and disk name
run: |
INSTANCE_UUID=$(uuidgen | cut -d '-' -f1)
INSTANCE_NAME="vm-$INSTANCE_UUID"
DISK_NAME="$INSTANCE_NAME-disk"
echo "INSTANCE_NAME=$INSTANCE_NAME" >> $GITHUB_ENV
echo "DISK_NAME=$DISK_NAME" >> $GITHUB_ENV
- name: Get current repo name
run: |
REPO_NAME=$(basename $(git rev-parse --show-toplevel))
echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV
- name: Set default project
run: |
gcloud config set project ${{ secrets.GCP_PROJECT_ID }}
- name: Generate SSH key pair
run: |
ssh-keygen -t rsa -b 4096 -f ./gcloud -C "${{ secrets.GCP_MAIL }}" -N ""
- 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 >= 40" | bc -l) )); then
echo "Selected Zone: $ZONE"
echo "ZONE=$ZONE" >> $GITHUB_ENV
break
fi
done
- 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 \
--maintenance-policy=TERMINATE \
--provisioning-model=SPOT \
--instance-termination-action=DELETE \
--service-account=${{ secrets.GCP_SERVICE_ACCOUNT }} \
--create-disk=size=10GB,device-name=$DISK_NAME,mode=rw,type=projects/${{ secrets.GCP_PROJECT_ID }}/zones/${{ env.ZONE }}/diskTypes/pd-balanced \
--metadata ssh-keys="${{ secrets.GCP_USER }}:$(cat ./gcloud.pub)"
- 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
break
else
echo "Retrying ($i)..."
sleep 2
fi
done
- name: Run init script
run: |
SCRIPT_PATH=scripts/linux-${{ matrix.fs }}.sh
SCRIPT_CONTENT=$(cat $SCRIPT_PATH)
gcloud compute ssh $INSTANCE_NAME --zone=${{ env.ZONE }} \
--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
- name: List files on VM
run: |
gcloud compute ssh ${{ secrets.GCP_USER }}@$INSTANCE_NAME --zone=${{ env.ZONE }} \
--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 }} \
--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
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
else
echo "Disk does not exist. Skipping deletion."
fi