Echo in capistrano task make empty file - capistrano

I try to create REVISION file with full commit hash on deploy.
#early..
revision = %x[git rev-parse HEAD]
set :revision, revision
namespace :assets do
task :install do
on roles :all do
puts fetch(:revision, "") #it's good, print ee51dc1308a07cb0dfadd60b2a9d1b3485614034
execute :sh, "-c 'echo #{fetch(:revision, "")} > #{release_path}/REVISION2'"
execute :sh, "-c 'cat #{release_path}/REVISION2'" #empty output
execute :php, "#{release_path}/public/index.php assetic build"
end
end
end
As a result I have file REVISION2 with no content.
Capistrano Version: 3.4.0 (Rake Version: 10.1.0)
Dev machine: Ubuntu 14.04.2 LTS
Deploy to CentOS release 6.4 (Final)

I solve this problem by writing REVISION file on the local and then upload it with the following task
task :save_revision do
revision = %x[git rev-parse HEAD]
File.open("REVISION", 'w') { |file| file.write(revision) }
set :revision, revision #save for future
puts revision #print
on roles :all do
upload! "REVISION", "#{release_path}"
end
end

Related

Buildroot Package Makefile: How to fetch the most recent commit from git?

I am working on adding my own module to the build of buildroot using $BR2_External. The make file of my package is as follows,
##############################################################
#
# GPIO
#
##############################################################
GPIO_VERSION = '2851a05c9b613c1736f79faa185a11118b229852'
GPIO_SITE = '<URL of git repo>'
GPIO_SITE_METHOD = git
GPIO_GIT_SUBMODULES = YES
GPIO_MODULE_SUBDIRS = GPIO_driver/
# GPIO_MODULE_SUBDIRS += GPIO_driver/
# define LDD_BUILD_CMDS
# $(MAKE) $(TARGET_CONFIGURE_OPTS) -C $(#D)/misc-modules
# $(MAKE) $(TARGET_CONFIGURE_OPTS) -C $(#D)/scull
# endef
#
# # TODO add your writer, finder and finder-test utilities/scripts to the installation steps below
define GPIO_INSTALL_TARGET_CMDS
#module
# $(INSTALL) -m 0755 $(#D)/01_simple_LKM/* $(TARGET_DIR)/usr/bin
$(INSTALL) -m 0755 $(#D)/GPIO_driver/* $(TARGET_DIR)/usr/bin
endef
$(eval $(kernel-module))
$(eval $(generic-package))
This make file always pulls only a specific commit (mentioned in GPIO_VERSION variable) from gitHub. This is getting a little frustrating as, everytime I push new code to git I have to update the make file with the new commit number as well. So, is there any way to write the make file such that the most recent commit is pulled.

Push version Number to git/azure devops automatically

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.
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:
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.)

Powershell script in VSTS Build Definition not updating AssemblyVersion.cs

I have a Project hosted in VSTS and I'm experimenting with the Build Definitions. I'm trying to execute a powershell script that updates the AssemblyVersion.cs before the Project is built. When I run the script locally it works fine, but when it's ran during the Build process, the script runs without error, but the AssemblyVersion.cs is not updated.
$regex = '\[assembly: AssemblyVersion\("(.*)"\)\]'
$assemblyInfoPath = "..\Properties\AssemblyInfo.cs"
(Get-Content $assemblyInfoPath) | ForEach-Object {
if($_ -match $regex)
{
# Get current version, and update revision number
$version = [version]$matches[1]
$newVersion = "{0}.{1}.{2}.{3}" -f $version.Major, $version.Minor, $version.Build, ($version.Revision + 1)
'[assembly: AssemblyVersion("{0}")]' -f $newVersion
Write-Host "Version updated to: $newVersion"
}
else
{
$_
}
} | Set-Content $assemblyInfoPath
The Build output states that the version has been updated, but when I view AssemblyInfo.cs in the File Viewer it shows the old version.
The build process doesn't check in/commit changes made to source code automatically at the end of the process. You have to write a script to do that.
You need to checkin or push changes to remote repository.
If you are using TFVC, there is TFVC Build Tasks extension that includes TFVC-Check-in Changes step/task that can check in changes.
If you are using Git, you can add these steps/tasks to push changes:
Command Line (Tool: git; Arguments: add **\*.*; Working folder: $(build.sourcesdirectory))
Command Line (Tool: git; Arguments: commit -m "update"; Working folder: $(build.sourcesdirectory))
Command Line (Tool: git; Arguments: push origin HEAD:$(Build.SourceBranchName); Working folder: $(build.sourcesdirectory))
On the other hand, you also could build a custom build/release task and use it in your build/release. More information about custom build/release task, you can refer to: Add a build task
Note: It is not recommended to checkin/push changes to the remote repository in build or release.

Creating and calling custom deployment task using run_locally in Capistrano version 3

I have a static page that I want to compile locally using gulp. The command I would run in the local shell, from the directory that contains gulp and the gulpfile (set by compile_path in this example) would be "$> gulp build".
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, 'appname'
set :repo_url, 'git#bitbucket.org/appname.git'
set :compile_path, '/Users/nico/DevOps/repo/appname'
# Default branch is :master
set :branch, 'cap3'
namespace :deploy do
after :started, :notify do
desc 'Run gulp to compile the static site'
task :gulp_build do
run_locally do
execute "#{fetch(:compile_path)}/gulp", " build"
end
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
# execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
Basically, what I'm trying to achieve is a local precompile so that my deployment consists of simply sending the locally compiled files to a deployment location. when I execute "bundle exec cap staging deploy:gulp_build" I get:
cap aborted!
Don't know how to build task 'deploy:gulp_build'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/gems/capistrano-3.1.0/lib/capistrano/application.rb:15:in run'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/gems/capistrano-3.1.0/bin/cap:3:in'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/cap:23:in load'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/cap:23:in'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/ruby_executable_hooks:15:in eval'
/Users/nico/.rvm/gems/ruby-1.9.3-p545/bin/ruby_executable_hooks:15:in'
(See full trace by running task with --trace)
I realize that there are probably much better ways to deploy this, but it's a companion static site to a rails app which is being deployed successfully via capistrano, and I'd like to just use the same deployment method for both.
This was handled pretty well by creating a new task in the deploy namespace. in my code below are placeholders for real values that I didn't want to post on SO.
lib/capistrano/tasks/gulp_build_local.cap:
#assumes the gulpfile is in root of your cap install
namespace :deploy do
desc 'Run gulp to compile the static site'
task :gulp_build do
#run_locally doesn't play nice with the 'on' directive (it's 'on' localhost)
run_locally do
execute "gulp build"
end
end
end
deploy.rb:
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, '<appname>'
set :repo_url, 'git#bitbucket.org/<appname>.git'
namespace :deploy do
#custom tasks to build via gulp
before :deploy, 'gulp_build_local'
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
#nothing here, because there's no app server for this static site.
end
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
#nothing here
end
end
end
of course, once I figured this out I immediately deprecated it in favor of new tasks to install gulp in the release dir on the target, compiling there and linking the site root to the pub folder generated by the gulp process. Hopefully this learning experience will be useful for someone working though the use of run_locally, though.

Capistrano 3: How to store git revision into a file?

Is there a way how to fetch a git revision variable from Capistrano 3?
I can't figure out how to access capistrano variables:
namespace :deploy do
after :finished, :set_current_version do
on roles(:app) do
# dump current git version
within release_path do
execute :echo, "#{fetch(:revision_log_message)} >> public/version"
end
end
end
end
This one works
after :finished, :set_current_version do
on roles(:app) do
# dump current git version
within release_path do
execute :echo, "#{capture("cd #{repo_path} && git rev-parse --short HEAD")} >> public/version"
end
end
end
This feature is added in 3.0.1, see their changelog!