Connect to a GitHub repository and create Cloud Build repository
检查我的进度
/ 20
Create the Cloud Build trigger
检查我的进度
/ 20
Test the pipeline
检查我的进度
/ 20
Quick tip: Review the prerequisites before you run the lab
Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.
如下圖所示,該執行個體的最後一欄會顯示外部 IP 位址和「SSH」按鈕。如果資訊面板擋到內容,可以關閉面板。
請記住外部 IP 位址,稍後會用到。
點選「SSH」,畫面會顯示殼層提示視窗。
在本機安裝 Hugo
現在要將 Hugo 安裝至 Linux 執行個體,以在本機測試網站,接著透過 Firebase 部署網站。為了方便操作,系統提供殼層指令碼。
在 Linux 執行個體殼層,查看 installhugo.sh 檔案。
cat /tmp/installhugo.sh
系統也會顯示下列內容:
輸出內容:
#!/bin/bash
# Copyright 2020 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
_HUGO_VERSION=0.96.0
echo Downloading Hugo version $_HUGO_VERSION...
wget \
--quiet \
-O hugo.tar.gz \
https://github.com/gohugoio/hugo/releases/download/v${_HUGO_VERSION}/hugo_extended_${_HUGO_VERSION}_Linux-64bit.tar.gz
echo Extracting Hugo files into /tmp...
mv hugo.tar.gz /tmp
tar -C /tmp -xzf /tmp/hugo.tar.gz
echo The Hugo binary is now at /tmp/hugo.
請留意下列部分:使用 wget 指令下載 Hugo,以及使用 tar 指令解開 Hugo 封存檔。稍後在本研究室建立管道時,您會看到類似的指令。
輸入下列指令來執行指令碼並安裝 Hugo:
cd ~
/tmp/installhugo.sh
系統會顯示下列訊息,表示 Hugo 已安裝至 /tmp 目錄。現在可以開始建立網站基礎架構。
建立存放區與初始網站
現在要建立存放網站的 Cloud 原始碼存放區,接著將存放區複製到 Linux 執行個體。複製存放區會在殼層建立該存放區的鏡像,這樣就能在殼層導入網站,然後將變更提交至檔案系統。稍後您會在本研究室設定管道,回應存放區的修訂內容。
cd ~/my_hugo_site
/tmp/hugo server -D --bind 0.0.0.0 --port 8080
如下圖所示,Hugo 會建構並提供網站,讓使用者在 TCP 通訊埠 8080 存取網站。伺服器會持續執行,直到您按下 CTRL + C 來停止作業。
開啟瀏覽器分頁,前往通訊埠 8080 的外部 IP 位址。使用下列網址,將 [EXTERNAL IP] 換成執行個體的外部 IP 位址:
http://[EXTERNAL IP]:8080
網站應如下所示:
點選「Check my progress」,確認目標已達成。
可在通訊埠 8080 存取網站
返回 Linux 殼層,按下 CTRL + C 鍵來停止 Hugo 伺服器。
將網站部署至 Firebase
在 Linux 執行個體殼層,安裝 Firebase CLI:
curl -sL https://firebase.tools | bash
現在需要將 Firebase 初始化。在殼層輸入下列指令:
cd ~/my_hugo_site
firebase init
使用方向鍵和空白鍵選取「Hosting: Configure files for Firebase Hosting and (optionally) set up GitHub Action deploys」,接著按下 Enter 鍵。系統詢問專案選項時,請選取「Use an existing project」,接著使用方向鍵、空白鍵和 Enter 鍵選取研究室說明頁面的專案 ID。在公開目錄部分,選取預設值「public」。在設為單頁應用程式形式部分,選取預設值「N」。在透過 GitHub 設定自動建構及部署部分,選取「N」。
如果系統詢問是否覆寫任何現有檔案,選取「Y」。
現在可以開始部署應用程式。在 Linux 執行個體殼層輸入下列指令,使用 Hugo 重新建構網站並透過 Firebase 部署網站:
# Copyright 2020 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
steps:
- name: 'gcr.io/cloud-builders/curl'
args:
- '--quiet'
- '-O'
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
steps:
- name: 'gcr.io/cloud-builders/curl'
args:
- '--quiet'
- '-O'
- 'firebase'
- 'https://firebase.tools/bin/linux/latest'
- name: 'gcr.io/cloud-builders/curl'
args:
- '--quiet'
- '-O'
- 'hugo.tar.gz'
- 'https://github.com/gohugoio/hugo/releases/download/v${_HUGO_VERSION}/hugo_extended_${_HUGO_VERSION}_Linux-64bit.tar.gz'
waitFor: ['-']
- name: 'ubuntu:20.04'
args:
- 'bash'
- '-c'
- |
mv hugo.tar.gz /tmp
tar -C /tmp -xzf /tmp/hugo.tar.gz
mv firebase /tmp
chmod 755 /tmp/firebase
/tmp/hugo
/tmp/firebase deploy --project ${PROJECT_ID} --non-interactive --only hosting -m "Build ${BUILD_ID}"
substitutions:
_HUGO_VERSION: 0.96.0
options:
defaultLogsBucketBehavior: REGIONAL_USER_OWNED_BUCKET
您會發現 cloudbuild.yaml 檔案的情況如下:
這個檔案中有三個具名步驟,每個步驟都由容器映像檔執行。前兩個步驟採用 Google 支援的建構工具,使用 curl 下載 Hugo 和 Firebase 工具。這兩個步驟會同時進行。使用 curl 建構工具比手動安裝 curl 更快。
第三個步驟會使用標準 Ubuntu 容器來安裝 Hugo 和 Firebase,然後建立並部署網站。為每項部署作業安裝 Hugo 和 Firebase,就能隨時變更 Hugo 版本,並使用最新版 Firebase。
tar 和 wget 指令與先前 installhugo.sh 指令碼中的指令雷同。
這個檔案也使用自訂替代變數 (_HUGO_VERSION) 和 Google 提供的替代變數 (PROJECT_ID),讓這個範本在不同環境都能使用。
Labs create a Google Cloud project and resources for a fixed time
Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
On the top left of your screen, click Start lab to begin
Use private browsing
Copy the provided Username and Password for the lab
Click Open console in private mode
Sign in to the Console
Sign in using your lab credentials. Using other credentials might cause errors or incur charges.
Accept the terms, and skip the recovery resource page
Don't click End lab unless you've finished the lab or want to restart it, as it will clear your work and remove the project
此内容目前不可用
一旦可用,我们会通过电子邮件告知您
太好了!
一旦可用,我们会通过电子邮件告知您
One lab at a time
Confirm to end all existing labs and start this one
Use private browsing to run the lab
Use an Incognito or private browser window to run this lab. This
prevents any conflicts between your personal account and the Student
account, which may cause extra charges incurred to your personal account.
在本實驗室中,您將學習如何使用 Cloud Build 和 Firebase 部署靜態 Hugo 網站。