前一篇「BuildCode YAML拆解task為step範本」已經將BuildCode Job都拆解成為template,CI Pipeline還剩下BuildImage、DeployCloudRun兩個Job,這篇就延續前面的經驗,繼續拆解BuildImage Job吧!
BuildImage Job
- job: BuildImage
dependsOn: BuildCode
steps:
- task: DownloadBuildArtifacts@0
displayName: 下載Pipeline Artifact
inputs:
buildType: 'current'
cleanDestinationFolder: true
downloadType: 'single'
artifactName: '$(pipelineArtifact)'
downloadPath: '$(System.ArtifactsDirectory)/'
- task: ExtractFiles@1
displayName: Unzip zip
inputs:
archiveFilePatterns: '$(System.ArtifactsDirectory)/$(pipelineArtifact)/$(buildResultZipName)'
destinationFolder: '$(System.ArtifactsDirectory)/BuildImage'
cleanDestinationFolder: true
overwriteExistingFiles: true
- task: Docker@2
displayName: Build image
inputs:
repository: '$(imgRepository)'
command: 'build'
Dockerfile: $(buildDockerfile)
buildContext: '$(System.ArtifactsDirectory)/BuildImage'
arguments: '--no-cache'
tags: |
latest
- task: Docker@2
displayName: "Login to Container Registry"
inputs:
command: login
containerRegistry: $(imgRegistryService)
- task: Bash@3
displayName: Push docker image
inputs:
targetType: 'inline'
script: |
docker push -a $(imgRepository)
在這個Job裡面主要只有兩件事:
- 從Pipeline Artifacts下載成品檔案(前面壓縮的zip檔)並且解壓縮
- Build Docker Image
這裡我們把下載檔案並且解壓縮的部份拆解為一個step template:
parameters:
- name: artifactName
type: string
default: OutputFiles
- name: unzip
type: boolean
default: false
- name: zipFileName
type: string
default: ''
- name: unzipToFolderPath
type: string
default: ''
steps:
- task: DownloadBuildArtifacts@0
displayName: 下載Pipeline Artifact
inputs:
buildType: 'current'
cleanDestinationFolder: true
downloadType: 'single'
artifactName: '${{ parameters.artifactName }}'
downloadPath: '$(System.ArtifactsDirectory)/'
- ${{ if eq(parameters.unzip, true) }}:
- task: ExtractFiles@1
displayName: Unzip zip
inputs:
archiveFilePatterns: '$(System.ArtifactsDirectory)/${{ parameters.artifactName }}/${{ parameters.zipFileName }}'
${{ if ne(parameters.unzipToFolderPath, '') }}:
destinationFolder: ${{ parameters.unzipToFolderPath }}
${{ else }}:
destinationFolder: $(System.ArtifactsDirectory)/${{ parameters.artifactName }}/${{ replace(parameters.zipFileName, '.zip','') }}
cleanDestinationFolder: true
overwriteExistingFiles: true
在上面拆解為step template的內容中設計了一個unzip參數可以設定是否需要解壓縮,steps裡也可以看到透過條件判斷式來決定是否加入負責解壓縮的task與設定解壓縮路徑的屬性。如果解壓縮的路徑沒有設定(空字串),那就使用預設的路徑(zip檔案去掉副檔名)。
要留意的是task上面的條件判斷式前面有減號「-」,在屬性上面的則是沒有減號「-」的。
再把Docker Build的部份拆解成為另一個step template:
parameters:
- name: imgRepository
type: string
- name: imgTags
type: object
- name: buildDockerfile
type: string
default: Build.Dockerfile
- name: buildContext
type: string
- name: containerRegistry
type: string
steps:
- task: Docker@2
displayName: Build image
inputs:
repository: '${{ parameters.imgRepository }}'
command: 'build'
Dockerfile: ${{ parameters.buildDockerfile }}
buildContext: ${{ parameters.buildContext }}
arguments: '--no-cache'
tags: ${{ parameters.imgTags }}
- task: Docker@2
displayName: "Login to Container Registry"
inputs:
command: login
containerRegistry: ${{ parameters.containerRegistry }}
- task: Bash@3
displayName: Push docker image
inputs:
targetType: 'inline'
script: |
docker push -a ${{ parameters.imgRepository }}