github.comでプロジェクトの連続配信CDを構成する
必要です:
- github.comのリポジトリ
- Ansibleのサーバー(たとえば、ip:1.1.1.1)
- プロジェクトを展開するためのサーバー(たとえば、ip:2.2.2.2)
- プロジェクトをカスタマイズしたローカルマシン
- 基本的なコマンドラインの知識、 Travis CIおよびAnsible
プロジェクトを展開するサーバーのセットアップ
[ root@2.2.2.2 ] adduser ansible # , Ansible [ root@2.2.2.2 ] su - ansible [ ansible@2.2.2.2 ] ssh-keygen -t rsa -b 4096 -C 'github' -f ~/.ssh/github_key # passphrase [ ansible@2.2.2.2 ] eval "$(ssh-agent -s)" [ ansible@2.2.2.2 ] ssh-add ~/.ssh/github_key [ ansible@2.2.2.2 ] cat ~/.ssh/github_key.pub # github_key.pub
github.comのリポジトリ展開用のキーにgithub_key.pub公開キーを追加します
(github.comリポジトリ設定の[キーのデプロイ]タブ)
Ansibleを使用したサーバーのセットアップ
[ root@1.1.1.1 ] yum install ansible [ root@1.1.1.1 ] adduser ansible # , Travis [ root@1.1.1.1 ] su - ansible [ ansible@1.1.1.1 ] ssh-keygen -t rsa -b 4096 -C 'ansible' -f ~/.ssh/ansible_key # passphrase [ ansible@1.1.1.1 ] eval "$(ssh-agent -s)" [ ansible@1.1.1.1 ] ssh-add ~/.ssh/ansible_key [ ansible@1.1.1.1 ] cat ~/.ssh/ansible_key.pub # ansible_key.pub
プロジェクトサーバーに公開キーansible_key.pubを追加します
[ ansible@2.2.2.2 ] mcedit .ssh/authorized_keys [ ansible@2.2.2.2 ] chmod 600 .ssh/authorized_keys
プロジェクトのサーバーをhosts.yml ipファイルに追加します
[ ansible@1.1.1.1 ] mcedit /path/to/ansible/hosts.yml
hosts.yml
[ansible] 2.2.2.2
masterブランチの現在のバージョンをアップロードする小さなプレイブックを書く
[ ansible@1.1.1.1 ] mcedit /path/to/ansible/playbook.yml
playbook.yml
- hosts: all user: ansible tasks: - name: Clone git repo git: repo: ssh://git@github.com/{github_username}/{github_repo}.git dest: /home/ansible/var/www/{github_repo} version: master accept_hostkey: yes force: yes
Travisを構成する
github.comアカウントからtravis-ci.orgに登録します
目的のリポジトリの統合を有効にします
Travisリポジトリー設定で、以下を有効にします。
- .travis.ymlが存在する場合のみビルドする
- ブランチの更新をビルドする
プロジェクトがデプロイされているローカルマシンで、 travisユーティリティをインストールしてログインします。
[ user@local ] gem install travis [ user@local ] travis login --auto
パスフレーズなしでsshキーを生成します。これを使用して、 TravisはAnsibleサーバーに接続します。
[ user@local ] ssh-keygen -t rsa -b 4096 -C 'travis' -f travis_key # passphrase Travis Ansible [ user@local ] cat travis_key.pub # travis_key.pub
ファイル/home/ansible/.ssh/authorized_keysの公開鍵travis_key.pubをAnsibleサーバーに追加します
[ ansible@1.1.1.1 ] mcedit /home/ansible/.ssh/authorized_keys [ ansible@1.1.1.1 ] chmod 600 /home/ansible/.ssh/authorized_keys
travisユーティリティを使用して秘密キーを暗号化します。
[ user@local ] travis encrypt-file travis_key --add
出力には、ファイルtravis_key.encおよび.travis.ymlが表示されます。 .travis.ymlファイルには、 次のようにキーを復号化する行があります。
openssl aes-256-cbc -K $encrypted_412afa050e5f_key -iv $encrypted_412afa050e5f_iv -in travis_key.enc -out /tmp/travis_key -d
両方のファイルをgitに追加します:
[ user@local ] git add travis_key.enc .travis.yml
.travis.ymlファイルを編集します。
[ user@local ] mcedit /path/to/repo/.travis.yml
.travis.yml
language: node_js # node_js install: true # sudo: false branches: # master only: - master script: - openssl aes-256-cbc -K $encrypted_412afa050e5f_key -iv $encrypted_412afa050e5f_iv -in travis_key.enc -out /tmp/travis_key -d # - eval "$(ssh-agent -s)" - chmod 600 /tmp/travis_key - ssh-add /tmp/travis_key - ssh -o "StrictHostKeyChecking no" ansible@1.1.1.1 'ansible-playbook playbook.yml' # Ansible playbook
変更をgitに注ぎます:
[ user@local ] git push origin master
その後、このビルドについてTravisのリポジトリページに表示されます。
トラビスビルド
The command "openssl aes-256-cbc -K $encrypted_412afa050e5f_key -iv $encrypted_412afa050e5f_iv -in travis_key.enc -out /tmp/travis_key -d" exited with 0. 0.01s$ eval "$(ssh-agent -s)" Agent pid 1842 The command "eval "$(ssh-agent -s)"" exited with 0. 0.01s$ chmod 600 /tmp/travis_key The command "chmod 600 /tmp/travis_key" exited with 0. 0.01s$ ssh-add /tmp/travis_key Identity added: /tmp/travis_key (/tmp/travis_key) The command "ssh-add /tmp/travis_key" exited with 0. 16.68s$ ssh -o "StrictHostKeyChecking no" ansible@2.2.2.2 'ansible-playbook playbook.yml' Warning: Permanently added '2.2.2.2' (ECDSA) to the list of known hosts. PLAY [all] ********************************************************************* TASK [setup] ******************************************************************* ok: [2.2.2.2] TASK [Clone git repo] ********************************************************** changed: [2.2.2.2] PLAY RECAP ********************************************************************* 2.2.2.2 : ok=1 changed=1 unreachable=0 failed=0 The command "ssh -o "StrictHostKeyChecking no" ansible@2.2.2.2 'ansible-playbook playbook.yml" exited with 0. Done. Your build exited with 0.
これで、masterブランチに変更が加えられ、 Travisが起動し、 Ansibleが呼び出されます。これにより、プロジェクトのあるサーバーに最新バージョンのコードがポストされます。
この指示が誰かに役立つことを願っています。