appspec.yml是YAML格式、用於定於CodeDeploy服務在整個階段所做的操作和文件拷貝路徑和權限等。首先,這個文檔名稱必須是appspec.yml
,而且文檔中的空格個數也有嚴格的要求,文章最後面會講到。
appspec.yml文檔結構:
version: 0.0 os: operating-system-name files: source-destination-files-mappings permissions: permissions-specifications hooks: deployment-lifecycle-event-mappings
version這裏,類似於aws api的版本號,目前只能寫0.0
os這裏可接受的選項是linux和windows,全小寫
files段,定於文件映射關係,例如下面這段
files: - source: Config/config.txt destination: /webapps/Config - source: source destination: /webapps/myApp
source文件路徑是是相對於本版包的相對路徑,如果是/
,表示本版包裏的全部文件和目錄
destination這裏是被部署服務器的完整路徑(絕對路徑)
permission段:用於定義和描述被拷貝到目標服務器上的文件拷貝後的權限
例如下面這個例子
permissions: - object: /home/webapp/tomcat/webapps/cms-front pattern: "**" except: /home/webapp/tomcat/webapps/cms-front/version.ini owner: webapp group: webapp mode: 644 acls: - u:read-only:r - u:wangfei:rw context: user: unconfined_u type: httpd_sys_content_t range: s0 type: - file
object是必選項,也就是你的版本包部署路徑
pattern是可選項,用於匹配想要賦權的文件,"**"
和不填,表示匹配所有文件(可選項)
except就是排除在pattern中被匹配的文件(可選項)
owner和group好理解,改變文件屬主(可選項)
mode這裏可接受類似644和755這樣的權限,或者4755這樣的帶粘滯位的賦權(可選項)
acls中設置Access Control List,這裏我們可以給read-only用戶只讀權限(可選項)
context是給SELinux用的,不熟悉(可選項)
type可選項是file和directory,用於表述被賦權對象類型,只是文件,還是隻是目錄,不填表示版本包裏的所有文件和目錄
hooks區段就是定義各個階段執行的操作
先介紹下CodeDeploy整個部署過程
1. Start,不可自定義操作
2. ApplicationStop,可定義停服過程
3. DownloadBundle,下載版本包,不可自定義該階段操作,linux系統下,版本包會被下載到/opt/codedeploy-agent/deployment-root/deployment-group-id/deployment-id/deployment-archive
,windows系統會被下載到C:\ProgramData\Amazon\CodeDeploy\deployment-group-id\deployment-id\deployment-archive
4. BeforeInstall,定義版本包文件複製到目標目錄之前的操縱,這裏可定義備份操作
5. Install,該過程不可被hook,只能通過appspec.yml中的路徑和權限等來定義,例如file區段,定義文件拷貝路徑
6. AfterInstall,文件複製完之後的操作,可以吧permisson中的定義在此處用腳本來實現
7. ApplicationStart,可被hook來進行起服前的自定義行爲
8. ValidateService,部署被驗證完成之後可被hook,可用於定義完成之後清理現場。
9. End,結束,不可被hook
hooks: deployment-lifecycle-event-name - location: script-location timeout: timeout-in-seconds runas: user-name
hook裏面的deployment-lifecycle-event-name
,可選項爲:
ApplicationStop,BeforeInstall,AfterInstall,ApplicationStart,ValidateService
。
location寫該階段需要執行的腳本文成和路徑,相對版本包的相對路徑,例如scripts/full_backup.sh
,location可以寫多個,每行一個。
timeout時間爲超時的秒數,超過指定時間腳本還沒完成就算部署失敗,默認是1800秒。注意hook階段的總超時時間是1小時,也就是說在該階段所有腳本的執行時間必須在1小時內完成,否則會被認爲是失敗了。
runas,可選項,定義腳本執行的身份
關於文件中的空格必須是指定的個數,例如下面例子中,[4]表示4個空格,不能多也不能少
version:[1]version-number os:[1]operating-system-name files: [2]-[1]source:[1]source-files-location [4]destination:[1]destination-files-location permissions: [2]-[1]object:[1]object-specification [4]pattern:[1]pattern-specification [4]except:[1]exception-specification [4]owner:[1]owner-account-name [4]group:[1]group-name [4]mode:[1]mode-specification [4]acls: [6]-[1]acls-specification [4]context: [6]user:[1]user-specification [6]type:[1]type-specification [6]range:[1]range-specification [4]type: [6]-[1]object-type hooks: [2]deployment-lifecycle-event-name: [4]-[1]location:[1]script-location [6]timeout:[1]timeout-in-seconds [6]runas:[1]user-name
解析出來就是這樣
version: 0.0 os: linux files: - source: / destination: /var/www/html/WordPress hooks: BeforeInstall: - location: scripts/install_dependencies.sh timeout: 300 runas: root AfterInstall: - location: scripts/change_permissions.sh timeout: 300 runas: root ApplicationStart: - location: scripts/start_server.sh timeout: 300 runas: root ApplicationStop: - location: scripts/stop_server.sh timeout: 300 runas: root