
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
github.comのリポジトリ展開用のキーに
github_key.pub公開キーを追加します
(github.comリポジトリ設定の[キーのデプロイ]タブ)
Ansibleを使用したサーバーのセットアップ
[ root@1.1.1.1 ] yum install ansible [ root@1.1.1.1 ] adduser ansible
プロジェクトサーバーに公開キー
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
ファイル/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が呼び出されます。これにより、プロジェクトのあるサーバーに最新バージョンのコードがポストされます。
この指示が誰かに役立つことを願っています。