diff --git a/scripts/basic_upload_apks_service_account.py b/scripts/basic_upload_apks_service_account.py index 80cdd328cf..353d7b5241 100755 --- a/scripts/basic_upload_apks_service_account.py +++ b/scripts/basic_upload_apks_service_account.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 # # Copyright 2014 Marta Rodriguez. # @@ -18,7 +18,7 @@ import argparse -from apiclient.discovery import build +from googleapiclient.discovery import build import httplib2 from oauth2client.service_account import ServiceAccountCredentials from oauth2client import client @@ -40,7 +40,7 @@ def main(): # Load the key in PKCS 12 format that you downloaded from the Google APIs # Console when you created your Service account. - with open('play_developer.p12', 'rb') as f: + with open('.ci/play_developer.p12', 'rb') as f: key = f.read() # Create an httplib2.Http object to handle our HTTP requests and authorize it @@ -49,7 +49,7 @@ def main(): # address associated with the key that was created. credentials = ServiceAccountCredentials.from_p12_keyfile( SERVICE_ACCOUNT_EMAIL, - '../.ci/play_developer.p12', + '.ci/play_developer.p12', scopes='https://www.googleapis.com/auth/androidpublisher') http = httplib2.Http() http = credentials.authorize(http) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 6185162279..1f868dc258 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,23 +1,29 @@ #!/bin/bash - set -e +source scripts/travis_env.sh + if [ "${TRAVIS_SECURE_ENV_VARS}" = "true" ]; then if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then echo -e "\e[31mDeploying app to pull request\e[0m" - curl -u m-kuhn:${GITHUB_API_TOKEN} -X POST --data '{"body": "Uploaded test apks for [armv7](https://opengis.ch/download/qfield/ci-builds/qfield-dev-'${TRAVIS_PULL_REQUEST}'-'${TRAVIS_COMMIT}'-armv7.apk) and [x86](https://opengis.ch/download/qfield/ci-builds/qfield-dev-'${TRAVIS_PULL_REQUEST}'-'${TRAVIS_COMMIT}'-x86.apk)"}' https://api.github.com/repos/opengisch/QField/issues/${TRAVIS_PULL_REQUEST}/comments + curl -u m-kuhn:${GITHUB_API_TOKEN} -X POST --data '{"body": "Uploaded test apks for [armv7](https://opengis.ch/download/qfield/ci-builds/qfield-dev-'${UPLOAD_ARTIFACT_ID}'-'${TRAVIS_COMMIT}'-armv7.apk) and [x86](https://opengis.ch/download/qfield/ci-builds/qfield-dev-'${UPLOAD_ARTIFACT_ID}'-'${TRAVIS_COMMIT}'-x86.apk)"}' https://api.github.com/repos/opengisch/QField/issues/${TRAVIS_PULL_REQUEST}/comments fi if [ "${TRAVIS_TAG}X" != "X" ]; then - echo -e "\e[31mDeploying app to releases\e[0m" + echo -e "\e[31mStarting to deploy a new release\e[0m" openssl aes-256-cbc -K $encrypted_play_upload_key -iv $encrypted_play_upload_iv -in .ci/play_developer.p12.enc -out .ci/play_developer.p12 -d - wget -o /tmp/qfield-${TRAVIS_TAG}-armv7.apk https://opengis.ch/download/qfield/ci-builds/qfield-dev-'${TRAVIS_PULL_REQUEST}'-'${TRAVIS_COMMIT}'-armv7.apk - wget -o /tmp/qfield-${TRAVIS_TAG}-x86.apk https://opengis.ch/download/qfield/ci-builds/qfield-dev-'${TRAVIS_PULL_REQUEST}'-'${TRAVIS_COMMIT}'-x86.apk - ./basic_upload_apks_service_account.py ch.opengis.qfield /tmp/qfield-${TRAVIS_TAG}-armv7.apk - ./basic_upload_apks_service_account.py ch.opengis.qfield /tmp/qfield-${TRAVIS_TAG}-x86.apk - ./release-upload.py --release=${TRAVIS_TAG} --oauth-token=${GITHUB_API_TOKEN} /tmp/qfield-${TRAVIS_TAG}-armv7.apk /tmp/qfield-${TRAVIS_TAG}-x86.apk + echo -e "\e[31m * Collecting apks to upload...\e[0m" + wget -O /tmp/qfield-${TRAVIS_TAG}-armv7.apk https://opengis.ch/download/qfield/ci-builds/qfield-dev-${UPLOAD_ARTIFACT_ID}-${TRAVIS_COMMIT}-armv7.apk + wget -O /tmp/qfield-${TRAVIS_TAG}-x86.apk https://opengis.ch/download/qfield/ci-builds/qfield-dev-${UPLOAD_ARTIFACT_ID}-${TRAVIS_COMMIT}-x86.apk + + echo -e "\e[31m * Deploying app to google play...\e[0m" + ./scripts/basic_upload_apks_service_account.py ch.opengis.qfield /tmp/qfield-${TRAVIS_TAG}-armv7.apk + ./scripts/basic_upload_apks_service_account.py ch.opengis.qfield /tmp/qfield-${TRAVIS_TAG}-x86.apk + + echo -e "\e[31m * Deploying app to github release...\e[0m" + ./scripts/release-upload.py --release=${TRAVIS_TAG} --oauth-token=${GITHUB_API_TOKEN} /tmp/qfield-${TRAVIS_TAG}-armv7.apk /tmp/qfield-${TRAVIS_TAG}-x86.apk fi fi diff --git a/scripts/travis_env.sh b/scripts/travis_env.sh new file mode 100644 index 0000000000..52f374c892 --- /dev/null +++ b/scripts/travis_env.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +export UPLOAD_ARTIFACT_ID=$( [[ ${TRAVIS_PULL_REQUEST} =~ false ]] && echo ${TRAVIS_TAG} || echo ${TRAVIS_PULL_REQUEST} ) diff --git a/scripts/upload-artifacts.sh b/scripts/upload-artifacts.sh index 783d8f7321..0f6b360c0f 100755 --- a/scripts/upload-artifacts.sh +++ b/scripts/upload-artifacts.sh @@ -1,16 +1,17 @@ #!/bin/bash - set -e +source scripts/travis_env.sh + # If we have secure env vars and are in either a pull request or a tag, we need to upload artifacts if [[ "${TRAVIS_SECURE_ENV_VARS}" = "true" && ( "${TRAVIS_PULL_REQUEST}" != "false" || "${TRAVIS_TAG}" ) ]]; then echo -e "\e[31mAbout to upload build artifacts\e[0m" - sudo mv build-${ARCH}/out/build/outputs/apk/out-release-signed.apk /tmp/qfield-dev-${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}-${ARCH}.apk + sudo mv build-${ARCH}/out/build/outputs/apk/out-release-signed.apk /tmp/qfield-dev-${UPLOAD_ARTIFACT_ID}-${TRAVIS_COMMIT}-${ARCH}.apk openssl aes-256-cbc -K $encrypted_dev_upload_key -iv $encrypted_dev_upload_iv -in .ci/id_rsa.enc -out .ci/id_rsa -d chmod 400 .ci/id_rsa - scp -i .ci/id_rsa -o "StrictHostKeyChecking no" /tmp/qfield-dev-${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}-${ARCH}.apk ${DEV_UPLOAD_USER}@${DEV_UPLOAD_HOST}:~/ci-builds/ - echo -e "\e[31Uploaded to https://opengis.ch/download/qfield/ci-builds/qfield-dev-${TRAVIS_PULL_REQUEST}-${TRAVIS_COMMIT}-${ARCH}.apk \e[0m" + scp -i .ci/id_rsa -o "StrictHostKeyChecking no" /tmp/qfield-dev-${UPLOAD_ARTIFACT_ID}-${TRAVIS_COMMIT}-${ARCH}.apk ${DEV_UPLOAD_USER}@${DEV_UPLOAD_HOST}:~/ci-builds/ + echo -e "\e[31Uploaded to https://opengis.ch/download/qfield/ci-builds/qfield-dev-${UPLOAD_ARTIFACT_ID}-${TRAVIS_COMMIT}-${ARCH}.apk \e[0m" else echo -e "Not uploading artifacts ..." if [ "${TRAVIS_SECURE_ENV_VARS}" != "true" ]; diff --git a/scripts/upload_apk.py b/scripts/upload_apk.py deleted file mode 100755 index 55f9f7c167..0000000000 --- a/scripts/upload_apk.py +++ /dev/null @@ -1,93 +0,0 @@ -#!/usr/bin/env python -# -# Copyright 2014 Marta Rodriguez. -# -# Licensed under the Apache License, Version 2.0 (the 'License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Uploads an apk to the alpha track.""" - -import sys -# required on OSX to prevent the default 'six' library from interfering with the needed newer package, -# as of the 1.4.1 google-api library -sys.path.insert(1, '/Library/Python/2.7/site-packages') - -import argparse -import httplib2 -import json - -from oauth2client.service_account import ServiceAccountCredentials -from oauth2client.client import AccessTokenRefreshError -from apiclient.discovery import build - - -def upload(package, service, apk, track): - - # Create an httplib2.Http object to handle our HTTP requests and authorize it - # with the Credentials. Note that the first parameter, service_account_name, - # is the Email address created for the Service account. It must be the email - # address associated with the key that was created. - credentials = ServiceAccountCredentials.from_json_keyfile_name( - service, ['https://www.googleapis.com/auth/androidpublisher']) - - http_auth = credentials.authorize(http=httplib2.Http()) - service = build('androidpublisher', 'v2', http=http_auth) - - try: - edit_request = service.edits().insert(body={}, packageName=package) - result = edit_request.execute() - edit_id = result['id'] - - apk_response = service.edits().apks().upload( - editId=edit_id, - packageName=package, - media_body=apk).execute() - - print('Version code {} has been uploaded'.format(apk_response['versionCode'])) - - track_response = service.edits().tracks().update( - editId=edit_id, - track=track, - packageName=package, - body={u'versionCodes': [apk_response['versionCode']]}).execute() - - print('Track {} is set for version code(s) {}'.format( - track_response['track'], track_response['versionCodes'])) - - commit_request = service.edits().commit( - editId=edit_id, packageName=package).execute() - - print('Edit "{}" has been committed'.format(commit_request['id'])) - - except AccessTokenRefreshError as e: - print('The credentials have been revoked or expired, please re-run the ' - 'application to re-authorize') - raise e - - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument('-p', '--package', required=True, - help='The package name. Example: com.android.sample') - parser.add_argument('-s', '--service', required=True, - help='The service account json file.') - parser.add_argument('-a', '--apk', required=True, - help='The path to the APK file to upload.') - parser.add_argument( - '-t', '--track', choices=['alpha', 'beta', 'production', 'rollout'], default='alpha') - args = parser.parse_args() - - upload(args.package, args.service, args.apk, args.track) - - -if __name__ == "__main__": - main()