Skip to content

Instantly share code, notes, and snippets.

@ahmedig
Last active October 28, 2020 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedig/f37c083d0977de0da96b3e5c0bd7f330 to your computer and use it in GitHub Desktop.
Save ahmedig/f37c083d0977de0da96b3e5c0bd7f330 to your computer and use it in GitHub Desktop.
Github workflow - Build, push to ACR and deploy to App Service
name: Push Docker Image to ACR
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: [ master ]
# paths:
# - "src/**"
pull_request:
branches: [ master ]
# paths:
# - "src/**"
workflow_dispatch:
inputs:
InvokedBy:
description: 'Name'
required: false
env:
CONTAINER_REGISTRY: ahmedigregistry.azurecr.io
image_name: appdemo
AZURE_WEBAPP_NAME: ahmedigwebapp
# defaults:
# run:
# working-directory: IaC
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, world!
- name: Azure Container Registry Login
uses: Azure/docker-login@v1
with:
# Container registry username
username: ${{ secrets.ACR_USERNAME }}
# Container registry password
password: ${{ secrets.ACR_PASSWORD }}
# Container registry server url
login-server: ${{env.CONTAINER_REGISTRY}}
- name: Push Container to ACR
working-directory: src
run: |
docker build . -t ${{env.CONTAINER_REGISTRY}}/${{env.image_name}}:${{ github.sha }}
docker push ${{env.CONTAINER_REGISTRY}}/${{env.image_name}}:${{ github.sha }}
DeployWebApp:
needs: build
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: Azure authentication
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
# - name: Azure CLI script to provision a new Web App for Container
# uses: azure/CLI@v0-beta
# with:
# azcliversion: latest
# inlineScript: |
# az webapp create --resource-group ${{ env.AZURE_RESOURCE_GROUP }} --plan ${{ env.AZURE_APP_PLAN }} --name ${{ env.AZURE_WEBAPP_NAME }} -i nginx
- name: Azure Container Registry Login
uses: Azure/docker-login@v1
with:
# Container registry username
username: ${{ secrets.ACR_USERNAME }}
# Container registry password
password: ${{ secrets.ACR_PASSWORD }}
# Container registry server url
login-server: ${{env.CONTAINER_REGISTRY}}
- name: Set Web App ACR authentication
uses: Azure/appservice-settings@v1
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
app-settings-json: |
[
{
"name": "DOCKER_REGISTRY_SERVER_PASSWORD",
"value": "${{ secrets.ACR_PASSWORD }}",
"slotSetting": false
},
{
"name": "DOCKER_REGISTRY_SERVER_URL",
"value": "https://${{env.CONTAINER_REGISTRY}}",
"slotSetting": false
},
{
"name": "DOCKER_REGISTRY_SERVER_USERNAME",
"value": "${{ secrets.ACR_USERNAME }}",
"slotSetting": false
}
]
- name: 'Deploy to Azure Web App for Container'
uses: azure/webapps-deploy@v2
with:
app-name: ${{ env.AZURE_WEBAPP_NAME }}
images: ${{ env.CONTAINER_REGISTRY }}/${{env.image_name}}:${{ github.sha }}
@ahmedig
Copy link
Author

ahmedig commented Oct 9, 2020

Add in the folder: .github/workflows/

@ahmedig
Copy link
Author

ahmedig commented Oct 11, 2020

Run this and paste the output in a secret named "AZURE_CREDENTIALS"

az ad sp create-for-rbac --name "GitHub-Actions" --role contributor \
                        --scopes /subscriptions/XXXXX-XXXXX-XXXXX-XXXXX \
                        --sdk-auth

@ahmedig
Copy link
Author

ahmedig commented Oct 22, 2020

name: Push Docker Image to ACR

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]
#     paths:
#       - "src/**"
  pull_request:
    branches: [ master ]
#     paths:
#       - "src/**"
  workflow_dispatch:
    inputs:
      InvokedBy:
        description: 'Name'
        required: false

env:
  CONTAINER_REGISTRY: ahmedigregistry.azurecr.io
  image_name: azurediyimage
  AZURE_WEBAPP_NAME: ahmedigwebapp
  
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: Azure Container Registry Login
        uses: Azure/docker-login@v1
        with:
          # Container registry username
          username: ${{ secrets.ACR_USERNAME }}
          # Container registry password
          password: ${{ secrets.ACR_PASSWORD }}
          # Container registry server url
          login-server: ${{env.CONTAINER_REGISTRY}}
          
      - name: Push Container to ACR
        working-directory: azurediy
        run: |
          docker build . -t ${{env.CONTAINER_REGISTRY}}/${{env.image_name}}:${{ github.sha }}
          docker push ${{env.CONTAINER_REGISTRY}}/${{env.image_name}}:${{ github.sha }}
  
  DeployWebApp:
    needs: build
    runs-on: ubuntu-latest
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: Azure authentication
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}
      
      - name: Set Web App ACR authentication
        uses: Azure/appservice-settings@v1
        with:
          app-name: ${{ env.AZURE_WEBAPP_NAME }}
          app-settings-json: |
            [
                {
                    "name": "DOCKER_REGISTRY_SERVER_PASSWORD",
                    "value": "${{ secrets.ACR_PASSWORD }}",
                    "slotSetting": false
                },
                {
                    "name": "DOCKER_REGISTRY_SERVER_URL",
                    "value": "https://${{env.CONTAINER_REGISTRY}}",
                    "slotSetting": false
                },
                {
                    "name": "DOCKER_REGISTRY_SERVER_USERNAME",
                    "value": "${{ secrets.ACR_USERNAME  }}",
                    "slotSetting": false
                }
            ]
          
      - name: 'Deploy to Azure Web App for Container'
        uses: azure/webapps-deploy@v2
        with: 
          app-name: ${{ env.AZURE_WEBAPP_NAME }} 
          images: ${{ env.CONTAINER_REGISTRY }}/${{env.image_name}}:${{ github.sha }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment