2015-11-01 208 views
1

我需要獲取自上次部署(git diff)以來發生更改的文件列表,gzip這些文件並將這些文件發送到S3。我需要獲取所有的css文件並將它們gzip,然後我需要獲取所有html文件,gzip它們以及所有的JS文件並將它們gzip。只有在最後一次提交後發生了變化。gzip和git diff以及s3

我可以輕鬆地從上次部署中獲取提交哈希。我也有來自當前部署的提交散列。

下面是我有gzips所有的css文件的代碼。我想修改這個代碼僅gzip的所有CSS文件,改變了

find . -iname '*.css' -exec gzip -9 {} \; -exec mv {}.gz {} \; 

下面是我不得不AWS示例代碼同步所有的HTML文件(我有獨立的AWS同步命令爲CSS,JS, HTML文件,因爲我需要不同的內容類型和內容編碼和緩存參數)。我想修改這個代碼僅AWS同步,改變

aws s3 sync . s3://test --content-type "text/html" --cache-control "max-age=31536000" --exclude "*" --include "*.html" 

如何配合這個代碼git的差異的HTML文件?只能gzip已更改的文件?並且只會同步更改過的文件(git diff返回的文件列表中的文件)?

另外,我想複製一些文件,git diff返回到一個單獨的目錄。例如,需要將images /文件夾中的所有文件複製到StaticAssets /目錄。如何獲取已更改並位於圖像文件夾中的文件列表並將它們複製到單獨的文件夾?

我想用bash來做到這一點。

所以我知道我可以使用git diff --name-only命令。我的問題是,我該如何使用該命令在bash做到以下幾點:

  • Gzip已全部改變 css文件,Gzip已全部更改的js文件,Gzip已全部單獨改變的HTML文件(注意,該css文件可能位於另一個目錄內的目錄中)。我需要檢索父目錄中的所有css文件。因此,如果父目錄有文件夾測試,並且該文件夾有另一個名爲test1的文件夾,並帶有一個名爲test.css的css文件,我需要能夠將該css文件gzip即使其2個目錄下載到
  • AWS Sync全部已更改 js文件,AWS同步所有改變CSS的單獨文件
  • 改變以及在圖像複製的文件/文件夾複製到另一個目錄
+0

你有一個標籤或其他一些標記最後部署?你有部署文件的副本嗎? –

+0

我有上次部署的git commit hash。 – Jasmine

+0

你是否要求在這裏需要'git diff'命令?你在問如何將輸出鉤到'gzip'和's3 sync'(或其他)?你是否有任何代碼作爲這方面的嘗試? –

回答

0

Gzip all of changed css files, Gzip all of changed js files, Gzip all of the changed html files separately

的基本方式來獲得的改變列表文件是git diff --name-only

您可以過濾該列表以獲取僅帶有grep '.css$' -的CSS文件。 -表示要從STDIN而不是文件中讀取。

您可以使用xargs將該列表gzip從STDIN轉換爲參數列表。請務必使用gzip -k以避免刪除原始文件。

git diff --name-only <revision> | grep '.css$' - | xargs gzip -k 

對每個文件擴展名重複。

Sync all changed js files, sync all of changed css files separately.

傳遞適當的--include過濾器進入aws s3 sync。對於CSS,它是*.css.gz

您可能需要使用git clean -dxf清理這些文件。

Copy files that have changed and that are in the images/ folder to a another directory

再次使用git diff --name-onlyxargs但這次只是對images/目錄。使用cp -t <destination>將列表從xargs複製到目標目錄。

git diff --name-only <revision> images/ | xargs cp -t <destination dir> 
+0

這些命令不適用於我。例如對於最後一個命令,我得到錯誤:cp:缺少文件操作數 – Jasmine

+0

cp(GNU coreutils)8.22 版權所有(C)2013自由軟件基金會,Inc. 許可證GPLv3 +:GNU GPL版本3或更高版本。 這是免費軟件:你可以自由更改和重新分配它。 在法律允許的範圍內,不存在任何擔保。 作者:TorbjörnGranlund,David MacKenzie和Jim Meyering。 – Jasmine

+0

@Jasmine這可能意味着沒有更改的文件。 – Schwern

0
jsfiles=() 
cssfiles=() 
htmlfiles=() 
imagesfiles=() 

# Loop over the output from `git diff --name-only -z` to operate by file. 
while IFS= read -r -d '' file || [ -n "$file" ]; do 
    # gzip the '.js', '.css' and '.html' in place while looping 
    # also set up the arrays as valid arguments (hopefully) for the 'aws s3 sync' commands later. 
    case "$file" in 
     *.js) 
      gzip "$file" && mv "$file"{.gz,} && jsfiles+=(--include "$file") 
      ;; 
     *.css) 
      gzip "$file" && mv "$file"{.gz,} && cssfiles+=(--include "$file") 
      ;; 
     *.html) 
      gzip "$file" && mv "$file"{.gz,} && htmlfiles+=(--include "$file") 
      ;; 
     images/*) 
      imagesfiles+=("$file") 
      ;; 
    esac 
done <(git diff --name-only -z "$revision") 

aws s3 sync . s3://test --content-type "JS" --cache-control "max-age=31536000" --exclude "*" "${jsfiles[@]}" 
aws s3 sync . s3://test --content-type "CSS" --cache-control "max-age=31536000" --exclude "*" "${cssfiles[@]}" 
aws s3 sync . s3://test --content-type "text/html" --cache-control "max-age=31536000" --exclude "*" "${htmlfiles[@]}" 

cp "${imagesfiles[@]}" "$some_other_dir/"