<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mehak Fatima</title>
    <description>The latest articles on DEV Community by Mehak Fatima (@mehakfatima).</description>
    <link>https://dev.to/mehakfatima</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F846268%2F0743085e-79d0-41e3-8b03-bf698f0b0481.png</url>
      <title>DEV Community: Mehak Fatima</title>
      <link>https://dev.to/mehakfatima</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mehakfatima"/>
    <language>en</language>
    <item>
      <title>How to Trigger Bitrise Build from Bitbucket Pipeline?</title>
      <dc:creator>Mehak Fatima</dc:creator>
      <pubDate>Wed, 13 Apr 2022 11:52:07 +0000</pubDate>
      <link>https://dev.to/mehakfatima/how-to-trigger-bitrise-build-from-bitbucket-pipeline-336f</link>
      <guid>https://dev.to/mehakfatima/how-to-trigger-bitrise-build-from-bitbucket-pipeline-336f</guid>
      <description>&lt;p&gt;Hi fellow coders!&lt;br&gt;
In my recent project, I felt the need to trigger Bitrise build from Bitbucket Pipeline. I hope the first question that comes in your mind would be what's the reason behind that.&lt;br&gt;
so &lt;em&gt;Let's see...&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Motivation:&lt;/strong&gt;&lt;br&gt;
Let's suppose you have a project "abc" which contains the 3 different sub projects or folder in it.(e.g mobile, web, server)and you want to use same bitbucket repo for these 3 projects. But whenever you push some changes in web or server's folder it triggers the bitrise mobile's build which is not required.&lt;br&gt;
Basically the reason behind that is whenever bitrise feels any change in bitbucket repo it starts automatic(trigger) build.&lt;br&gt;
In bitrise Enable selective builds feature is not currently available for bitbucket repo.&lt;br&gt;
&lt;em&gt;"&lt;strong&gt;So our target is to run mobile build when you find changes in mobile's folder only.&lt;/strong&gt;"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
we are going to use Bitrise's api instead of automatic Bitrise’s triggers and we will hit Bitrise api endpoint through Bitbucket pipeline.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Let's start...&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Trigger Bitrise using Postman (optional):&lt;/strong&gt;&lt;br&gt;
we are using postman here for testing bitrise api&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can collect slug number from Url of bitrise(&lt;a href="https://api.bitrise.io/v0.1/apps/%24Slug_number/builds"&gt;https://api.bitrise.io/v0.1/apps/$Slug_number/builds&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Add Authentication token &lt;a href="https://devcenter.bitrise.io/api/authentication/"&gt;https://devcenter.bitrise.io/api/authentication/&lt;/a&gt; in header of call.&lt;/li&gt;
&lt;li&gt;Add the following object in the body of request.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const data = {
    hook_info: {
      type: 'bitrise',
      triggered_by: '',
    },
    build_params: {
      branch: 'master',
      commit_message: "",
      commit_hash: "",
      workflow_id: 'primary',
    },
  };
//Now post the api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Open bitbucket.yml file of your project in your editor.&lt;/li&gt;
&lt;li&gt;For testing purpose, lets first create a step for Mobile Build in Bitbucket pipeline using node image.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- step:
          name: Run Android Build
          image: node:10.15.3
          script:
            - echo "Going to trigger Android Build"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If Pipeline run successfully after step 2, proceed further. If not, there is something wrong with yml file.&lt;/li&gt;
&lt;li&gt;We will apply Change set condition on step (the step should run only if we make changes in this folder) .
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- step:
          name: Run Android Build
          image: node:10.15.3
          condition:
            changesets:
              includePaths:
                - "mobile/**"
          script:
            - echo "Going to trigger Android Build"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt; We will Create folder inside Project with name “MobileBuilds” .&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the Folder we will add 2 files (Android.js ,Git.js).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now implement these steps here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get Latest git commit inside (git.js.&lt;a href="https://dev.to/mehakfatima/how-to-get-last-git-commit-in-js-file-3il6"&gt;https://dev.to/mehakfatima/how-to-get-last-git-commit-in-js-file-3il6&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Hit bitrise api inside the Android.js (&lt;a href="https://dev.to/mehakfatima/trigger-bitrise-build-using-bitrise-api-350p"&gt;https://dev.to/mehakfatima/trigger-bitrise-build-using-bitrise-api-350p&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Invoke Android.js file from Bitbucket Pipeline. Also include required dependencies like axios and git-last-commit.&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; - step:         
          name: Run Android Build
          image: node:10.15.3
          condition:
            changesets:
              includePaths:
                - "mobile/**"
          script:
            - echo "Going to trigger Android Build"
            - npm install axios
            - npm install git-last-commit
            - cd mobile/MobileBuilds
            - node Android.js $BITRISE_TOKEN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;$BITRISE_TOKEN is passing value  from Bitbucket’s variable to Android.js file.(&lt;a href="https://dev.to/mehakfatima/can-we-pass-bitbucket-variables-to-js-file-hnk"&gt;https://dev.to/mehakfatima/can-we-pass-bitbucket-variables-to-js-file-hnk&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Now just run the pipeline.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>devops</category>
      <category>reactnative</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Can we pass Bitbucket Variables to js File...</title>
      <dc:creator>Mehak Fatima</dc:creator>
      <pubDate>Wed, 13 Apr 2022 11:39:08 +0000</pubDate>
      <link>https://dev.to/mehakfatima/can-we-pass-bitbucket-variables-to-js-file-hnk</link>
      <guid>https://dev.to/mehakfatima/can-we-pass-bitbucket-variables-to-js-file-hnk</guid>
      <description>&lt;p&gt;&lt;strong&gt;Pre-Requisite:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to use Bitbucket pipeline.&lt;/li&gt;
&lt;li&gt;How to store Bitbucket variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Motivation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As you know we can store variable inside Bitbucket repository and can use them inside the Yml file to enhance Security but  can we use these variables inside js files?&lt;/p&gt;

&lt;p&gt;let’s see…&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can pass these variables to the file like this &lt;code&gt;node Android.js $BITRISE_TOKEN&lt;/code&gt;.This command will run the Android.js with the parameter. we can extract this Parameter inside the file like this
&lt;code&gt;const buildArgs = process.argv.slice(2);&lt;/code&gt;
buildArgs is the array here  so we will extract value like this &lt;code&gt;buildArgs[0]&lt;/code&gt; means value 1 &lt;code&gt;buildArgs[1]&lt;/code&gt; means value 2.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Trigger Bitrise Build Using Bitrise Api</title>
      <dc:creator>Mehak Fatima</dc:creator>
      <pubDate>Wed, 13 Apr 2022 11:37:25 +0000</pubDate>
      <link>https://dev.to/mehakfatima/trigger-bitrise-build-using-bitrise-api-350p</link>
      <guid>https://dev.to/mehakfatima/trigger-bitrise-build-using-bitrise-api-350p</guid>
      <description>&lt;p&gt;&lt;strong&gt;Pre-Requisite:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what is Bitrise.&lt;/li&gt;
&lt;li&gt;How to use Bitrise.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Motivation:&lt;/strong&gt;&lt;br&gt;
Now I can trigger bitrise builds from anywhere according to my need i am not bind.&lt;/p&gt;

&lt;p&gt;Let’s Start…&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create Android.js file inside the project.&lt;/li&gt;
&lt;li&gt;Collect Authorization token from bitrise &lt;a href="https://dev.tourl"&gt;https://devcenter.bitrise.io/api/authentication/&lt;/a&gt; .&lt;/li&gt;
&lt;li&gt;Import latest git commit method &lt;a href="https://dev.tourl"&gt;&lt;/a&gt; .&lt;/li&gt;
&lt;li&gt;Add following code inside the file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const axios = require('axios');
const {lastGitCommit} = require('./Git');
const buildArgs = process.argv.slice(2);
// we have to extract first index of perameters buildArgs[0]
const headers = {
  Authorization: 'Auth Token',
};
const gitCommit = [];
const gitInfo = async () =&amp;gt; {
  const response = await lastGitCommit();
  // console response and check if anything else you need.
  gitCommit.push(response);
  return response;
};
gitInfo();

setTimeout(function () {
  const data = {
    hook_info: {
      type: 'bitrise',
      triggered_by: 'Bitrise BUild',
    },
    build_params: {
      branch: 'master',
      commit_message: gitCommit[0].subject,
      commit_hash: gitCommit[0].hash,
      workflow_id: 'android',
    },
  };
  const post = async (url, data, headers) =&amp;gt; {
    const payload = {
      method: 'post',
      url: url,
      headers,
      data,
    };
     const response = await axios(payload);
     return response;
  };
  const triggerNewBuild = async () =&amp;gt; {
    let response;
    try {
      response = await post(
        'https://api.bitrise.io/v0.1/apps/slug_num/builds',
        data,
        headers,
      );
    } catch (exception) {
      error(`Error fetching All builds${exception} `);
    }
    return response;
  };
  triggerNewBuild();
}, 5000)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run file with the command Node Android.js.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>mobile</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to get Last git Commit in Js file</title>
      <dc:creator>Mehak Fatima</dc:creator>
      <pubDate>Wed, 13 Apr 2022 11:35:27 +0000</pubDate>
      <link>https://dev.to/mehakfatima/how-to-get-last-git-commit-in-js-file-3il6</link>
      <guid>https://dev.to/mehakfatima/how-to-get-last-git-commit-in-js-file-3il6</guid>
      <description>&lt;p&gt;&lt;strong&gt;Pre-Requisite:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to use git.&lt;/li&gt;
&lt;li&gt;Purpose of git.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Motivation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can collect following values inside the js file.&lt;/li&gt;
&lt;li&gt; What is the subject of latest git commit.&lt;/li&gt;
&lt;li&gt; what is the branch of latest git commit.&lt;/li&gt;
&lt;li&gt; what is the hash of latest git commit and so on.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "shortHash": "d2346fa",
  "hash": "d2346faac31de5e954ef5f6baf31babcd3e899f2",
  "subject": "initial commit",
  "sanitizedSubject": "initial-commit",
  "body": "this is the body of the commit message",
  "authoredOn": "1437988060",
  "committedOn": "1437988060",
  "author": {
    "name": "Ozan Seymen",
    "email": "oseymen@gmail.com"
  },
  "committer": {
    "name": "Ozan Seymen",
    "email": "oseymen@gmail.com"
  },
  "notes": "commit notes",
  "branch": "master",
  "tags": ['R1', 'R2']
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node provide us Library for this purpose &lt;code&gt;npm i git-last-commit&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create a file Git.js and add this function here.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const git = require("git-last-commit");
function getGitCommit() {
  return new Promise((res, rej) =&amp;gt; {
    git.getLastCommit((err, commit) =&amp;gt; {
      if (err) {
        return rej(err);
      } else {
        return res(commit);
      }
    });
  });
}
module.exports = {
  lastGitCommit: getGitCommit,
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This function will return the Value of latest git commit, Call this function in other files like this.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const axios = require('axios');
const {lastGitCommit} = require('./Git');
const gitCommit = [];
const gitInfo = async () =&amp;gt; {
  const response = await lastGitCommit();
  // console response and check if anything else you need.
  gitCommit.push(response);
  return response;
};
gitInfo();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt; Now just extract the values from gitCommit.&lt;/li&gt;
&lt;li&gt;const hash= gitCommit[0].subject.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>node</category>
    </item>
  </channel>
</rss>
