In this post I’ll guide you on how to directly upload the compiled .apk from travis to your GitHub repository.
Why do we need this?
Well, assume that you need to provide an app to your testers after each commit on the repository, so instead of manually copying and emailing them the app, we can setup travis to upload the file to our repository where the testers can fetch it from.
So, lets get to it!
Step 1 :
Link Travis to your GitHub Account.
Open up https://travis-ci.org.
Click on the green button in the top right corner that says “Sign in with GitHub”
Step 2 :
Add your existing repository to Travis
Click the “+” button next to your Travis Dashboard located on the left.
Choose the project that you want to setup Travis from the next page
Click the cog here and add an Environment Variable named GITHUB_API_KEY.
Proceed by adding your Personal Authentication Token there.
Read up here on how to get the Token.
Great, we are pretty much done here.
Let us move to the project repository that we just integrated and create a new file in the root of repository by clicking on the “Create new file” on the repo’s page.
Name it .travis.yml and add the following commands over there
language: android jdk: - oraclejdk8 android: components: - tools - build-tools-24.0.0 - android-24 - extra-android-support - extra-google-google_play_services - extra-android-m2repository - extra-google-m2repository - addon-google_apis-google-24 before_install: - chmod +x gradlew - export JAVA8_HOME=/usr/lib/jvm/java-8-oracle - export JAVA_HOME=$JAVA8_HOME after_success: - chmod +x ./upload-gh-pages.sh - ./upload-apk.sh script: - ./gradlew build
Next, create a bash file in the root of your repository using the same method and name it upload-apk.sh
#create a new directory that will contain out generated apk
mkdir $HOME/buildApk/ #copy generated apk from build folder to the folder just created
cp -R app/build/outputs/apk/app-debug.apk $HOME/android/ #go to home and setup git
cd $HOME git config --global user.email "useremail@domain.com" git config --global user.name "Your Name" #clone the repository in the buildApk folder
git clone --quiet --branch=master https://user-name:$GITHUB_API_KEY@github.com/user-name/repo-name master > /dev/null #go into directory and copy data we're interested
cd master cp -Rf $HOME/android/* . #add, commit and push files
git add -f . git remote rm origin git remote add origin https://user-name:$GITHUB_API_KEY@github.com/user-name/repo-name.git git add -f . git commit -m "Travis build $TRAVIS_BUILD_NUMBER pushed" git push -fq origin master > /dev/null echo -e "Donen"
Once you have done this, commit and push these files, a Travis build will be initiated in few seconds.
You can see it ongoing in your Dashboard at https://travis-ci.org/.
After the build has completed, you will can see an app-debug.apk in your Repository.
IMPORTANT NOTE :
You might be wondering as to why did I write [skip ci] in the commit message.
Well the reason for that is, Travis starts a new build as soon as it detects a commit made on the master branch of your repository.
So once the apk is uploaded, that will trigger another build in Travis and hence forming an infinite loop.
We can prevent this in 2 ways :
First, simply write [skip ci] somewhere in the commit message and it will cause Travis to ignore the commit.
Or, push the apk to any other branch which is not configured for Travis build.
So well, that’s almost it.
I hope that you found this tutorial helpful, and if you have any doubts regarding this feel free to comment down below, I would love to help you out.
Cheers.