1 Reply Latest reply on Jan 23, 2017 7:36 AM by danielem18727002

    Automate PhoneGap Build help

    danielem18727002 Level 1

      Hi,

       

      I am new at the PhoneGap API and I am trying to automate the build process of the App I am working on.

       

      I have a local zip file which I would like upload to phonegap build, make a build and download it on my laptop! I have got something in place but at the moment I cannot managed to:

      - download the apk/ios file into my laptop

      - listen for when the build is ready to download

       

      I am using Gulp tasks for the automation and this is what I have got so far:

      var pgBuild = require('phonegap-build-api');

       

      gulp.task('build-debug', function () {

      //ask for authentication

        pgBuild.auth({ username: 'myemail@gmail.com', password: 'mypassword' }, function (e, api) {

        api.get(phonegapEndpoint, function(e, data) {

          var options = {

            form: {

             data: {

             platforms: [ 'ios', 'android' ],
             download: './BuildOutputs/',
             debug: true
             },
             file: './www/app.zip'
           }

         };

      //make the build

      api.post('/apps/myappid/build', options, function(e, data) {
        if(data != null) {

       

         if(data.status.android === "complete") {

           //here I should have my build saved in the path mentioned on form.data.download' which is in  'BuildOutputs' folder

       

        }

        }

      });

      }

       

      This is what the 'data' attribute looks like after the build:

       

      build_data = {

        id: myappid,
        title: 'MyApp',
        package: 'xx.xxxxxxx.xx',
        version: '3.0.13',
        build_count: 297,
        private: true,
        phonegap_version: 'cli-6.4.0',
        hydrates: false,
        share: false,
        last_build: '2017-01-19 07:51:48 -0800',
        description: 'Electronic based rota and timesheet.',
        repo: null,
        tag: null,
        debug: false,
        head: null,
        link: '/api/v1/apps/myappid',
        plugins: '/api/v1/apps/myappid/plugins',
        completed: false,
        role: 'admin',
        icon: {

         filename: null,
         link: null
        },
        status: {

         android: 'complete',
         ios: 'pending',
         winphone: 'error'
        },
        phonegap_versions: {

         android: '6.1.0',
         ios: '4.3.0',
         winphone: '4.4.3'
        },
        download: {

         android: '/api/v1/apps/myappid/android'
        },
        error: {

         winphone: '"["/home/Administrator/gimlet/var/winphone8/4.4.3/bin/create.bat ./project xxxxxx"]": No such file or directory - /home/Administrator/gimlet/var/winphone8/4.4.3/bin/create.bat'
        },
        install_url: 'https://build.phonegap.com/apps/myappid/install/sxGWtXktrxvm92yesPt4',
        share_url: 'https://build.phonegap.com/apps/myappid/share',
        collaborators: {

         link: '/api/v1/apps/myappid/collaborators',
         active: [

        [

        Object

        ],
         [

        Object

        ]

        ],
         pending: []

        },
        keys: {

         android: {

         id: myandroidid,
         title: 'Android',
         platform: 'android',
         link: '/api/v1/keys/android/myandroidid',
         default: false
         },
         ios: {

         id: myiosid,
         title: 'iOS Release Distr',
         platform: 'ios',
         link: '/api/v1/keys/ios/myiosid',
         default: false
         },
         winphone: null
        },
        logs: {

         android: '/api/v1/apps/myappid/logs/android/build',
         ios: '/api/v1/apps/myappid/logs/ios/build',
         winphone: '/api/v1/apps/myappid/logs/winphone/build'
        }

      };

       

      Now if I look at the download attribute it says: 

      download: {

         android: '/api/v1/apps/myappid/android'
        }

       

      I would have expect the APK to be saved where I wanted to but it's not. Does anyone know how to do that?

      Another question would be about 'listen' for the pending builds to be completed, well in the API Documentation I have found this:

      "Once the builds are queued, you will want to watch the results of GET /api/v1/apps/:id to check when each platform's status changes from pending to either complete or error." but I am not able to do it

       

      Can anyone help please?

      Thanks

        • 1. Re: Automate PhoneGap Build help
          danielem18727002 Level 1

          I'll reply to myself since I have managed to move forward...so my first question was:

          1 - I would have expect the APK to be saved where I wanted to but it's not. Does anyone know how to do that?

          I have accomplished this by using this simple command:

          api.get('/apps/myappid/android').pipe(fs.createWriteStream('ppmobilev3.apk')); //if we don't specify a location, it saves the file in your current location

           

          2 - 'listen' for the pending builds to be completed

          to do that I have implemented my own waiting method by using a simple setInterval as shown below:

           

          api.post(phonegapEndpoint + '/build', options, function(ee, data) {

            if(data != null && !ee){

            var buildStatus = data.completed;
            if(buildStatus) {

            buildCompletedAndReadyToDownload(api);
            return;
            }

            waitingForPendingBuild(api);
            }

          });

           

          and then I have the following function for keep checking the build status until it is completed or gives back an error:

           

          function waitingForPendingBuild(api){

            var intervalForCheckingBuildStatus = setInterval(function(){

            api.get(phonegapEndpoint, function(e, data){

            var buildStatus = data.completed;
            if(buildStatus) {

            console.log('BUILT SUCCESSFULLY');
            clearInterval(intervalForCheckingBuildStatus);
            buildCompletedAndReadyToDownload(api);
            }

           

            if(data.status.android === 'Error' || data.status.ios === 'Error'){

            clearInterval(intervalForCheckingBuildStatus);
            console.log('There have been an error on the build process \n');
            console.log('iOS Error: ' + error.ios + ' \n');
            console.log('Android Error: ' + error.android + ' \n');
            }

            });
            }, 3000);
          }

           

          Hope this would be useful to someone else

           

          Thanks