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:

  1. Configure Jenkins on your computer and connect it to your GitHub server.
  2. Create a Jenkins project to generate a release APK file whenever there is a new merge or commit to your application project.
  3. 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:

  1. Install Python on your computer.
  2. 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.
  3. Create a Galaxy Store Seller Account in the Galaxy Store Seller Portal. For more information, see the Galaxy Store Seller Portal User Guide.
  4. Obtain a Service Account ID from the Seller Portal.
  5. 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:

  1. In the Jenkins dashboard, select "Manage Jenkins > Configure System."
  2. In the "Jenkins Location" section, define the Jenkins URL. It is recommended to define a specific hostname instead of the default value "localhost."
  3. 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.

    Environment variables

    Figure 1: Environment variables

  4. 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.


    GitHub Access Token

    Figure 2: GitHub Access Token

  5. 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"
  6. 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."


    Jenkins Add Credentials

    Figure 3: Add Credentials Pop-Up

  7. In the "Credentials" dropdown menu, select the credential that you just created.


    Jenkins GitHub Server

    Figure 4: Configured GitHub Server

  8. To test that the connection is correctly configured, click "Test connection."
  9. 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:

  1. Go to the "Source Code Management" section of the "Configure" page and select "Git."
  2. 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.

  3. Confirm the configuration by clicking "Apply" and "Save."

Git Configuration in Jenkins Project

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:

  1. Click "Add webhook."
  2. In the "Payload URL" field, enter your Jenkins address, adding "/github-webhook/" to the end of the address. The trailing "/" is mandatory.
  3. For "Content type," select "application/json."
  4. For additional security, you can define a "Secret." This step is optional.
  5. 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:

  1. In the "Build Steps" section, from the "Add build step" dropdown menu, select "Invoke Gradle script."
  2. Select "Use Gradle Wrapper" and select the "Make gradlew executable" checkbox.
  3. 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."
  4. In the "Tasks" field, enter "clean build --stacktrace."

Gradle Build Script

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.


Generate Signed Bundle or APK 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>

Generate Signed Bundle or APK in Android Studio

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.


Build Triggers setup

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.


Run Python script through Windows batch command

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