Microsoft Azure pipeline setup to build android APK and push it to Appcenter

48 Views Asked by At

I have been working on this and my current Yaml pipeline script looks like this:

 variables:
- group: configurationAppCenterAppSlug
- group: configurationPipeline


parameters:
  - name: schemeName
    displayName: 'Choose environment:'
    type: string
    default: stageCasa
    values:
      - stageCasa
      - stageCz
      - stagePsk
      - stageSk
      - stageRo

trigger:
- casa-redesign

pool:
  vmImage: 'macos-latest'


steps:

- checkout: self
  displayName: 'Update repo files'
  fetchDepth: 1
  fetchTags: false
  lfs: false
  clean: false

- script: |
    echo "Checking for Java 11"
    if /usr/libexec/java_home -v 11 >/dev/null 2>&1; then
      echo "Java 11 found"
      JAVA_HOME=$(/usr/libexec/java_home -v 11)
      echo "Setting JAVA_HOME to $JAVA_HOME"
    else
      echo "Java 11 not found, installing via Homebrew"
      brew install openjdk@11
      brew link --force --overwrite openjdk@11
      JAVA_HOME=$(/usr/libexec/java_home -v 11)
      echo "Setting JAVA_HOME to $JAVA_HOME"
    fi
    echo "##vso[task.setvariable variable=JAVA_HOME]$JAVA_HOME"
  displayName: 'Check for Java 11 and install if not present'

- script: |
    echo "JAVA_HOME is set to: $JAVA_HOME"
    java -version
  displayName: 'Verify Java Installation'

- task: Gradle@2
  inputs:
    workingDirectory: ''
    gradleWrapperFile: 'gradlew'
    gradleOptions: '-Xmx3072m'
    publishJUnitResults: false
    testResultsFiles: '**/TEST-*.xml'
    tasks: "bundle${{ parameters.schemeName }}Release"
  env:
    JAVA_HOME: $(JAVA_HOME)


    
- script: |
    if [ -f "$(System.DefaultWorkingDirectory)/app/build/outputs/bundle/stageCasaRelease/app-stage-casa-release.aab" ]; then
      cp "$(System.DefaultWorkingDirectory)/app/build/outputs/bundle/stageCasaRelease/app-stage-casa-release.aab" "$(Build.ArtifactStagingDirectory)/app-stage-casa-release.aab"
      echo "AAB file copied successfully."
    else
      echo "AAB file does not exist."
    fi
  displayName: 'Copy AAB file to Artifact Staging Directory'
  

- task: AppCenterDistribute@3
  inputs:
    serverEndpoint: 'AppCenter Service'
    # appSlug: 'FEG/Vegas-CASA-Stage-1'
    appSlug: '$(${{ parameters.schemeName }}_appSlug)'
    appFile: '$(Build.ArtifactStagingDirectory)/app-stage-casa-release.aab'
    releaseNotesOption: 'input'
    releaseNotesInput: '- empty'

What bothers me is that when I run this pipeline, it sometimes works. It builds aab file and pushes it to the Appcenter. Other times it fails with errors like this:

    AppCenterDistribute

View raw log

Starting: AppCenterDistribute
==============================================================================
Task         : App Center distribute
Description  : Distribute app builds to testers and users via Visual Studio App Center
Version      : 3.232.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/deploy/app-center-distribute
==============================================================================
##[error]Error: Cannot find any file based on /Users/runner/work/1/a/app-stage-casa-release.aab.
Finishing: AppCenterDistribute

It confuses me because sometimes it works and sometimes not. Where am I going wrong here are there any ideas. I feel like I'm very close to the solution, it's like 1 piece of puzzle is missing... Thanks

1

There are 1 best solutions below

0
SiddheshDesai On

Thank you @Ziyang Liu-MSFT for your comments.

You can use the Azure Yaml pipeline below which utilizes if else statements to check if the AAB files exist or not:-

variables:
- group: configurationAppCenterAppSlug
- group: configurationPipeline

parameters:
  - name: schemeName
    displayName: 'Choose environment:'
    type: string
    default: stageCasa
    values:
      - stageCasa
      - stageCz
      - stagePsk
      - stageSk
      - stageRo

trigger:
- casa-redesign

pool:
  vmImage: 'macos-latest'

steps:
- checkout: self
  displayName: 'Update repo files'
  fetchDepth: 1
  fetchTags: false
  lfs: false
  clean: false

- script: |
    echo "Checking for Java 11"
    if /usr/libexec/java_home -v 11 >/dev/null 2>&1; then
      echo "Java 11 found"
      JAVA_HOME=$(/usr/libexec/java_home -v 11)
      echo "Setting JAVA_HOME to $JAVA_HOME"
    else
      echo "Java 11 not found, installing via Homebrew"
      brew install openjdk@11
      brew link --force --overwrite openjdk@11
      JAVA_HOME=$(/usr/libexec/java_home -v 11)
      echo "Setting JAVA_HOME to $JAVA_HOME"
    fi
    echo "##vso[task.setvariable variable=JAVA_HOME]$JAVA_HOME"
  displayName: 'Check for Java 11 and install if not present'

- script: |
    echo "JAVA_HOME is set to: $JAVA_HOME"
    java -version
  displayName: 'Verify Java Installation'

- task: Gradle@2
  inputs:
    workingDirectory: ''
    gradleWrapperFile: 'gradlew'
    gradleOptions: '-Xmx3072m'
    publishJUnitResults: false
    testResultsFiles: '**/TEST-*.xml'
    tasks: "bundle${{ parameters.schemeName }}Release"
  env:
    JAVA_HOME: $(JAVA_HOME)

- script: |
    if [ -f "$(Build.SourcesDirectory)/app/build/outputs/bundle/${{ parameters.schemeName }}Release/app-${{ parameters.schemeName }}-release.aab" ]; then
      cp "$(Build.SourcesDirectory)/app/build/outputs/bundle/${{ parameters.schemeName }}Release/app-${{ parameters.schemeName }}-release.aab" "$(Build.ArtifactStagingDirectory)/app-${{ parameters.schemeName }}-release.aab"
      echo "AAB file copied successfully."
    else
      echo "AAB file does not exist."
      exit 1
    fi
  displayName: 'Copy AAB file to Artifact Staging Directory'

- task: AppCenterDistribute@3
  inputs:
    serverEndpoint: 'AppCenter Service'
    appSlug: '$(appSlug)'  # Make sure the correct variable name is used here
    appFile: '$(Build.ArtifactStagingDirectory)/app-${{ parameters.schemeName }}-release.aab'
    releaseNotesOption: 'input'
    releaseNotesInput: '- empty'