0

I have these three commands in my package.json

{
  "build-major": "npm version major --no-git-tag-version && node ./replace.build.js && node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod --base-href /QMobile/",
    "build-minor": "npm version minor --no-git-tag-version && node ./replace.build.js && node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod --base-href /QMobile/",
    "build-patch": "npm version patch --no-git-tag-version && node ./replace.build.js && node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod --base-href /QMobile/",
}

My replace.build.js Contains following Code:

var replace = require('replace-in-file');
var package = require("./package.json");
var buildVersion = package.version;
const options = {
    files: 'src/environments/environment.prod.ts',
    from: /version: '(.*)'/g,
    to: "version: '"+ buildVersion + "'",
    allowEmptyPaths: false,
};

const htmlOptions={
    files:'src/index.html',
    from: /<meta name="version" content="(.*)">/g,
    to: '<meta name="version" content="'+buildVersion+'">',
    allowEmptyPaths: false
}

try {
    let changedFiles = replace.sync(options);
    if (changedFiles == 0) {
        throw "Please make sure that file '" + options.files + "' has \"version: ''\"";
    }

    let indexFileChanged = replace.sync(htmlOptions);

    if (indexFileChanged == 0) {
        throw "Index.html Version Change Failed";
    }

    console.log('Build version set: ' + buildVersion);
}
catch (error) {
    console.error('Error occurred:', error);
    throw error
}

So every time i take my Angular Build, i get 3 files changes?

index.html, environment.prod.ts and package.json

I am looking at a solution to make these changes get pushed to the current branch too while build is in Process or a Standard way to maintain the Build Number in sync with all Branches.

mike
  • 1,233
  • 1
  • 15
  • 36
saikiran
  • 2,247
  • 5
  • 27
  • 42

1 Answers1

1

I am looking at a solution to make these changes get pushed to the current branch too while build is in Process or a Standard way to maintain the Build Number in sync with all Branches.

Just put a git-related task at the end of your pipeline to push the changed files to remote branch. You can use CMD task, Powershell task or third-party git tasks here to push the changed files.

For my pipeline that calls Replace token task to modify one file:

enter image description here

1.The cmd task at the start of the pipeline:

git checkout $(Build.SourceBranchName)
git config --global user.email "xxx@xxx.com"
git config --global user.name "xxx"

For build source directory, git repo is in HEAD detached by default, so we need to switch to the build branch by git checkout first.

2.The cmd task at the end of the pipeline:

git add .
git commit -m "Update files in Build $(Build.BuildNumber)."
git push https://{MyPat}@dev.azure.com/{OrganizationName}/{ProjectName}/_git/{RepoName} $(Build.SourceBranchName)

Check this similar issue for more details about git push command. About PAT see here. You can also define the PAT as an secret variable and reference it in command.

You can put your real tasks between the first CMD and the last CMD, then every time when the pipeline is finished, the changes will be pushed to current branch. (With BuildNumber in commit message.)

LoLance
  • 25,666
  • 1
  • 39
  • 73