「AWS CodeDeploy」:修訂間差異

出自Gea-Suan Lin's Wiki
跳至導覽 跳至搜尋
本頁面具有訪問限制。如果您看見此訊息,這代表您沒有訪問本頁面的權限。
行 59: 行 59:


=== 外部機器 ===
=== 外部機器 ===
 當機器不在[[EC2]]上時,有幾種方法可以註冊到CodeDeploy的系統上,會被稱為On-Premises Instance。這邊我們介紹的方法是一台機器給一個IAM user的方式:
 當機器不在[[EC2]]上時,有幾種方法可以註冊到CodeDeploy的系統上,會被稱為On-Premises Instance。這邊我們介紹的方法是一台機器給一個IAM user的方式 。先在一般的機器上產生出對應的權限與設定檔(不需要在需要註冊的機器上)


先在一般的機器上產生出對應的權限與設定檔(不需要在需要註冊的機器上):
<syntaxhighlight lang="shell-session">
<syntaxhighlight lang="shell-session">
$ aws deploy register --instance-name api-example-1 --region us-east-1
$ aws deploy register --instance-name api-example-1 --region us-east-1
行 67: 行 66:


 然後把生出的<code>.yml</code>檔案傳到要註冊的機器上:
 然後把生出的<code>.yml</code>檔案傳到要註冊的機器上:
<syntaxhighlight lang="shell-session">
<syntaxhighlight lang="shell-session">
$ scp codedeploy.onpremises.yml api-example-1:/tmp/
$ scp codedeploy.onpremises.yml api-example-1:/tmp/
行 72: 行 72:


 然後在要註冊的機器上執行<code>aws deploy install</code>:
 然後在要註冊的機器上執行<code>aws deploy install</code>:
<syntaxhighlight lang="shell-session">
<syntaxhighlight lang="shell-session">
$ cd /tmp
$ cd /tmp

於 2018年5月10日 (四) 02:47 的修訂

AWS CodeDeployAWS提供的服務之一,用於發佈伺服器端的軟體。

簡介

AWS CodeDeploy是一套伺服器軟體佈署的服務,跟其他軟體佈署不同的點主要在於:

  • SSH由外部連入更新,是由伺服器端主動取得檔案並且佈署。

這點在雲端時代特別重要。佈署發起人不容易知道現在線上有哪些機器,甚至有可能在開新機器時(如Auto Scaling)有race condition,而導致佈署發起人不知道有新機器需要佈署。

安裝

在伺服器端需要安裝Agent,這隻Agent會負責取得檔案並且執行設定的步驟[1]。以Ubuntu的環境為例子來說[2],會需要先安裝Ruby 2.0(在14.04下)或Ruby 2.3(在16.04下)後,取得安裝設定檔執行:

$ sudo apt install ruby2.3
$ cd /tmp
$ wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
$ chmod 755 install
$ sudo ./install auto

在非EC2的機器上可能會跑比較久(需要等169.254.169.254的timeout,在EC2上時這個IP會有HTTP服務提供資訊給Instance)。

然後看Agent是否有啟動:

$ sudo service codedeploy-agent status

如果沒有的話可以用start啟動:

$ sudo service codedeploy-agent start

設定

軟體本身有兩段設定,一段是Deploy的發起端透過awscli呼叫,另外一段是在appspec.yml定義在伺服器上的行為。

發起端

通常會有兩個指令:

  • 將現在的目錄打包起來傳到S3上。可能會使用--ignore-hidden-files避免.git或是.svn被包進去,但這個方式會使得.htaccess不會被包進去,對於使用Apache的使用者來說要注意。
  • 要求CodeDeploy送指令到各機器上抓檔案。
$ aws deploy push --application-name "${APPLICATIO_NAME}" --region us-east-1 --s3-location s3://${S3_BUCKET}/${GIT_BRANCH}-${GIT_HASH}
$ aws deploy create-deployment --application-name "${APPLICATION_NAME}" --deployment-group-name "${DEPLOYMENT_GROUP_NAME}" --region us-east-1 --s3-location bucket="${S3_BUCKET},key=${GIT_BRANCH}-${GIT_HASH},bundleType=zip"

伺服器端

appspec.xml內最簡單的設定就是指定要將這包檔案解到哪邊:

version: 0.0
os: linux
files:
  - source: /
    destination: /srv/www.example.com

外部機器

當機器不在EC2上時,有幾種方法可以註冊到CodeDeploy的系統上,會被稱為On-Premises Instance。這邊我們介紹的方法是一台機器給一個IAM user的方式。先在一般的機器上產生出對應的權限與設定檔(不需要在需要註冊的機器上):

$ aws deploy register --instance-name api-example-1 --region us-east-1

然後把生出的.yml檔案傳到要註冊的機器上:

$ scp codedeploy.onpremises.yml api-example-1:/tmp/

然後在要註冊的機器上執行aws deploy install

$ cd /tmp
$ sudo aws deploy install --config-file codedeploy.onpremises.yml --region us-east-1

上面的指令目前會因為他想裝ruby2.0而爛掉(不知道什麼時候才會修好),我們只是要他把設定檔塞進系統。請照上面的方式改裝us-east-1install檔案即可(這邊的指令與上面是一樣的,為了方便這邊複製一份過來):

$ sudo apt install ruby2.3
$ cd /tmp
$ wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
$ chmod 755 install
$ sudo ./install auto

參考資料

外部連結