Google Cloudbuild yaml file 中 entrypoint 和 args 的寫法

編寫cloudbuild.yaml 時有幾個關鍵參數

entrypoint 和 args 的基本介紹

id: 顯示在 cloud build logs 里的item 名字
name: docker 鏡像名字 - 下面的命令會在這個鏡像的1個容器instance 內執行
entrypoint: 執行的命令入口 , 只能有1個對象
args: 命名的參數, 它是1個list

問題來了, 如何理解深而慢是entrypoint 和 args

entrypoint 就是執行的命令 bin file 名字, args 是參數, 不能混淆

例如:

cat /tmp/1.txt /tmp/2.txt


cat 就entrypoint
/tmp/1.txt /tmp/2.txt 就是兩個參數, 因為args 是1個list

又如:

echo abc def

中, echo 是 entrypoint, args 是 [abc, def]

所以在clouldbuild.yaml 中下面command 1是正確的, command 2 是錯誤的

steps:# correct- id: test command 1name: 'gcr.io/cloud-builders/gcloud'entrypoint: echoargs: [abc, def]# wrong- id: test command 2name: 'gcr.io/cloud-builders/gcloud'entrypoint: echo abcargs: [ def ]logsBucket: gs://jason-hsbc_cloudbuild/logs/
options: # https://cloud.google.com/cloud-build/docs/build-config#optionslogging: GCS_ONLY # or CLOUD_LOGGING_ONLY https://cloud.google.com/cloud-build/docs/build-config#logging

因為第2中寫法, 它把 echo abc 作為endpoint, 雖然合并字符串 也是 echo abc def, 跟 command 1 的寫法一樣, 但是yaml 中命令的寫法絕對不是字符串

而鏡像中的 /usr/bin 中絕對不可能有1個 echo abc 包括空格的file, 所以會出錯
日志

starting build "34e39f7f-e039-4572-a392-21a154ed8228"FETCHSOURCE
Fetching storage object: gs://jason-hsbc_cloudbuild/source/1717185531.688094-1bcec1bbcfb64618b377c2a5e097c551.tgz#1717185513807965
Copying gs://jason-hsbc_cloudbuild/source/1717185531.688094-1bcec1bbcfb64618b377c2a5e097c551.tgz#1717185513807965...
/ [0 files][    0.0 B/ 73.9 KiB]                                                
-
- [1 files][ 73.9 KiB/ 73.9 KiB]                                                
Operation completed over 1 objects/73.9 KiB.                                     
tar: cloudbuild-test.yaml: time stamp 2024-05-31 19:58:47.9453259 is 4.465130295 s in the future
BUILD
Starting Step #0 - "test command 1"
Step #0 - "test command 1": Already have image (with digest): gcr.io/cloud-builders/gcloud
Step #0 - "test command 1": abc def
Finished Step #0 - "test command 1"
Starting Step #1 - "test command 2"
Step #1 - "test command 2": Already have image (with digest): gcr.io/cloud-builders/gcloud
Finished Step #1 - "test command 2"
ERROR
ERROR: build step 1 "gcr.io/cloud-builders/gcloud" failed: starting step container failed: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "echo abc": executable file not found in $PATH: unknown




使用 bash - c 命令

如果我們想執行 cat 命令 則需要把entrypoint set 成 cat, 不是echo
而其實我們可以用bash entrypoint 統一起來

我們先看下 -c 作用

    -c        If  the -c option is present, then commands are read from the first non-option argument command_string.  If there are arguments after the command_string, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters.  The assignment to $0 sets the name of the shell, which is used in warning and error messages.

不是那么好懂, 舉個例子:

[gateman@manjaro-x13 mkinitcpio.d]$ bash -c 'echo $1 $0' abc def
def abc

bash -c 后面第1個參數 就是要執行的命令, 但是這個參數要用單引號(不是雙引號) 來包住, 第2個參數開始, 都是第1個參數(被執行命令)

注意, 單引號不要寫成雙引號, 否則, 參數可能獲取失敗, $0 會被賦予 shell name

[gateman@manjaro-x13 mkinitcpio.d]$ bash -c "echo $1 $0" abc def
/bin/bash

總之, bash -c 只會執行1條命令, 第2個參數開始都是第1個參數的子參數

如果想一次執行兩條命令, 下面是錯誤示范

[gateman@manjaro-x13 mkinitcpio.d]$ bash -c 'echo abc' 'echo def'
abc

因為它把 ‘echo def’ 作為 ‘echo abc’ 的參數, 并沒有被執行

正確寫法:

[gateman@manjaro-x13 demo_cloud_user]$ bash -c 'echo abc; echo def'
abc
def

對于cloudbuild 來講, 我們也可以用bash -c 的寫法來編寫 entrypoint 和 args

steps:# correct- id: test command 1name: 'gcr.io/cloud-builders/gcloud'entrypoint: echoargs: [abc, def]# correct- id: test command 2name: 'gcr.io/cloud-builders/gcloud'entrypoint: bashargs: [ -c, echo abc def ]# incorrect nothing output- id: test command 3name: 'gcr.io/cloud-builders/gcloud'entrypoint: bashargs: [ -c, echo , abc def ]# correct- id: test command 4name: 'gcr.io/cloud-builders/gcloud'entrypoint: bashargs: [ -c, echo $0 , abc def ]# correct- id: test command 5name: 'gcr.io/cloud-builders/gcloud'entrypoint: bashargs: [ -c, echo $0 $1 , abc, def ]logsBucket: gs://jason-hsbc_cloudbuild/logs/
options: # https://cloud.google.com/cloud-build/docs/build-config#optionslogging: GCS_ONLY # or CLOUD_LOGGING_ONLY https://cloud.google.com/cloud-build/docs/build-config#logging

注意第3種寫法是錯誤的, abc def 作為 ‘echo’ 的參數毫無效果
輸出:

starting build "e234fd52-bfe2-4c5a-91c0-6980ef6db448"FETCHSOURCE
Fetching storage object: gs://jason-hsbc_cloudbuild/source/1717245688.299578-00e798950592461f9661290baa21addd.tgz#1717245670180704
Copying gs://jason-hsbc_cloudbuild/source/1717245688.299578-00e798950592461f9661290baa21addd.tgz#1717245670180704...
/ [0 files][    0.0 B/ 73.9 KiB]                                                
-
- [1 files][ 73.9 KiB/ 73.9 KiB]                                                
Operation completed over 1 objects/73.9 KiB.                                     
tar: cloudbuild-test.yaml: time stamp 2024-06-01 12:41:24.1218889 is 4.953891927 s in the future
BUILD
Starting Step #0 - "test command 1"
Step #0 - "test command 1": Already have image (with digest): gcr.io/cloud-builders/gcloud
Step #0 - "test command 1": abc def
Finished Step #0 - "test command 1"
Starting Step #1 - "test command 2"
Step #1 - "test command 2": Already have image (with digest): gcr.io/cloud-builders/gcloud
Step #1 - "test command 2": abc def
Finished Step #1 - "test command 2"
Starting Step #2 - "test command 3"
Step #2 - "test command 3": Already have image (with digest): gcr.io/cloud-builders/gcloud
Step #2 - "test command 3": 
Finished Step #2 - "test command 3"
Starting Step #3 - "test command 4"
Step #3 - "test command 4": Already have image (with digest): gcr.io/cloud-builders/gcloud
Step #3 - "test command 4": abc def
Finished Step #3 - "test command 4"
Starting Step #4 - "test command 5"
Step #4 - "test command 5": Already have image (with digest): gcr.io/cloud-builders/gcloud
Step #4 - "test command 5": abc def
Finished Step #4 - "test command 5"
PUSH
DONE




對于args 使用另1種的數組寫法

眾所周知, 在yaml 中, 數組有兩種表示方式

1是 中括號模式
例如:

args: [abc, def]args:- abc- def

上面那種寫法是正確的
對于本文里例子, couldbuild.yaml 命令也可以寫成

  # correct- id: test command 1name: 'gcr.io/cloud-builders/gcloud'entrypoint: echoargs: [abc, def]# correct- id: test command 2name: 'gcr.io/cloud-builders/gcloud'entrypoint: bashargs: [ -c, echo abc def ]# correct- id: test command 3name: 'gcr.io/cloud-builders/gcloud'entrypoint: bashargs:- -c- echo abc def

上面3種寫法都是等價的




對于多條命令的另1種寫法

例如我想連續執行兩條命令
echo abc 和 head /etc/proc/cpuinfo

這時entrypoint 就不能是 echo 和 head, 只能是bash

寫法1:

  - id: test command 1name: 'gcr.io/cloud-builders/gcloud'entrypoint: bashargs:- -c- echo abc; head /proc/cpuinfo

主要用分號隔開, 如果真的想寫成兩行

則寫法2, 用| 表示 ,則兩行之間不需要寫 ; 但是他們其實加起來還是args 的1個參數, 并不是兩個

  # correct- id: test command 2name: 'gcr.io/cloud-builders/gcloud'entrypoint: bashargs:- -c- |echo abchead /proc/cpuinfo

輸出是一樣的

starting build "5733316f-83e9-4566-98fd-f33fca535eda"FETCHSOURCE
Fetching storage object: gs://jason-hsbc_cloudbuild/source/1717249155.147565-a60f825e1e024b9eb1fc4529b01cf417.tgz#1717249137259618
Copying gs://jason-hsbc_cloudbuild/source/1717249155.147565-a60f825e1e024b9eb1fc4529b01cf417.tgz#1717249137259618...
/ [0 files][    0.0 B/ 73.9 KiB]                                                
-
- [1 files][ 73.9 KiB/ 73.9 KiB]                                                
Operation completed over 1 objects/73.9 KiB.                                     
tar: cloudbuild-test.yaml: time stamp 2024-06-01 13:39:13.706183 is 8.186919054 s in the future
BUILD
Starting Step #0 - "test command 1"
Step #0 - "test command 1": Already have image (with digest): gcr.io/cloud-builders/gcloud
Step #0 - "test command 1": abc
Step #0 - "test command 1": processor	: 0
Step #0 - "test command 1": vendor_id	: GenuineIntel
Step #0 - "test command 1": cpu family	: 6
Step #0 - "test command 1": model		: 79
Step #0 - "test command 1": model name	: Intel(R) Xeon(R) CPU @ 2.20GHz
Step #0 - "test command 1": stepping	: 0
Step #0 - "test command 1": microcode	: 0xffffffff
Step #0 - "test command 1": cpu MHz		: 2199.998
Step #0 - "test command 1": cache size	: 56320 KB
Step #0 - "test command 1": physical id	: 0
Finished Step #0 - "test command 1"
Starting Step #1 - "test command 2"
Step #1 - "test command 2": Already have image (with digest): gcr.io/cloud-builders/gcloud
Step #1 - "test command 2": abc
Step #1 - "test command 2": processor	: 0
Step #1 - "test command 2": vendor_id	: GenuineIntel
Step #1 - "test command 2": cpu family	: 6
Step #1 - "test command 2": model		: 79
Step #1 - "test command 2": model name	: Intel(R) Xeon(R) CPU @ 2.20GHz
Step #1 - "test command 2": stepping	: 0
Step #1 - "test command 2": microcode	: 0xffffffff
Step #1 - "test command 2": cpu MHz		: 2199.998
Step #1 - "test command 2": cache size	: 56320 KB
Step #1 - "test command 2": physical id	: 0
Finished Step #1 - "test command 2"
PUSH
DONE

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/20728.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/20728.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/20728.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

函數的創建和調用

自學python如何成為大佬(目錄):https://blog.csdn.net/weixin_67859959/article/details/139049996?spm1001.2014.3001.5501 提到函數,大家會想到數學函數吧,函數是數學最重要的一個模塊,貫穿整個數學學習過程。在Python中,函數…

深入解析 YOLOv8 中的 `conv.py`(代碼圖文全解析-下)

😎 作者介紹:我是程序員行者孫,一個熱愛分享技術的制能工人。計算機本碩,人工制能研究生。公眾號:AI Sun,視頻號:AI-行者Sun 🎈 本文專欄:本文收錄于《yolov8》系列專欄&…

【linux軟件基礎知識】與調度相關的進程描述符

進程描述符 每個進程描述符都包括幾個與調度相關的字段,如下代碼所示: //include/asm-arm/thread_info.h /** low level task data that entry.S needs immediate access to.* __switch_to() assumes cpu_context follows immediately after cpu_domain.*/ struct thread_in…

vite為什么速度快

原因 vite快的原因是因為 vite在開發環境中是使用的 esbuild,esbuild 是 go 寫的,go 編譯型語言、多線程,nodejs 解釋型語言、單線程,并且 vite 使用了原生 esm 導入的,所以快一點,當然,這也…

6.1Java方法

1、方法定義: 方法是一種語法結構,它可以把一段代碼封裝成一個功能,以便重復調用 方法的完整格式: 修飾符 返回類型 方法名(形參列表){ 方法體代碼(需要執行的功能代碼) return 返回值; } package com.define;public class …

【緩存】框架層常見問題和對策

緩存是為了加快讀寫速度,再了解redis這類框架層的緩存應用之前,我們不妨先思考下操作系統層面的緩存解決方案,這樣有助于我們更深的理解緩存,哪些是系統層面的,哪些是服務層面。 以下是一些常見的緩存問題及其解決方案…

面向對象編程 (OOP):深入理解繼承、多態和抽象

1. 簡介 面向對象編程 (OOP) 是一種強大的編程范式,它通過將程序組織成對象的集合來簡化軟件設計和開發。與傳統的程序設計方法相比,OOP 提供了一種更自然、更易于理解和維護的方式來構建復雜的軟件系統。OOP 的核心概念包括:對象、類、繼承、…

Java進階學習筆記31——日期時間

Date: 代表的是日期和時間。 分配Date對象并初始化它以表示自標準基準時間(稱為紀元)以來的指定毫秒數,即1970年1月1日00:00:00。 有參構造器。 package cn.ensource.d3_time;import java.util.Date;public class Test1Date {pu…

linux C/C++靜態庫制作

概念:程序在編譯時會把庫文件的二進制代碼鏈接到目標程序中,這種方式稱為靜態鏈接。 如果多個程序中用到了同一靜態庫中的函數或類,就會存在多份拷貝。 特點: 靜態庫的鏈接是在編譯時期完成的,執行的時候代碼加載速度…

Java—異常處理

異常的結構圖 異常知識點 異常分類: 按照在程序編譯階段是否被檢查,異常分為編譯時異常(Checked Exception)和運行時異常(Unchecked Exception)。編譯時異常是指必須進行顯式處理的異常,例如IOE…

【Linux】寫一個日志類

文章目錄 1. 源代碼2. 函數功能概覽3. 代碼詳細解釋3.1 頭文件和宏定義3.2 Log類定義3.3 打印日志的方法3.4 操作符重載和析構函數3.5 可變參數函數的原理 4. 測試用例 1. 源代碼 下面代碼定義了一個 Log 類,用于記錄日志信息。這個類支持將日志信息輸出到屏幕、單…

Java擴展機制:SPI與Spring.factories詳解

一、SPI SPI全稱Service Provider Interface,是Java提供的一套用來被第三方實現或者擴展的API,它可以用來啟用框架擴展和替換組件。 整體機制圖如下: Java SPI 實際上是“基于接口的編程+策略模式+配置文件”組合實現的動態加載機制。 系統設計的各個抽象,往往有很多不…

戴爾科技:一盆冷水澆醒了AIPC

這年頭,只要沾上英偉達的公司,不論美股還是大A,都跟著雞犬升天幾輪過,但昨晚英偉達蒸發1064億美元, 跟著遭罪的也不少,有沒有一夜驚魂夢醒的感覺? 今天我們來說說——戴爾科技。 昨晚戴爾科技大跌5.18%&a…

5G無線標準演進綜述及新技術引入

摘 要 隨著經濟和社會的發展,5G業務越來越豐富多彩,1080P高清視頻、裸眼3D、網聯汽車、云手機等新業務、新終端對網絡的要求也越來越高;另一方面,5G標準持續演進,在MIMO、載波聚合、移動性管理、uRLLC、切片、定位等方…

你了解MySQL分區表嗎?知道哪些情況不適用分區表嗎?

一、分區表的使用 簡單來說,分區表就是把物理表結構相同的幾張表,通過一定算法,組成一張邏輯大表。這種算法叫“分區函數”,當前 MySQL 數據庫支持的分區函數類型有 RANGE、LIST、HASH、KEY、COLUMNS。 無論選擇哪種分區函數,都要指定相關列成為分區算法的輸入條件,這些列…

ESP32開發筆記

ESP32 學習筆記 MQTT5 共享訂閱 什么是共享訂閱? 在普通的訂閱中,每發布一條消息,所有匹配的訂閱端都會收到該消息的副本。然而,當某個訂閱端的消費速度無法跟上消息的生產速度時,我們無法將其中一部分消息分流到…

`nano` 文本編輯器快捷鍵使用

在 nano 文本編輯器中,可以幫助用戶高效編輯文本,下面是每個快捷鍵的詳細解釋: 常用快捷鍵 ^G: Help - 顯示幫助信息。這里的 ^ 代表 Ctrl 鍵,因此 ^G 就是 Ctrl G。^O: Write Out - 保存文件。^O 即 Ctrl O,用于將…

模仿庫實現priority_queue

1 priority_queue 1.1 概念 優先級隊列,一種大/小堆(默認為大堆) 1.2 大堆和小堆 一種完全二叉樹,大堆根節點一定比子字節大 小堆根節點一定比子字節小 向下調整 從根節點開始比較與子節點的大小不斷向下 向上調整 找到最后一個非葉子節點&#xf…

mac多媒體影音庫:Emby for Mac 中文版

Emby軟件是一款功能強大的媒體服務器軟件,旨在為用戶提供豐富的多媒體體驗。以下是關于Emby軟件的詳細介紹: 下載地址:https://www.macz.com/mac/7964.html?idOTI2NjQ5Jl8mMjcuMTg2LjE1LjE4Mg%3D%3D 主要功能 媒體管理:Emby允許用…

代碼隨想錄-Day25

216.組合總和III 找出所有相加之和為 n 的 k 個數的組合,且滿足下列條件: 只使用數字1到9 每個數字 最多使用一次 返回 所有可能的有效組合的列表 。該列表不能包含相同的組合兩次,組合可以以任何順序返回。 示例 1: 輸入: k 3, n 7 輸…