Deploy code ไป App Service ด้วย Local Git repository

  1. กำหนดวิธี Deploy ของ App Service ให้เป็น Local Git
  2. สร้างโปรเจ็กส์ Web Application
  3. Deploy ด้วย git command

1.กำหนดวิธี Deploy ของ App Service ให้เป็น Local Git

ที่ Portal เลือกเมนู App Services แล้วเลือก App Services อันที่ใช้งาน (jack2019)

เลือกเมนู Deployment Center

เลือกไอคอน Local Git แล้วกด Continue

เลือก App Service build service

เลือกเมนู Deployment Credentials

url ที่ใช้กำหนดให้ local git

กำหนด Username และ Password

2. สร้างโปรเจ็กส์ Web Application

Note: ทดลองบน Cloud Shell

สร้างโปรเจ็กส์ Web Application ชื่อ WebApp1

phaisarn@Azure:~$ dotnet new webapp -o WebApp1

Welcome to .NET Core!
---------------------
Learn more about .NET Core: https://aka.ms/dotnet-docs
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli-docs

Telemetry
---------
The .NET Core tools collect usage data in order to help us improve your experience. The data is anonymous and doesn't include command-line arguments.The data is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environmentvariable to '1' or 'true' using your favorite shell.

Read more about .NET Core CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

ASP.NET Core
------------
Successfully installed the ASP.NET Core HTTPS Development Certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only). For establishing trust on other platforms refer to the platform specific documentation.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
Getting ready...
The template "ASP.NET Core Web App" was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/aspnetcore-template-3pn-210 for details.

Processing post-creation actions...
Running 'dotnet restore' on WebApp1/WebApp1.csproj...
  Restore completed in 4.83 sec for /home/phaisarn/WebApp1/WebApp1.csproj.

Restore succeeded.

ทดลองรัน

phaisarn@Azure:~$ cd WebApp1/
phaisarn@Azure:~/WebApp1$ dotnet run
: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using '/home/phaisarn/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[58]
      Creating key {b320aceb-500b-4a99-a928-d60c63de11b5} with creation date 2019-05-11 06:56:10Z, activation date 2019-05-11 06:56:10Z, and expiration date 2019-08-09 06:56:10Z.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {b320aceb-500b-4a99-a928-d60c63de11b5} may be persisted to storage in unencrypted form.
info: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[39]
      Writing data to file '/home/phaisarn/.aspnet/DataProtection-Keys/key-b320aceb-500b-4a99-a928-d60c63de11b5.xml'.
Hosting environment: Development
Content root path: /home/phaisarn/WebApp1
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

ขึ้นแบบนี้ก็รันได้ปกติ ออกโดยกด Ctrl+c

3. Deploy ด้วย git command

Set up Git

กำหนด user และ email โดย user ได้มากจากการกำหนด Deployment Credentials ในขั้นตอนที่ 1

phaisarn@Azure:~$ git config --global user.name "[your name]"
phaisarn@Azure:~$ git config --global user.email "[your email]"

ตรวจสอบข้อมูลในไฟล์คอนฟิก

phaisarn@Azure:~$ cat .gitconfig
[user]
        name = jack
        email = phaisarn@outlook.com

Initialize a local Git repository

phaisarn@Azure:~/WebApp1$ git init
Initialized empty Git repository in /home/phaisarn/WebApp1/.git/

Stage ไฟล์ทั้งหมดเข้า Git

phaisarn@Azure:~/WebApp1$ git add .

commit Git

phaisarn@Azure:~/WebApp1$ git commit -m "Initial commit"
[master (root-commit) 159e914] Initial commit
 91 files changed, 58475 insertions(+)
.......

เพิ่ม remote

phaisarn@Azure:~/WebApp1$ git remote add origin https://[your-username]@jack2019.scm.azurewebsites.net:443/jack2019.git

Note: url ได้มาจากขั้นตอนที่ 1 ตอนสร้าง Local Git

ตรวจสอบ

phaisarn@Azure:~/WebApp1$ git remote -v
origin  https://[your-username]@[your-app-name].scm.azurewebsites.net:443/[your-app-name].git (fetch)
origin  https://[your-username]@[your-app-name].scm.azurewebsites.net:443/[your-app-name].git (push)

Push code ขึ้น Azure

phaisarn@Azure:~/WebApp1$ git push origin master
Password for 'https://[your-username]@jack2019.scm.azurewebsites.net:443':
Counting objects: 113, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (99/99), done.
Writing objects: 100% (113/113), 806.54 KiB | 0 bytes/s, done.
Total 113 (delta 24), reused 0 (delta 0)
......
remote: Deployment successful.

เสร็จแล้วจะเปิด browser ไปที่ http://jack2019.azurewebsites.net/ ก็จะเห็นเว็บของเราละ

ถ้ากลับไปดูที่ Deployment Center ก็จะเห็นรายการที่เรา commit ละ