Capistrano deploys not pulling latest commits - ruby-on-rails

I'm deploying a Rails app with Capistrano, to an Ubuntu server (EC2).
When I deploy, with --trace, everything appears to go fine.
When I look at the revisions log on the server, it shows the latest commit hash was used on the most recent deploy, however, when I go into that latest release directory (yes I confirmed that a new release directory was created and that I'm in that one) it doesn't have the most recent commits.
If I do a 'git pull origin master' from with the new release directory on the server, of course it pulls the latest commits.
Any idea why the git pull wouldn't be happening on the Capistrano deploy?
EDIT:
Here's the deploy.rb file:
lock "~> 3.14.0"
set :pty, true
set :application, "123abc"
set :repo_url, "git#github.com:123/abc.git "
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
set :branch, "master"
set :rbenv_ruby, File.read('.ruby-version').strip
append :linked_files, "config/secrets.yml"
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets"
namespace :deploy do
before :compile_assets, :force_cleanup_assets do
on release_roles(fetch(:assets_roles)) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, 'assets:clobber'
end
end
end
end
app_service_name = "#{fetch(:application)}-#{fetch(:stage)}"
services = ["#{app_service_name}-workers"]
desc "Restart application"
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute :sudo, :systemctl, :stop, app_service_name
sleep 1
execute :sudo, :systemctl, :start, app_service_name
# execute :sudo, :systemctl, :restart, app_service_name
end
end
desc "Restart Workers"
task :restart_services do
on roles(:app), in: :sequence, wait: 5 do
services.each { |service| execute "sudo systemctl restart #{service}" }
end
end
desc "Start Workers"
task :start_services do
on roles(:app), in: :sequence, wait: 5 do
services.each { |service| execute "sudo systemctl start #{service}" }
end
end
desc "Stop Workers"
task :stop_services do
on roles(:app), in: :sequence, wait: 5 do
services.each { |service| execute "sudo systemctl stop #{service}" }
end
end
end
after "deploy:publishing", "deploy:restart"
after "deploy:publishing", "deploy:restart_services"

Is your organization using a proxy with ca certificate?.
Are you pulling from github site using SSL or from another git clone with a self signing certificate?.
Please try to su to the user used for the deployment, and attempt git pull, to see if it works?.
Are you using Tokens to authenticate or credentials or certificates?.
I would attempt to tcpdump to see what's going on, if effectively it attempts to connect to github.
Your deploy works with full clone or pull?. Can you deploy using full clone?.
Are you using SSH or HTTPS, and default or special ports?.
Can you publish the trace, or at least check that you don't have something like:
Connection refused - connect(2)
I guess that the ending spaces after your repourl are not in your final file.
Cheers

This could happen because of ownership/permissions inside <deploy_path>/repo, for example if once you had run deploy or git pull on server under other user.
Make sure that you have correct username in your deploy/<env>.rb configs and chown -r that_user:that_user <deploy_path>/repo (and may be other directories as well)

Related

Don't 'artifactoryPublish' when running Artifactory Jenkins Plugin

Is it possible to do a gradle.run (see below), without running the artifactoryPublish task? I thought I could accomplish this by specifying the tasks parameter, but the plugin appears to add the task back in. For example, the following:
def server = Artifactory.server('artifactory-primary')
def gradle = Artifactory.newGradleBuild()
gradle.resolver server: server, repo: 'gradle-all-virtual'
gradle.deployer server: server, repo: 'gradle-libs-snapshot-local'
gradle.deployer.mavenCompatible = true
gradle.useWrapper = true
gradle.usesPlugin = true
def buildInfo = gradle.run(
rootDir: ".",
buildFile: 'build.gradle',
tasks: 'build',
switches: '--no-daemon -x check')
server.publishBuildInfo buildInfo
Results in:
...
gradlew -x check build artifactoryPublish -b build.gradle
...
When what I really want is:
...
gradlew -x check build -b build.gradle
...
Ultimately I want to build in one stage and deploy in another.
The same snippet but with references to artifactory removed from my Gradle file and with Tamir's addition added in:
def server = Artifactory.server('artifactory-primary')
def gradle = Artifactory.newGradleBuild()
gradle.resolver server: server, repo: 'gradle-all-virtual'
gradle.deployer server: server, repo: 'gradle-libs-snapshot-local'
gradle.deployer.mavenCompatible = true
gradle.deployer.deployArtifacts = false
gradle.useWrapper = true
gradle.usesPlugin = false
def buildInfo = gradle.run(
rootDir: ".",
buildFile: 'build.gradle',
tasks: 'build',
switches: '--no-daemon -x check')
server.publishBuildInfo buildInfo
Produces the same result.
The artifactoryPublish task is added by default, you can see that in the Jenkins Artifactory plugin code.
If you prefere not to deploy artifacts to artifactory you can do so by configuring deployer.deployArtifacts = false.
In your case:
gradle.deployer.deployArtifacts = false
If you want to build you project in two phases you can once build it with deployArtifacts=false and in the second time to build it with deployArtifacts=true

Rake task failing to compare current branch to string

I am writing a rake task for my rails app to do some stuff after I've determined what the current branch is, but I'm having trouble comparing the current branch to master. My rake task is bellow:
desc "TODO"
task repo: :environment do
branch = `git rev-parse --abbrev-ref HEAD`
if branch.eql? "master"
puts "On master!"
else
puts "Not on master!"
end
end
I've checked, and git rev-parse --abrev-ref HEAD returns "master". I also double checked that the class of branch is String. I'm pretty confused why this isn't working.
Any suggestions?

Ssh authentication error while deploying using capistrano

I am trying to deploy my rails application (Ruby 2.1.2 and Rails 4.1.4) through capistrano from mac. I have ssh keys set up on server. But i keep getting authentication error whenever i try to deploy. The error is:
SSHKit::Runner::ExecuteError: Exception while executing on host xxx.xxx: Authentication failed for user deploy#xxx.xxx
followed by:
Net::SSH::AuthenticationFailed: Authentication failed for user deploy#xxx.xx
This is my staging.rb:
server "xxx.xx.xxx", user: "deploy", roles: %w{web app db}
set :ssh_options, {
user: "root",
forward_agent: false,
keys: '~/.ssh/id_rsa',
auth_methods: %w(publickey password)
}
set :branch, "master"
set :rails_env, "staging"
I am able to login to server via terminal using ssh root#xxx.xx but cannot login with capistrano. Any help or advice will be appericiated.
At first. You use two different users in one config. Choice one and edit your staging.rb
Also I am not sure that using public key is a good way. Try to add private key for user Deploy. Then if you able to login as deploy
ssh -i id_rsa deploy#xxx.xx.xx.xx
try to update gem net-ssh to version 3.0.1. Then you can write your config like
set :ssh_options, {
user: "deploy",
keys: ["~/.ssh/id_rsa"]
}
I have faced the same issue for my application https://www.wiki11.com.
Those who are getting error
Net::SSH::AuthenticationFailed: Authentication failed for user deploy#XXX.XX.XX.XXX
Here is the solution,
First of all you need to ssh to your server and run
eval `ssh-agent`
and then
ssh-add ~/.ssh/id_rsa
and now change
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
#...
to
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa) }
#...
I just removed pub from id_rsa.pub.
And then run
cap production deploy:initial
It should work now.

rails app deploy setting failed

I'm following the this site.
And I'm in step 6(6. Deploy!).
So I executed below the command in turn.
$ git add .
$ git commit -am "add deployment configs"
$ git push master
$ cap deploy:setup
And all command was successfully executed.
Next, I tried to cap deploy command.
But I get some error.
$ cap deploy
* 2013-06-04 19:19:27 executing `deploy'
triggering before callbacks for `deploy'
* 2013-06-04 19:19:27 executing `deploy:check_revision'
WARNING: HEAD is not the same as origin/master
Run `git push` to sync changes.
$
So, I try git push and I get a message...
$ git push
Everything up-to-date
...
What is the problem??
I don't know that...
What should I do??
more informateion---
$ git branch
* master
$ git status
# On branch master
nothing to commit (working directory clean)
$ git remote
origin
Edit:
I also tried to $ git push origin master. And I get a same message that is nothing to commit (working directory clean)
Edit2:
$ git rev-parse HEAD
c3e758f2d47bb0bc126de91560905a1893fe08c6
$ git rev-parse origin/master
c3e758f2d47bb0bc126de91560905a1893fe08c6
Check your current branch and deploy.file.
In the deploy.file, it could exist the following code snippet:
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
So you must be on the same branch set in this code.
To fix, change the following line:
unless `git rev-parse HEAD` == `git rev-parse origin/${YOUR_CURRENT_BRANCH}`
The following worked nice for/to me.
Try:
git remote show origin
If you get an error, ask for all remotes:
git remote -v
and set remote origin:
git remote add origin git#bitbucket.org:xyz/abc.git
Now 'show origin' will provide the correct answer:
git remote show origin
try:
git push origin master
Looks like you may not be pushing the code back to the remote repo.
You should be able to edit your .git/config file so in the future you can just type "git push"
I think your deploy.rb file is not set up correctly. Check line 14 where your repository is set up. Make sure it matches your github repo.
If that doesn't work, try this: after task :check_revision, roles: :web do add these lines:
puts `git rev-parse HEAD`
puts `git rev-parse origin/master`
Hopefully that will give you some more info and you can post the results.

How can I create identical cron jobs for staging and production with the whenever gem?

I have one job in schedule.rb:
set :output, File.expand_path('../log/whenever.log', __FILE__)
set :job_template, "bash -l -c 'source ~/.bashrc ; :job'"
every 1.day, :at => '12:01 am' do
runner "MyModel.do_something"
end
In my staging deployment (bash) script I have this line to write to cron:
ssh $SERVER "cd $DEPLOY_TO && whenever --set environment=staging -w"
And this line in the production deployment script:
ssh $SERVER "cd $DEPLOY_TO && whenever --set environment=production -w"
This works fine and creates the job when I deploy either environment. The problem is that whenever sees them both as one job so it gets overwritten by whichever environment was last deployed:
# Begin Whenever generated tasks for: /Users/simon/apps/myapp/staging/config/schedule.rb
1 0 * * * bash -l -c 'source ~/.bashrc ; cd /Users/simon/apps/myapp/staging && script/rails runner -e staging 'MyModel.do_something' >> /Users/simon/apps/myapp/staging/log/whenever.log 2>&1'
# End Whenever generated tasks for: /Users/simon/apps/myapp/staging/config/schedule.rb
and...
# Begin Whenever generated tasks for: /Users/simon/apps/myapp/production/config/schedule.rb
1 0 * * * bash -l -c 'source ~/.bashrc ; cd /Users/simon/apps/myapp/production && script/rails runner -e production 'MyModel.do_something' >> /Users/simon/apps/myapp/production/log/whenever.log 2>&1'
# End Whenever generated tasks for: /Users/simon/apps/myapp/production/config/schedule.rb
What's a sensible way to add the same cron job for two separate environments on the same server?
You can namespace your whenever tasks by using something similar to the following:
# Whenever
set :whenever_environment, defer { stage }
set :whenever_identifier, defer { "#{application}-#{stage}" }
require "whenever/capistrano"
In the above example, stage is the variable that contains the environment. Change it to whatever you are using.
The Capistrano integration section at https://github.com/javan/whenever goes into a bit more detail if you need it.
For capistrano-v3-integration add require "whenever/capistrano" to Capfile and set set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" } to config/deploy.rb
capistrano-v3-integration

Resources