[RUBY] Pass User credential to deployment destination with Jenkins + Capistrano

How do I send the source environment variable to the deployment destination in Capistrano?

Setting Access token with direnv and using it is a common practice of AWS CLI. Also, having the environment variable hold the User credential is a rather good way.

There are many Credentials such as username, password, access token, .... This is a story when you want to use this at the deployment destination.

Read the Jenkins User Handbook

It is written in Don Pisha in Jenkins User Handbook.

You can now refer to it in pipeline-stage-steps.

Another method is described in this section. You can give Jenkins Credential information, right? (It is a guy who operates with GUI) You can refer to that.

Usernames and passwords - Using a Jenkinsfile

It's just a little tricky, so I'll explain it.

  1. Register as hoge_credential
  2. Add instructions to use in Jenkinsfile.
  3. When referencing

Sample of Jenkinsfile

Jenkinsfile


pipeline {
    environment {
        USER_CREDENTIAL = credentials('$hoge_credential')
    }
}

It will be like this.

Send environment variables remotely from Capistrano

Since it can be referenced in Jenkinsfile, it can also be referenced as ENV from Capistrano.

If you write with USER_CREDENTIAL earlier,

deploy.rb


namespace :deploy do
  tasks :sample-task do
    on roles(:app) do
      with hoge_user: ENV['USER_CREDENTIAL_USR'] do
        with hoge_passwd: ENV['USER_CREDENTIAL_PSW'] do
          capture(:echo, '--user=$HOGE_USER', '--password=$HOGE_PASSWD')
        end
      end
    end
  end
end

It will be.

Recommended Posts

Pass User credential to deployment destination with Jenkins + Capistrano
Deploy to EC2 with CircleCi + Capistrano
Easy deployment with Capistrano + AWS (EC2) + Rails
Sample to start Ubuntu with Deployment with client-go
pass two arguments to the URI with link_to
How to redirect after user login with Spring-security