-
Notifications
You must be signed in to change notification settings - Fork 0
198 lines (161 loc) · 6.67 KB
/
ci.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
name: Android CI Example Workflow
on:
push:
branches:
- main
pull_request:
env:
SELECTEL_AUTH_TOKENS_URL: "https://cloud.api.selcloud.ru/identity/v3/auth/tokens"
SELECTEL_MOBILE_FARM_API_URL: "https://api.selectel.ru/mobfarm/api"
USER_NAME: ${{ secrets.USER_NAME }}
PASSWORD: ${{ secrets.PASSWORD }}
PROJECT_NAME: ${{ secrets.PROJECT_NAME }}
ACCOUNT_NAME: ${{ secrets.ACCOUNT_NAME }}
DEVICE_SERIAL: ${{ secrets.DEVICE_SERIAL }}
jobs:
run-autotests:
runs-on: ubuntu-latest
container:
image: thyrlian/android-sdk:10.0 # Prebuilt Android SDK Docker image
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install dependencies
run: |
apt-get update
apt-get install -y curl jq
- name: Update Android SDK
run: |
sdkmanager --update # Update the SDK to ensure the latest tools are available
sdkmanager "platforms;android-34" "build-tools;34.0.0" "platform-tools" "cmdline-tools;latest" # Install required SDK components
- name: Install Gradle dependencies
run: ./gradlew dependencies
- name: Build APK
run: ./gradlew assembleDebug
- name: Obtain Authorization Token
run: |
response=$(curl -s -D - -X POST \
--header 'Content-Type: application/json' \
--data-raw '{
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"name": "${{ env.USER_NAME }}",
"domain": { "name": "${{ env.ACCOUNT_NAME }}" },
"password": "${{ env.PASSWORD }}"
}
}
},
"scope": {
"project": {
"name": "${{ env.PROJECT_NAME }}",
"domain": { "name": "${{ env.ACCOUNT_NAME }}" }
}
}
}
}' \
$SELECTEL_AUTH_TOKENS_URL)
token=$(echo "$response" | grep -i "^x-subject-token" | awk '{print $2}' | tr -d '\r')
# Check if token is successfully obtained
if [ -z "$token" ]; then
echo "Error: unable to obtain X-Auth-Token."
exit 1
fi
echo "Successfully obtained X-Auth-Token."
# Save the token for future steps
echo "X_AUTH_TOKEN=$token" >> .env
- name: Generate ADB Key Pair
run: |
# Create the default .android directory for storing ADB keys
mkdir -p ~/.android
# Generate a new ADB key
adb keygen ~/.android/adbkey
# Extract the public key
adb pubkey ~/.android/adbkey > ~/.android/adbkey.pub
- name: Store ADB Key in Mobile Farm
shell: bash
run: |
source .env
# Get the generated ADB public key
adb_pub_key=$(cat ~/.android/adbkey.pub)
# Send the ADB public key to the Mobile Farm API
curl --location "$SELECTEL_MOBILE_FARM_API_URL/v2/keys/adb" \
--header "Content-Type: application/json" \
--header "X-Auth-Token: $X_AUTH_TOKEN" \
--data "$(jq -n --arg title "$GITHUB_RUN_ID" --arg pubKey "$adb_pub_key" \
'{"title": $title, "publicKey": $pubKey}')" \
> response.json
# Extract fingerprint from the response
fingerprint=$(cat response.json | jq -r '.publicKey.fingerprint')
# Check if key was successfully stored
if [ -z "$fingerprint" ]; then
echo "Error: ADB key was not successfully stored."
exit 1
fi
echo "Successfully stored ADB key."
# Save the fingerprint for future use
echo "FINGERPRINT=$fingerprint" >> $GITHUB_ENV
- name: Assign Device
shell: bash
run: |
source .env
# Assign device from the Mobile Farm
curl --location --request POST "$SELECTEL_MOBILE_FARM_API_URL/v1/user/devices" \
--header "Accept: application/json" \
--header "X-Auth-Token: $X_AUTH_TOKEN" \
--header "Content-Type: application/json" \
--data '{"serial": "${{ env.DEVICE_SERIAL }}", "timeout": 300000}'
- name: Start Remote ADB Connection
shell: bash
run: |
source .env
# Start remote ADB connection for the device
curl --location --request POST "$SELECTEL_MOBILE_FARM_API_URL/v1/user/devices/${{ env.DEVICE_SERIAL }}/remoteConnect" \
--header "Accept: application/json" \
--header "X-Auth-Token: $X_AUTH_TOKEN" \
--output response_body.txt
# Extract remote connect URL
remote_connect_url=$(cat response_body.txt | jq -r '.remoteConnectUrl // ""')
# Check if remote connection URL was returned
if [ -z "$remote_connect_url" ]; then
echo "Error: unable to start remote ADB connect session."
exit 1
fi
echo "Successfully started remote ADB connect session."
# Save the unique device identifier (UDID) for future steps
echo "UDID=$remote_connect_url" >> $GITHUB_ENV
- name: Connect to ADB
run: |
# Connect to the remote device via ADB
adb connect ${{ env.UDID }}
# Wait for the connection to stabilize
sleep 1
# Verify the device connection
adb devices
echo "Device is ready"
- name: Run Tests
run: |
export ADB_INSTALL_TIMEOUT=5
./gradlew connectedAndroidTest --info
- name: Release Device
# Ensure this step runs even if previous steps fail
if: ${{ always() }}
shell: bash
run: |
source .env
# Release the device after testing
curl --location --request DELETE "$SELECTEL_MOBILE_FARM_API_URL/v1/user/devices/${{ env.DEVICE_SERIAL }}" \
--header "Accept: application/json" \
--header "X-Auth-Token: $X_AUTH_TOKEN"
- name: Remove ADB Key
# Run if ADB key was generated and stored
if: ${{ always() && env.FINGERPRINT != null && env.FINGERPRINT != '' }}
shell: bash
run: |
source .env
# Remove the ADB key from Mobile Farm after testing
curl --location --request DELETE "$SELECTEL_MOBILE_FARM_API_URL/v2/keys/adb/${{ env.FINGERPRINT }}" \
--header "Accept: application/json" \
--header "X-Auth-Token: $X_AUTH_TOKEN"