연습 - 템플릿 사양 게시

완료됨

팀에서 회사의 새로운 거버넌스 모델을 준수하는 보안 강화 Bicep 파일을 빌드했습니다. 강화된 Bicep 파일 중 하나는 Linux 기반 Azure App Service 앱을 배포합니다. 이 연습에서는 배포 파이프라인을 사용하여 Bicep 파일을 템플릿 사양으로 게시합니다.

프로세스 중에 다음을 수행합니다.

  • 파이프라인에 린팅 스테이지를 추가합니다.
  • 템플릿 사양을 게시하는 파이프라인 스테이지를 추가합니다.
  • 파이프라인이 성공적으로 시작되고 완료되는지 확인합니다.
  • Azure에서 게시된 템플릿 사양을 확인합니다.

파이프라인에 린팅 스테이지 추가

리포지토리에는 시작점으로 사용할 수 있는 파이프라인 정의 초안이 포함되어 있습니다.

  1. Visual Studio Code에서 리포지토리의 루트에 있는 template-specs/linux-app-service 폴더를 확장합니다.

  2. pipeline.yml 파일을 엽니다.

    Screenshot of Visual Studio Code that shows the location of the pipeline definition file.

  3. 파일 맨 아래에서 # To be added과(와) 다음 린팅 스테이지 정의를 교체합니다.

    stages:
    
    - stage: Lint
      jobs:
      - job: LintCode
        displayName: Lint code
        steps:
          - script: |
              az bicep build --file $(TemplateSpecFilePath)
            name: LintBicepCode
            displayName: Run Bicep linter
    

    리포지토리에는 경고 대신 오류를 내보내도록 Linter를 구성하는 bicepconfig.json 파일이 있습니다. 린팅 스테이지 중에 오류가 발생하면 파이프라인이 실패합니다.

    YAML 파일은 들여쓰기를 구분합니다. 이 코드를 입력하든 아니면 붙여넣든, 들여쓰기가 정확해야 합니다. 이 연습 뒷부분에서 파일의 일치 여부를 확인할 수 있도록 전체 YAML 파이프라인 정의가 표시됩니다.

파이프라인에 게시 스테이지를 추가합니다.

이제 템플릿 사양을 Azure에 게시하는 두 번째 스테이지를 추가할 수 있습니다.

  1. 다음 코드를 pipeline.yml 파일 끝에 추가합니다.

    - stage: Publish
      jobs:
      - job: Publish
        steps:
          - task: AzureCLI@2
            name: Publish
            displayName: Publish template spec
            inputs:
              azureSubscription: $(ServiceConnectionName)
              scriptType: 'bash'
              scriptLocation: 'inlineScript'
              inlineScript: |
                az ts create \
                  --resource-group $(AzureResourceGroupName) \
                  --name $(TemplateSpecName) \
                  --version $(Build.BuildNumber) \
                  --template-file $(TemplateSpecFilePath) \
                  --location $(AzureRegion) \
                  --yes
    

    이 스테이지에서는 리포지토리의 코드를 확인하고 앞에서 만든 서비스 연결을 사용하여 Azure에 로그인합니다. 그런 다음, az ts create 명령을 실행하여 템플릿 사양을 Azure에 게시합니다.

    간단한 설명을 위해 파이프라인에서 파이프라인의 빌드 번호를 템플릿 사양의 버전 번호로 사용합니다. 다음 단원에서는 좀 더 복잡한 버전 관리 체계에 대해 알아보겠습니다.

  2. 파일의 변경 내용을 저장합니다.

파이프라인 정의 확인 및 커밋

  1. pipeline.yml 파일이 다음 예제와 같이 표시되는지 확인합니다.

    trigger:
      batch: true
      branches:
        include:
        - main
      paths:
        include:
        - 'template-specs/linux-app-service/**'
    
    variables:
    - name: ServiceConnectionName
      value: ToyReusable
    - name: AzureResourceGroupName
      value: ToyReusable
    - name: AzureRegion
      value: westus3
    - name: TemplateSpecName
      value: linux-app-service
    - name: TemplateSpecFilePath
      value: template-specs/linux-app-service/main.bicep
    
    pool:
      vmImage: ubuntu-latest
    
    stages:
    
    - stage: Lint
      jobs:
      - job: LintCode
        displayName: Lint code
        steps:
          - script: |
              az bicep build --file $(TemplateSpecFilePath)
            name: LintBicepCode
            displayName: Run Bicep linter
    
    - stage: Publish
      jobs:
      - job: Publish
        steps:
          - task: AzureCLI@2
            name: Publish
            displayName: Publish template spec
            inputs:
              azureSubscription: $(ServiceConnectionName)
              scriptType: 'bash'
              scriptLocation: 'inlineScript'
              inlineScript: |
                az ts create \
                  --resource-group $(AzureResourceGroupName) \
                  --name $(TemplateSpecName) \
                  --version $(Build.BuildNumber) \
                  --template-file $(TemplateSpecFilePath) \
                  --location $(AzureRegion) \
                  --yes
    

    같지 않으면 다음 예제와 일치하도록 업데이트한 후 저장합니다.

  2. Visual Studio Code 터미널에서 다음 명령을 실행하여 변경 내용을 커밋하고 Git 리포지토리에 푸시합니다.

    git add .
    git commit -m "Add lint and publish stages to Linux App Service template spec pipeline"
    git push
    

    푸시하는 즉시 Azure Pipelines가 새 파이프라인 실행을 시작합니다.

파이프라인 모니터링

  1. 브라우저에서 파이프라인>파이프라인을 선택합니다.

    Screenshot of Azure Pipelines that shows the Pipelines menu item.

  2. 활성 파이프라인 실행을 선택합니다.

  3. 파이프라인 실행이 표시됩니다.

    파이프라인 실행이 완료될 때까지 기다립니다. 실행이 표시되면 템플릿 사양이 Azure에 게시된 것입니다.

    Screenshot of Azure Pipelines that shows a successful pipeline run and highlights the build number.

  4. 오늘 날짜와 고유한 수정 번호가 포함된 파이프라인의 빌드 번호를 확인합니다. 예제 스크린샷에서 빌드 번호는 20230407.1입니다.

Azure에서 템플릿 사양 검토

Azure Portal에서 게시된 템플릿 사양을 볼 수도 있습니다.

  1. 브라우저에서 Azure Portal로 이동합니다.

  2. ToyReusable 리소스 그룹으로 이동하여 linux-app-service 템플릿 사양을 선택합니다.

    Screenshot of the Azure portal that shows the resource group, with the template spec highlighted.

  3. 템플릿 사양의 세부 정보를 검사합니다.

    Screenshot of the Azure portal that shows the template spec details.

    최신 버전버전 번호가 파이프라인의 빌드 번호와 동일합니다. 파이프라인에서는 빌드 번호를 템플릿 사양의 버전 번호로 사용합니다.