Automate Your App Management Using the Galaxy Store Developer API, Jenkins, and Python
Samiul Hossain
Engineer, Samsung Developer Program
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."
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."
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.
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>
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.
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.
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)
- To define the certificate location:
-
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.