-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
167 lines (147 loc) · 4.88 KB
/
.gitlab-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
stages:
- install
- setup
- test
- cleanup
variables:
SELECTEL_AUTH_TOKENS_URL: "https://cloud.api.selcloud.ru/identity/v3/auth/tokens"
SELECTEL_MOBILE_FARM_API_URL: "https://api.selectel.ru/mobfarm/api"
default:
image: thyrlian/android-sdk:10.0
before_script:
- apt-get update && apt-get install -y curl jq
- sdkmanager --update
- sdkmanager "platforms;android-34" "build-tools;34.0.0" "platform-tools" "cmdline-tools;latest"
cache:
paths:
- .gradle/caches
- .gradle/wrapper
setup-environment:
stage: setup
script:
# Obtain an authorization token
- |
response=$(curl -s -D - -X POST \
--header 'Content-Type: application/json' \
--data-raw '{
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"name": "'"$USER_NAME"'",
"domain": { "name": "'"$ACCOUNT_NAME"'" },
"password": "'"$PASSWORD"'"
}
}
},
"scope": {
"project": {
"name": "'"$PROJECT_NAME"'",
"domain": { "name": "'"$ACCOUNT_NAME"'" }
}
}
}
}' \
$SELECTEL_AUTH_TOKENS_URL)
# Extract the X-Auth-Token from the response X-Subject-Token header
- token=$(echo "$response" | grep -i "^x-subject-token" | awk '{print $2}' | tr -d '\r')
- export X_AUTH_TOKEN=$token
# Pass X-Auth-Token to the following job
- echo "X_AUTH_TOKEN=$token" >> .env
# Generate ADB key-pair
- mkdir -p ~/.android
- adb keygen ~/.android/adbkey
- adb pubkey ~/.android/adbkey > ~/.android/adbkey.pub
# Copy the keys to the project directory for artifacts
- mkdir -p adb-keys
- cp ~/.android/adbkey adb-keys/
- cp ~/.android/adbkey.pub adb-keys/
# Store adb public key at Mobile farm
- adb_pub_key=$(cat ~/.android/adbkey.pub)
- |
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 "$CI_PIPELINE_ID" --arg pubKey "$adb_pub_key" \
'{"title": $title, "publicKey": $pubKey}')" \
> response.json
- fingerprint=$(cat response.json | jq -r '.publicKey.fingerprint')
- export FINGERPRINT=$fingerprint
- echo "ADB key with fingerprint $FINGERPRINT successfully stored in Mobile farm"
# Pass fingerprint to the following job
- echo "FINGERPRINT=$fingerprint" >> .env
artifacts:
paths:
- adb-keys/
reports:
dotenv: .env
setup-device:
stage: setup
needs: ["setup-environment"]
dependencies:
- setup-environment
script:
# Assign device to a user
- |
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": "'$DEVICE_SERIAL'", "timeout": 300000}'
# Start remote ADB connection on server side
- |
curl --location --request POST "$SELECTEL_MOBILE_FARM_API_URL/v1/user/devices/$DEVICE_SERIAL/remoteConnect" \
--header "Accept: application/json" \
--header "X-Auth-Token: $X_AUTH_TOKEN" \
--output response_body.txt
# Save remote connect URL as env variable
- remote_connect_url=$(cat response_body.txt | jq -r '.remoteConnectUrl // ""')
- echo "Remote URL is $remote_connect_url"
- export UDID=$remote_connect_url
- echo "Obtained uniques device identifier to connect with value $UDID"
# Pass unique device identifier to the following job
- echo "UDID=$UDID" >> .env
- echo "Device setup completed"
artifacts:
reports:
dotenv: .env
run-tests:
stage: test
needs:
- setup-device
- setup-environment
dependencies:
- setup-environment
- setup-device
script:
# Restore ADB key pair
- mkdir -p ~/.android
- cp adb-keys/adbkey ~/.android/adbkey
- cp adb-keys/adbkey.pub ~/.android/adbkey.pub
# Connect to ADB and ensure device is available
- adb connect $UDID
- sleep 1
- adb devices
# Run tests
- ./gradlew connectedAndroidTest
cleanup:
needs:
- setup-device
- setup-environment
- run-tests
dependencies:
- setup-environment
- setup-device
stage: cleanup
script:
# Release user device
- |
curl --location --request DELETE "$SELECTEL_MOBILE_FARM_API_URL/v1/user/devices/$DEVICE_SERIAL" \
--header "Accept: application/json" \
--header "X-Auth-Token: $X_AUTH_TOKEN"
# Remove ADB key from Mobile farm
- |
curl --location --request DELETE "$SELECTEL_MOBILE_FARM_API_URL/v2/keys/adb/$FINGERPRINT" \
--header "Accept: application/json" \
--header "X-Auth-Token: $X_AUTH_TOKEN"