バゼル1.0であなたのServerlessなAzure機能アプリを構築してください🌿


バゼルは、ちょうど新しいマイルストーンを打ちました:バゼル1.0は、今日発表されました.この分散ビルドとテストオーケストレーションツールに投資を開始する時間です.


バゼルビル
@バゼルビル

1.0ケーキ!
午後20時03分- 2019年10月10日
このクイックガイドでは、我々は、ビルドするためにバゼルを使用するつもりですAzure Function アプリケーションスクリプト.

Bazel is polyglot, meaning you can use it to build and test different technologies, even in the same monorepo, at the same! Today we are going to focus on TypeScript.


もしあなたがWebアプリケーションの構築に関してBAZELの機能についてもっと知りたいならば、私はGoogleのJavaScriptビルド/サーブチームから私の友人アレックスイーグルによって以下のポストを読むことを強く勧めます.


以下に続く手順を示します.
  • Scaffold an Azure Function Application
  • Set up a Bazel workspace
  • Build the Azure Function Application using Bazel
  • Deploy to Azure
  • If needed, I included a link to a free Azure trial, so you can try it yourself.


    始めましょう.

    UPDATE Oct. 20th: Hexa now has built-in support for Bazel! Liquid error: internal


    足場機能アプリケーション

    In order to easily create and then deploy our Azure Function アプリケーション、我々は使用するつもりですHexa CLI :
    $ mkdir azurefunctionbazel && cd $_
    $ hexa init --just functions
    $ cd functions/azurefunctionbazel-5816
    

    Hexa is an open-source CLI tool that enhances the Azure CLI (learn more about Hexa and how to install it).


    使用Hexa 我々は、内部に足場を広げた./functions/azurefunctionbazel-5816 新しいタイプスクリプトAzure Function 使用するアプリケーションtsc (デフォルトでビルドする).
    Bazelを設定し、それを使ってビルドを調整しますts_library ルール(すなわち、バゼルプラグイン).

    バゼルワークスペースを設定する

    In order to set up and use Bazel inside our Azure Function project ./functions/azurefunctionbazel-5816 , we will need:

    1. Install Bazel and its dependencies from NPM
    2. Add a WORKSPACE file at the root of the project
    3. Add a BUILD.bazel file at the root of the project
    4. Add one BUILD.bazel inside each function

    Usually you wouldn't have to set up Bazel manually, but rather use a tool such as @bazel/create to automate the setup and config.

    Bazelのインストールと依存関係


    インサイド./functions/azurefunctionbazel-5816 フォルダは、次のコマンドを使用してNPMから必要なBazelとそのピア依存関係をインストールしますpackage.json ):
    $ npm install -D \
          @bazel/bazel@latest \
          @bazel/typescript@latest \
          typescript@^3.3.3
    

    Make sure typescript is listed and installed as a dependency in the package.json


    ワークスペースファイルを追加する


    ファイルを作るWORKSPACE 根の./functions/azurefunctionbazel-5816 次のコンテンツを参照してください(後述の説明を参照).
    workspace(
        name = "azurefunctionbazel",
        managed_directories = {"@npm": ["node_modules"]},
    )
    
    load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
    http_archive(
        name = "build_bazel_rules_nodejs",
        sha256 = "1447312c8570e8916da0f5f415186e7098cdd4ce48e04b8e864f793c766959c3",
        urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.38.2/rules_nodejs-0.38.2.tar.gz"],
    )
    
    load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")
    npm_install(
        name = "npm",
        package_json = "//:package.json",
        package_lock_json = "//:package-lock.json",
    )
    
    load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")
    install_bazel_dependencies()
    
    load("@npm_bazel_typescript//:index.bzl", "ts_setup_workspace")
    ts_setup_workspace()
    
    
    各章を説明しましょう.
    workspace(
        name = "azurefunctionbazel",
        managed_directories = {"@npm": ["node_modules"]},
    )
    
  • The workspace ルールは、このフォルダがバゼルのルートであることを宣言しますworkspace .
  • The name 属性は、他のワークスペースから絶対的なラベルでこのワークスペースを参照する方法を宣言します.@azurefunctionbazel//
  • the managed_directories 属性マップ@npm バゼルワークスペースnode_modules ディレクトリ.これにより、バゼルは同じnode_modules 別のローカルツーリングとして.
  • 
    load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
    http_archive(
        name = "build_bazel_rules_nodejs",
        sha256 = "1447312c8570e8916da0f5f415186e7098cdd4ce48e04b8e864f793c766959c3",
        urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.38.2/rules_nodejs-0.38.2.tar.gz"],
    )
    
    これでノードがインストールされます.JPの“ブートストラップ”パッケージを実行し、パッケージのノードの基本的なツールを提供します.BazelのJSアプリ.
    load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")
    npm_install(
        name = "npm",
        package_json = "//:package.json",
        package_lock_json = "//:package-lock.json",
    )
    
  • The npm_install ルール実行npm (or yarn あなたの設定に応じていつでもpackage.json or package-lock.json ファイルの変更.また、任意のバゼル規則をnpm パッケージ.
  • The name 属性は、Bazelのラベル参照がどのように見えるかです@npm//@azure/functions
  • load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")
    install_bazel_dependencies()
    
    このコールは、npm_install ルール.
    load("@npm_bazel_typescript//:index.bzl", "ts_setup_workspace")
    ts_setup_workspace()
    
    これはあなたのワークスペースのためのtypescript toolchainを設定します.

    ルートビルド。バゼル


    ファイルを作るBUILD.bazel 根の./functions/azurefunctionbazel-5816 次のコンテンツを使用します.
    exports_files(["tsconfig.json"], visibility = ["//:__subpackages__"])
    
    この単純なBUILD.bazeltsconfig.json したがって、他のパッケージから参照することができます.

    パッケージビルド。バゼル


    Bazelは、より効率的なビルド実行のために細粒度パッケージを作成することを推奨します.Bazel用語のパッケージはBUILD.bazel ファイル../functions/azurefunctionbazel-5816/httpTrigger/BUILD.bazel .
    我々の場合では、Azure機能アプリケーションは、各々の機能のために1つのフォルダを含みます.これらのフォルダは私たちのバゼルパッケージになります.
    インサイドBUILD.bazel 私たちは、そのパッケージ(すなわちAzure機能)がどのように構築されるべきかについて説明するつもりです、そして、Bazelにオーケストレーションの世話をさせてください.
    我々のアプリケーションを構築するには、我々はそれをシンプルにして、単にtsc コンパイラ.しかし、より複雑なシナリオについては、我々はまた、我々のアプリケーションをバンドルすることができますrollup を使用して、terser ツールこのような仮定的なビルドプロセスは次のようになります.
    # Note: this build hasn't been tested!
    
    load("@npm_bazel_typescript//:index.bzl", "ts_library")
    load("@npm_bazel_rollup//:index.bzl", "rollup_bundle")
    load("@npm_bazel_terser//:index.bzl", "terser_minified")
    
    package(default_visibility = ["//visibility:public"])
    
    ts_library(
        name = "httpTrigger",
        srcs = ["index.ts"],
        deps = ["@npm//@azure/functions"]
    )
    
    rollup_bundle(
        name = "bundle",
        entry_point = ":index.ts",
        deps = [":httpTrigger"],
    )
    
    terser_minified(
        name = "bundle.min",
        src = ":bundle",
    )
    
    しかし、我々のケースでは、我々はそれを簡単にします
    load("@npm_bazel_typescript//:index.bzl", "ts_library")
    
    package(default_visibility = ["//visibility:public"])
    
    ts_library(
        name = "httpTrigger",
        srcs = ["index.ts"],
        deps = ["@npm//@azure/functions"]
    )
    
  • The name 属性はターゲットの名前ですts_library コール
  • The srcs 属性は、すべてのtypescriptファイルを
  • The deps 属性は、このパッケージ内で使用されるすべての外部依存関係を示します.@azure/functions ). The @npm// NPMワークスペースの名前ですWORKSPACE 上記).
  • 細粒度パッケージを使用すると、Bazelからの増加を得るために、リモートの並列化可能なビルドとキャッシュが自動的にビルド時間を大幅に改善されます.

    Azure関数アプリケーションの構築

    Now, we can run Bazel to build this specific package:

    $ bazel build @azurefunctionbazel//httpTrigger:httpTrigger
    

    The canonical path to a target is explained as the following:


    実際には、このパスを短縮することができます.
    $ bazel build //httpTrigger
    
    これは、同じワークスペースから指定したターゲットを構築しているためです.それで、我々はワークスペース識別子を省略することができます、そして、パッケージと目標名が同一であるので、我々は目標名を省略することができます.
    また、現在のワークスペース内ですべてのパッケージのすべてのターゲットをビルドするようにBazelを指示することもできます.
    $ bazel build //...
    
    つの単一の些細な機能を構築するためにBazelを使用しての利点を見ることができないかもしれませんが、一度あなたが無制限のアプリケーションの100 sを扱う開始すると、Bazelはあなたのビルドとテストパフォーマンスを高めるためにそこになります.
    バゼルも我々のアプリケーションの依存関係のグラフを理解するのに適しています.この依存関係グラフを表示するには、Bazelに問い合わせてください.
    $ bazel query --noimplicit_deps 'deps(//...)' --output graph | dot -Tpng > azurefunctionbazel.png
    
    出力

    We could use this visualization to better understand the internals of our application. Image an application with 100s of Function Apps!


    アジュールへの配備

    By default, the output of the ts_library() ( *.js and *.d.ts ) rule will be stored in ./functions/azurefunctionbazel-5816/bazel-bin/httpTrigger .

    Before deploying to Azure, we need to update the function.json of each function and point the scriptFile to bazel-bin/ :

    {
      ...
      "scriptFile": "../bazel-bin/httpTrigger/index.js"
    }
    

    We also need to make sure not to deploy the Bazel workspace to Azure, by updating the .funcignore file:

    ## Bazel ignored files
    node_modules/@bazel/*
    BUILD.bazel
    
    # ignore all bazel output folder except the bazel-bin folder 
    # where the transpiled code lives
    bazel-*
    !bazel-bin
    
    Now we are ready to deploy our application to Azure, using the Hexa CLI :
    $ # cd to the root of azurefunctionbazel where hexa.json is located
    $ hexa deploy
    
    展開コマンドの出力結果は次のようになります.
    
    ✔ Building Function app azurefunctionbazel-5816...
    ✔ Deploying Function app azurefunctionbazel-5816...
    ✔ Application azurefunctionbazel deployed successfully!
    
    ➜ Functions:
     - httptrigger: https://azurefunctionbazel-5816.azurewebsites.net/api/httptrigger?code=POfqT9xFCtYKOaryFW8LkzFsUtD3ioWi8Y2e4M8BpBeLhTOH8aqUIg==
    ✔ Done in 68 seconds.
    
    祝辞🎉! あなたのAzure関数のアプリケーションは、現在Bazelを使用して構築されていると展開Hexa .
    Githubのサンプルプロジェクトを見つけてください

    実技家 / アズクレンファン



    ビルドAzure機能アプリバゼル1.0で


    View on GitHub