Skip to content

Instantly share code, notes, and snippets.

@igoravl
Created April 29, 2020 19:55
Show Gist options
  • Save igoravl/72f53d2f1d5f153f8b73f1e3ad094320 to your computer and use it in GitHub Desktop.
Save igoravl/72f53d2f1d5f153f8b73f1e3ad094320 to your computer and use it in GitHub Desktop.

Revisions

  1. igoravl created this gist Apr 29, 2020.
    62 changes: 62 additions & 0 deletions azure-pipelines.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    trigger:
    - master

    resources:
    - repo: self

    variables:
    azureSubscription: '<azure-subscription>'
    dockerRegistryServiceConnection: '<service-connection>'
    imageRepository: '<repository-name>'
    containerRegistry: '<registry>.azurecr.io'
    dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
    tag: '$(Build.BuildId)'
    vmImageName: 'ubuntu-latest'

    stages:
    - stage: Build
    displayName: Build and push stage
    jobs:
    - job: Build
    displayName: Build
    pool:
    vmImage: $(vmImageName)
    steps:
    - task: AzureCLI@2
    name:
    displayName: 'Add agent IP to firewall whitelist'
    inputs:
    azureSubscription: $(azureSubscription)
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
    AGENT_IP="$(dig +short myip.opendns.com @resolver1.opendns.com)"
    if [ -z "$(az acr network-rule list --name $(containerRegistry) | grep ${AGENT_IP})"]
    then
    echo "Adding agent IP '${AGENT_IP}' to Azure Container Registry '$(containerRegistry)' firewall whitelist"
    az acr network-rule add --name $(containerRegistry) --ip-address $AGENT_IP
    else
    echo "Agent is already whitelisted; skipping."
    fi
    - task: Docker@2
    displayName: Build and push an image to container registry
    inputs:
    command: buildAndPush
    repository: $(imageRepository)
    dockerfile: $(dockerfilePath)
    containerRegistry: $(dockerRegistryServiceConnection)
    tags: |
    $(tag)
    - task: AzureCLI@2
    displayName: 'Remove agent IP from firewall whitelist'
    condition: always()
    inputs:
    azureSubscription: $(azureSubscription)
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
    AGENT_IP="$(dig +short myip.opendns.com @resolver1.opendns.com)"
    echo "Removing agent IP '${AGENT_IP}' from Azure Container Registry '$(containerRegistry)' firewall whitelist"
    az acr network-rule remove --name $(containerRegistry) --ip-address $AGENT_IP --only-show-errors --output none