Automate Your App Management Using the Galaxy Store Developer API, Jenkins, and Python
application distribution in galaxy store is usually managed through the galaxy store seller portal. however, when you have many applications, updating and distributing them manually through the seller portal interface can be a hassle. the content publish api, a core part of the galaxy store developer (gsd) api, enables you to handle application distribution and management programmatically without using the seller portal interface. the previous article in the series, app management using the galaxy store developer api and python, showed you how to view, modify, submit and change the status of your registered applications in the seller portal with the api. this article builds on the previously presented information and shows you how to create an automated release management system using jenkins and python to access the content publish api. the task is accomplished in three separate steps: configure jenkins on your computer and connect it to your github server. create a jenkins project to generate a release apk file whenever there is a new merge or commit to your application project. create a second jenkins project to run a python script that programmatically uploads and submits the apk file to galaxy store for review. finally, this article discusses how to resolve common issues encountered when using the galaxy store developer api features through python scripting. prerequisites before following the steps in this tutorial: install python on your computer. install jenkins. follow the instructions in the jenkins documentation for your operating system and make sure that the recommended plugins are installed and up-to-date. create a galaxy store seller account in the galaxy store seller portal. for more information, see the galaxy store seller portal user guide. obtain a service account id from the seller portal. retrieve an access token from the galaxy store authentication server. for more information, see how to create an access token for the galaxy store developer api using python. configure jenkins to configure jenkins to work with your github server, follow the steps below: in the jenkins dashboard, select "manage jenkins > configure system." in the "jenkins location" section, define the jenkins url. it is recommended to define a specific hostname instead of the default value "localhost." define the environment variables. select the "environment variables" checkbox and add the following variable names and values: android_home: path to your android sdk installation folder java_home: path to your java development kit (jdk) installation folder gradle_user_home: path to a folder where jenkins can store the gradle cache. the cache can help avoid some apk build errors. noteyou must define the environment variables using these specific names. figure 1: environment variables create a personal github access token. this step is especially important if you are a github enterprise user or if you want additional security for your project. in github, go to "settings > developer settings > personal access tokens," and select "generate new token." define the scope of your token, making sure you include the appropriate hook-related admin scopes. copy the token to a safe place. figure 2: github access token return to the jenkins settings and select "add github server." define the api url field with the appropriate value: if you use github enterprise, enter "https://your-enterprise-url.com/api/v3" otherwise, enter "https://api.github.com" in the "credentials" section, click "add." in the pop-up, define the credentials kind as "secret text" and paste your github access token in the "secret" field. to save the credentials and close the pop-up, click "add." figure 3: add credentials pop-up in the "credentials" dropdown menu, select the credential that you just created. figure 4: configured github server to test that the connection is correctly configured, click "test connection." since webhooks are used to trigger building the application apk file, select the "manage hooks" checkbox. generating a signed apk the first jenkins project for this task generates a signed apk whenever there is a merge or commit to your application project in github. on the jenkins dashboard, click "new item." select "freestyle project," define a name for the project, and select "ok." the "configure" page opens. all the details required to build the project are defined on the "configure" page, such as the source code management: go to the "source code management" section of the "configure" page and select "git." select "add repository" and enter your repository details: repository url: the https address to your repository. make sure to include the ".git" extension at the end of the url. credentials: select your credentials from the list, if it is not selected already. if you are working on a public github project, you can omit this step. branches to build: specify the release branch to be built. if you do not specify a release branch, the main branch is built. note: jenkins clones the repository into its workspace. confirm the configuration by clicking "apply" and "save." figure 5: git configuration in jenkins project to test building the project, click "build now." you can review the build result details in the "build history" tab. the "build triggers" section is vital as it defines when the jenkins project is to be built. to detect when there is a new commit or release in the specified release branch, you can use a github webhook. in the "build triggers" section, select the "github hook trigger for gitscm polling" checkbox. in your github repository, go to "settings > hooks > webhooks" and create a webhook: click "add webhook." in the "payload url" field, enter your jenkins address, adding "/github-webhook/" to the end of the address. the trailing "/" is mandatory. for "content type," select "application/json." for additional security, you can define a "secret." this step is optional. in the "which events would you like to trigger this webhook?" field, select the option you want. if you are behind a firewall, such as on a corporate or nat network, you must expose your jenkins url to github. to bypass the firewall, you can use smee or nginx. now you are ready to implement the functionality for the project, which is defined through build steps in jenkins. the first step builds your android studio project: in the "build steps" section, from the "add build step" dropdown menu, select "invoke gradle script." select "use gradle wrapper" and select the "make gradlew executable" checkbox. in the "wrapper location" field, enter the jenkins workspace path to your application project. to retrieve the location of your jenkins workspace directory, go to "dashboard > manage jenkins > configure system." in the "tasks" field, enter "clean build --stacktrace." figure 6: gradle build script the next step signs the apk file. to understand which information is needed for this step, you can look within android studio. the following figure shows the "generate signed bundle or apk" dialog in android studio. figure 7: generate signed bundle or apk in android studio all the fields in this dialog are mandatory. if you do not have an existing keystore, you must create one. to sign the apk file in jenkins, add another build step, selecting "execute windows batch command." in the text box, enter the following command: jarsigner -keystore <pathtokeystore> -storepass <storepassword> -keypass <keypassword> -signedjar <pathtosavesignedapk> <pathtounsignedapk> <keyalias> figure 8: windows batch command to sign apk to archive the generated apk, in the "post-build actions" section, from the "add post-build action" dropdown menu, select "archive the artifacts." in the "files to archive" field, enter the path to the signed apk. your jenkins project is ready to automatically generate a signed apk when you make changes to your application project in github. upload to galaxy store the second jenkins project triggers a python script that uploads and submits the signed apk to galaxy store. in jenkins, create a new freestyle project. in the "build triggers" section, select "build after other projects are built," and in the "projects to watch" field, enter the name of the previous project. since this project must build only when the previous project is built successfully and is stable, select the "trigger only if build is stable" option. figure 9: build triggers setup define the functionality for the jenkins project. in the "build steps" section, from the "add build step" dropdown menu, select "execute windows batch command." in the text box, write a simple command that navigates to your python script and runs it. figure 10: run python script through windows batch command finally, create the python script to handle the upload. for detailed information on writing the script, see app management using the galaxy store developer api and python. common issues with the python script the following tips can help you troubleshoot common issues with implementing the python script: http request "ssl: certificate_verify_failed" error if you receive this error, you must explicitly specify the "verify" parameter in your http request. to define the certificate location: requests.get(api_url, headers=headers,data=payload, verify="/path/to/certificate") to skip verification (not recommended): requests.get(api_url, headers=headers,data=payload, verify=false) file upload "content-type mismatch" error if you receive this error, check the headers in your http request. replace 'accept': 'application/json' with 'content-type': 'application/json' application submission "check validation (essential input)" error if you receive this error, mandatory parameters in your http request payload are missing. some commonly missed parameters include: publicationtype: for the available values, see the content publish api reference. usexportlaws: certifies that your application complies with u.s. export laws. to validate and distribute your application, the value must be "true." summary you can easily automate your application management in galaxy store using jenkins to build and sign apk files from your github project and upload them through the galaxy store developer api with a python script. if you have questions about or need help with the information in this tutorial, you can share your queries on the samsung developers forum. for more specialized support, you can contact us through samsung developer support. blog articles in this series how to create an access token for the galaxy store developer api using python: an access token is sent in the authorization header of every galaxy store developer api call. want statistics for your apps using the galaxy store developer api?: how to implement the galaxy store statistics apis using python to get statistical data about your applications. managing in-app items using the galaxy store api and python: view, create, modify, and remove in-app items using the iap publish api and python. app management using the galaxy store developer api and python: upload and submit an update to your application using the galaxy store developer api and python.