Ansibleでgit cloneしてアーカイブをサーバにデプロイ
Ansible + Git
久しぶりにAnsibleを使った
本当は上の記事のように、「ssh-add」してからのSSHフォワーディングをやりたかったのだが、謎のエラーで進めなくなったのでもっと原始的な方法で用を済ませた。
Ansibleのスクリプトサンプル
- 要は、local_actionを使ってshellからgitを実行する
- これってただのシェルでは…?
- name: Deploy from Gitlab hosts: test-app-server user: ec2-user tasks: - name: Test Gitlab connection local_action: shell ssh -T git@gitlab.mordor.dev - name: Clone Repository locally local_action: shell git clone git@gitlab.mordor.dev/hogehoge.git - name: Archive Repository locally local_action: shell rm -f hogehoge.tar.gz && cd hogehoge && git archive HEAD --format=tar.gz --output=../hogehoge.tar.gz
特定コミットへの切り戻し
- 例えばansible実行時に --extra-vars "hash=xxx" と与えておくと、以下の方法で git archive HEAD でチェックアウトするソースを切り替えられる
- debug: msg="hash = {{ hash }}, double check = {{ hash | default('HEAD') }}" - debug: msg="Developer specified commit id = {{ hash | default('HEAD') }} ! The module has the commit hash will be deployed !" - name: Archive Repository locally local_action: shell rm -f hogehoge.tar.gz && cd hogehoge && git archive {{ hash | default('HEAD') }} --format=tar.gz --output=../hogehoge.tar.gz