[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

This post is an introduction to Azure release pipeline using AGC publish API, In this walkthrough I’ll lay out the general steps on how to setup your Azure DevOps pipeline in such a way that a single click will trigger a new AppGallery app release.

summary

We will create a pipeline that executes the gradle task to generate an apk and sign it. We create a connection to the repository containing our android project. We will then integrate AGC publish API using python to deploy the APK to AppGallery.

Requirements

I will assume that you already have a working Android app released on AppGallery and have the following:

AppGallery account

Microsoft Azure account

Android project hosted in a remote repo (azure repo, github, bitbucket...)

Setting up CI

Configure Gradle to sign generated APK:

Copy the signature file generated in Generating a Signing Certificate Fingerprint to the app root directory and configure the signature in the build.gradle file in the project directory

signingConfigs {
release {
storeFile file('xxx.xxx')
keyAlias 'xxxx'
keyPassword 'xxxx'
storePassword 'xxxx'
}
}
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
signingConfig signingConfigs.release
}
}

Create new release pipeline:

Go to azure release pipelines page, then click on New->New release pipeline

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825055224.31301300240274365986070391493468:50510918025852:2800:C250F17A772AFBDAECC7A02A62B074EDA83F0CD7C10AB02D0AD1CD3D48FBFAAE.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825055224.31301300240274365986070391493468:50510918025852:2800:C250F17A772AFBDAECC7A02A62B074EDA83F0CD7C10AB02D0AD1CD3D48FBFAAE.png[/IMG]

You will see pipeline template selection page:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052704.40556183578915704606644912063916:50510918025852:2800:9CF153EF2E49095AE23CB738B697B171755D3D52E960F382430F52CB32903B00.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052704.40556183578915704606644912063916:50510918025852:2800:9CF153EF2E49095AE23CB738B697B171755D3D52E960F382430F52CB32903B00.png[/IMG]

Here, we will not pick and existing template, we will click on Empty Job:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052714.31963010610733613213335920497768:50510918025852:2800:3ED09A9533D28D1D8B9BD47A76F6AE1B6176C51A63892D695708B8768CE7CDBE.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052714.31963010610733613213335920497768:50510918025852:2800:3ED09A9533D28D1D8B9BD47A76F6AE1B6176C51A63892D695708B8768CE7CDBE.png[/IMG]

Our pipeline is now ready to be developed

Add artifact (link repo)

Our project is hosted on Github, you can host your project on any repository management service, we recommend Azure repos, github or bitbucket.

To link the repo, we have to click on the add button in next to artifacts as shown here:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052721.20157046949704996325619630974981:50510918025852:2800:9E3C141AD32B078FF0AA42CE9A2C451E74CE3C94B54F8756B06FFA04E7D0F3A1.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052721.20157046949704996325619630974981:50510918025852:2800:9E3C141AD32B078FF0AA42CE9A2C451E74CE3C94B54F8756B06FFA04E7D0F3A1.png[/IMG]

We will create a connection to github, click on new button, name the connection then click on Authorize using OAuth, new Github window will pop up asking to give azure permission.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052737.97040869858564203913431046124761:50510918025852:2800:0105EA266611F8F6AD158DCD2BAF079472281575EF81B91B411AFA35EC4A3461.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052737.97040869858564203913431046124761:50510918025852:2800:0105EA266611F8F6AD158DCD2BAF079472281575EF81B91B411AFA35EC4A3461.png[/IMG]

to select the repo. We click on the three dots button to browser our repos

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052740.36130939994028923811938750048505:50510918025852:2800:A5EB261772FCA1A07D01806F6445795DE97C9F0090B9E9AB1357E4B6F96AEF37.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052740.36130939994028923811938750048505:50510918025852:2800:A5EB261772FCA1A07D01806F6445795DE97C9F0090B9E9AB1357E4B6F96AEF37.png[/IMG]

Now we pick our android project repo

We then chose branch, version and set an alias name for the artifact as shown in the next screenshot:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052800.80948261124269787836466522969568:50510918025852:2800:093039636C6786232B43C4FB5499FB2CA333AA5F9090DD3709B34EE6F1DCCC1E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052800.80948261124269787836466522969568:50510918025852:2800:093039636C6786232B43C4FB5499FB2CA333AA5F9090DD3709B34EE6F1DCCC1E.png[/IMG]

Click Add button then we add tasks

Add tasks

To add add a task you need to click on the plus icon on the agent as follow:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052805.12174466047380370745362603274946:50510918025852:2800:07B4A0E059F07628B9FDD9F0A7EC1BDC9E6A7C6F2394FB3518692BDA65112E56.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052805.12174466047380370745362603274946:50510918025852:2800:07B4A0E059F07628B9FDD9F0A7EC1BDC9E6A7C6F2394FB3518692BDA65112E56.png[/IMG]

Task 1: Use python version:

This task is to select python version to run our scripts

Add new task, on the sidebar search for “use python version”, hover over the first task and click on add, leave the default information, it should be version 3.x

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052809.70526995180715583663875259680053:50510918025852:2800:2BF39CF59DB2A244A539F29A3106F4B30EC726811E0E485FBB119827E534166A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052809.70526995180715583663875259680053:50510918025852:2800:2BF39CF59DB2A244A539F29A3106F4B30EC726811E0E485FBB119827E534166A.png[/IMG]

Task 2: Gradle assembleRelease

We add this task to run gradle script to make a build (generate apk)

Click on add task icon, then search and add Gradle task:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052815.50795754332647071680079165965970:50510918025852:2800:99594222BFAEBE5776B8C98744FED8E6AFBF950B9CE135D4B439F0C23F80CB4A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052815.50795754332647071680079165965970:50510918025852:2800:99594222BFAEBE5776B8C98744FED8E6AFBF950B9CE135D4B439F0C23F80CB4A.png[/IMG]

We select the gradle wrapper by clicking on the three dots icon:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052821.40259349473071163822917243794583:50510918025852:2800:E0A2C9121CD71ADEB50974A929F5C6476FB24D88B15F9E9E0C095CF0B882E8F0.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052821.40259349473071163822917243794583:50510918025852:2800:E0A2C9121CD71ADEB50974A929F5C6476FB24D88B15F9E9E0C095CF0B882E8F0.png[/IMG]

Now select the gradlew.bat from the dialog that pops up

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052827.49130237702079998205341098931268:50510918025852:2800:3CBF2C2699B9A87267923290CED9965AD4B9EF7CE21690594A90FF2337F5B40C.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825052827.49130237702079998205341098931268:50510918025852:2800:3CBF2C2699B9A87267923290CED9965AD4B9EF7CE21690594A90FF2337F5B40C.png[/IMG]

now let’s select the working directory by clicking on three dots icon next to working directory input and select the root folder of the project and click OK

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825053001.36149902700098517340063848722285:50510918025852:2800:F2D39711D6C9E95B3EA04F508953ABA4DC8CC6405DC06CA7FD64E2309E6551EA.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825053001.36149902700098517340063848722285:50510918025852:2800:F2D39711D6C9E95B3EA04F508953ABA4DC8CC6405DC06CA7FD64E2309E6551EA.png[/IMG]

See the scripts, we will create a release apk therefore write, assembleRelease in Gradle tasks. You can as well write assembleDebug if you want to generate a debug apk, also we choose not to publish Junit test results (under gradle tasks)

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825053445.71070262551207111340940303129361:50510918025852:2800:6F8760C4EB2369CD9C700239371522A6DFF4474FD2A78B12DD80523D12F458E3.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825053445.71070262551207111340940303129361:50510918025852:2800:6F8760C4EB2369CD9C700239371522A6DFF4474FD2A78B12DD80523D12F458E3.png[/IMG]

Task 3: copy generate APK

We add a task to copy the generate apk into an azure special directory to prepare to upload it to AppGallery, click on add tasks button and search for “Copy Files”

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825054001.06368683275750392317009832033633:50510918025852:2800:83C1E335397D05DAC0D070D9B1142A131DE70C0FCBA5A923C76245854ACF6D47.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825054001.06368683275750392317009832033633:50510918025852:2800:83C1E335397D05DAC0D070D9B1142A131DE70C0FCBA5A923C76245854ACF6D47.png[/IMG]

Set the Contents input to **/*.apk and the Target Folder input to: $(build.artifactStagingDirectory) as shown:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825054009.04991500411843256449512979695467:50510918025852:2800:B8D4880B58D1492C7934567DC06397A9C966F1A28EC722BDFD185BB77B61F846.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825054009.04991500411843256449512979695467:50510918025852:2800:B8D4880B58D1492C7934567DC06397A9C966F1A28EC722BDFD185BB77B61F846.png[/IMG]

Task 4: pip install request

We install request module to use it in out python scripts

Click on the add task button and search for “Command line” and add it

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825054015.11853211917381847492363644397955:50510918025852:2800:EA813420F2D7B44AA6AF7412A610BDC1E8FEFE17B132C9CB274CC9E1F8C75569.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825054015.11853211917381847492363644397955:50510918025852:2800:EA813420F2D7B44AA6AF7412A610BDC1E8FEFE17B132C9CB274CC9E1F8C75569.png[/IMG]

Give the task and meaningful name, and add the following script:

cd C:\hostedtoolcache\windows\Python\3.8.1\x64\Scripts

pip install requests

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825054021.36478623038946686993472495729242:50510918025852:2800:F95882FE5CC2ED89C5F56295F30C5CD80721CFA4730170179D0A088039995385.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/032/641/0260971000000032641.20200825054021.36478623038946686993472495729242:50510918025852:2800:F95882FE5CC2ED89C5F56295F30C5CD80721CFA4730170179D0A088039995385.png[/IMG]

Note: We will follow AGC publish API documentation to upload the apk to AppGallery which will be done using 5 tasks

you don’t need to change the code in this post as we are using variables, which we will add in a later step

Task 5: Get AppGallery access token

First we need to get access token to use AGC Publish API

Click on add task button and search for “Python script”, give the task a name, select inline, then add the following python script:

import requests

json_data = {"client_id":"$(client_id)","client_secret":"$(client_secret)","grant_type":"client_credentials"}

req = requests.post("https://connect-api.cloud.huawei.com/api/oauth2/v1/token",json=json_data)

res_data = req.json()

res_data = "Bearer " + res_data['access_token']

print("##vso[task.setvariable variable=access_token]"+res_data)

This is not the end. For full content, you can visit https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0202337168449920057&fid=0101187876626530001