跳到主要内容

环境变量

环境变量是可用于自定义构建过程和存储敏感数据的键值对,例如对部署服务器访问权限的相关信息。

级别

环境变量分为四个级别:

  1. 工作区 - 可用于所有项目和流水线,在仪表板上进入配置页面。
  2. 项目 - 可用于项目中所有流水线,在目标项目中进入配置页面。
  3. 流水线 - 仅可用于所定义的流水线,在流水线中变量标签页进入配置。
  4. 操作 - 仅可用于所定义的操作,在操作中变量标签页进入配置。
提示

每个后续级别都将覆盖较高级别的值。例如,如果您有两个相同的变量,但一个在工作区级别定义,另一个在流水线级别定义,Buddy将在该流水线中使用流水线定义的变量值,并在工作区其余中使用工作区定义的变量值。

配置

  • 权限范围 – 定义变量权限级别
  • 名称 – 变量名称,例如:$SFTP
  • 值 – 变量名称所传达的值,例如:192.168.0.100

其他选项可让您在日志中隐藏变量值,并将变量标记为可设置,从而允许您更改其值并将其传递给下一个操作或流水线运行。

用法示例

让我们配置一些变量来加密对SFTP服务器的访问:

  1. 转到部署流水线中变量标签页。
  2. 添加变量并将您的服务器凭据设置为值。 例如:
    • $SFTP_HOST / 192.168.0.100
    • $SFTP_LOGIN / admin
    • $SFTP_PASS / buddy1234

  1. 现在您可以使用变量将访问凭据存储到您的部署服务器:

支持字段

整个服务都支持环境变量,但最常用于构建操作。您可以通过在支持的字段开头输入$来预测变量:

以下是当前支持的字段列表:

操作名称支持字段
Amazon S3SourcePath, Remote Path, Tag Objects Value
AWS CLIRun Commands
AWS CLI 2Run Commands
AWS ECSSource Path
AWS Lambda DeploySource Path
AWS Lambda TriggerPayload
Azure App ServiceURL, Login, Password
Azure CLIRun Commands
Azure StorageSource Path, Remote Path
Build Android AppRun Commands, Mount Filesystem Path, Mount Container Path, Working dir
Build Android React Native ApplicationRun Commands, Mount Filesystem Path, Mount Container Path, Working dir
Build Docker ImageName, Value
Build Flutter AppRun Commands, Mount Filesystem Path, Mount Container Path, Working dir
Build Ionic appMount Filesystem Path, Mount Container Path, Working dir
BugsnagAPI Key, Version, Revision, Builder Name, Release Stage
CloudFrontBase URL, Source Path
CloudflareBase URL
CodeDeploySource Path, Bucke tName, Description
Compress imagesSource Path, Destination Path
Copy filesSource Path, Remote Path
Custom BuildRun Commands, Mount Filesystem Path, Mount Container Path, Working dir
DatadogEvent Title, Content, Host, Tags, Aggregation Key
Datadog Service CheckHostname, Check
DigitalOcean DropletSource Path, Port, Login, Password, Remote Path, SSH Key Passphrase
DigitalOcean SpacesSourcePath, Remote Path
DigitalOcean Spaces CDNBaseURL
DiscordChannel, Attachments, Message
DockerfileBuild Arguments
Dockerfile LinterDockerfile, YAML file with ignores
Elastic BeanstalkSource, Version, Bucket 1, Environment 2
EmailSubject, Attachments, Message
ESLintMount Filesystem Path, Mount Container Path, Working dir
Find & replaceSource Path, Replacements
FirebaseMount Filesystem Path, Mount Container Path, Working dir
FTPSource Path, Hostname, Port, Login, Password, RemotePath
FTP DownloadHostname, Port, Login, Password, Source Path, Destination Path
FTPSSource Path, Hostname, Port, Login, Password, Remote Path
Function Deploy
GCESource Path, Hostname, Port, Login, Password, Remote Path
GCloud CLIRun Commands
GCSSourcePath, RemotePath
Ghost InspectorPath
Git PushURL, Login, Password
Gitcrypt lock
Gitcrypt unlock
Google App EngineSource Path, Bucket, Version, ImageURL
Google CDNBaseURL
Google Cloud CLIRun Commands
Google Cloud FunctionsSource Path
Google Cloud StorageSource Path, Remote Path
Google Compute EngineSource Path, Hostname, Port, Login, Password, Remote Path
Google Function Trigger
Heroku
Heroku CLIRun Commands
HoneybadgerEnvironment
HTTPEndpointURL, Login, Password, Request headers
Image compressionSource, Destination
Kubernetes ApplyServer, Username, Password, Source Path, Prune Whitelist, Grace Period
Kubernetes CLIServer, Username, Password, Run Commands
Run Kubernetes JobServer, Username, Password, YAML
Run Kubernetes PodServer, Username, Password, YAML
Set Kubernetes ImageServer, Username, Password, ImageName, Image Tag
Lambda DeploySourcePath
Lambda TriggerQualifier
Link checkerURL
LighthouseURL
Local Shell
LogglyTags
MonitoringEnvironment 3, Application 4
MS TeamsChannel email, Title, Attachments from filesystem
Netlify
New RelicDescription, Changelog, Version
Pass arguments
PHP CodeSnifferMount Filesystem Path, Mount Container Path, Working dir
PingHost
Publish APK to Google PlayAPK, Main expansion file, Patch expansion file, Service Key, Package name, Mapping file
PushbulletTitle, URL, Message
Push Docker ImageLogin, Password, Repository, Tags
PushoverTitle, URL, URL Title, Message
RackspaceSource Path, Remote Path
RaygunAccess Token, API Key, Version, User, Email, Comment
ReplaceSource Path, Replacements (name)
RollbarEnvironment, Comment, Username, Rollbar Username
RsyncSource Path, Hostname, Port, Login, Password, Remote Path
Run Docker ImageLogin, Password, Image, Version, CMD, Entrypoint
Run Helm CMDsRun Commands, Server, Username, Password
Run next pipelineComment
SentryRelease Version, Deploy Environment, Release URL, Deploy URL, Repository
Sentry On-premisesRelease Version, Deploy Environment, Release URL, Deploy URL, Repository
Sign APKSelect the APK, Location, Keystore Password, Key Alias, Output Directory, Output APK Name
SFTPSourcePath, Hostname, Port, Login, Password, Remote Path, Private SSH Key, SSH Key Passphrase
SFTP DownloadHostname, Port, Login, Password, Source Path, Destination Path, Private SSH Key, SSH Key Passphrase
ShopifySourcePath
SlackAttachments, Message
Sleep
SMSMessage
S3 DownloadSource Path, Destination
Split testsTests Path
SSHSource Path, Hostname, Port, Login, Password, RemotePath
SSL verifyWebsite URL
TCPHost, Port, String To Send, String To Expect
Theme Kit CLIRun Commands
TelegramAttachments, Message
UpCloudSource Path, Port, Login, Password, RemotePath, SSH Key Passphrase
Visual tests
VultrSourcePath, Port, Login, Password, Remote Path
Wait for approvalQuestion
WEBURL, Check For String, Login, Password, Request headers
WebDAVSource Path, Hostname, Login, Password
ZIPSource Path, Target Path, File Name
1-4 这些字段在前端不可用。但是,如果通过REST API或YAML添加操作,则变量正常工作。
提醒

有些字段是HTML文本区域,不支持推荐使用变量。例如,通知中的消息字段。然而它们确实完全支持环境变量。

提示

您还可以在列出的操作中使用多行环境变量

信息

如果您在此处未找到您的操作或必填字段不受支持,请联系客服:support@buddy.red

默认环境变量

Buddy在每次执行期间填充许多环境变量,例如:您部署的分支和执行的ID。您可以在webhooks、通知、条件执行以及任何需要传递CI/CD相关信息的地方使用。

名称
BUDDYRepresents whether the current environment is a Buddy environment
BUDDY_WORKSPACE_URLThe URL of the workspace
BUDDY_WORKSPACE_IDThe ID of the workspace
BUDDY_WORKSPACE_NAMEThe ID of the workspace
BUDDY_WORKSPACE_DOMAINThe slug of the workspace
BUDDY_PROJECT_URLThe URL of the project
BUDDY_PROJECT_NAMEThe name of the project
BUDDY_PROJECT_NAME_IDThe ID of the project
BUDDY_REPO_SLUGThe slug of the repository
BUDDY_REPO_SSH_URLThe SSH URL of the repository
BUDDY_SCM_URLThe URL of the repository synchronized with the project
BUDDY_SCM_PROVIDERThe name of the Git hosting provider synchronized with the project
BUDDY_PIPELINE_URLThe URL of the executed pipeline
BUDDY_PIPELINE_IDThe ID of the executed pipeline
BUDDY_PIPELINE_NAMEThe name of the executed pipeline
BUDDY_PIPELINE_REF_NAMEThe branch/tag/pull request or a wildcard string set in the executed pipeline
BUDDY_PIPELINE_TRIGGER_MODEThe trigger mode set in the executed pipeline
BUDDY_PIPELINE_TARGET_SITE_URLThe target website URL
BUDDY_INVOKER_URLThe URL to the profile of the execution invoker
BUDDY_INVOKER_IDThe ID of the execution invoker
BUDDY_INVOKER_NAMEThe name of the execution invoker
BUDDY_INVOKER_EMAILThe email address of the execution invoker
BUDDY_INVOKER_AVATAR_URLThe URL of the avatar of the execution invoker
BUDDY_EXECUTION_URLThe URL of the current execution
BUDDY_EXECUTION_IDThe ID of the current execution
BUDDY_EXECUTION_START_DATEThe date of the current execution
BUDDY_EXECUTION_MODEThe trigger mode used to run the current execution
BUDDY_EXECUTION_CLEAR_CACHERepresents whether the cache was purged before the current execution
BUDDY_EXECUTION_REFRESHRepresents whether the files were uploaded from scratch by the deployment actions in the current execution
BUDDY_EXECUTION_COMMENTThe comment attached to the current execution
BUDDY_EXECUTION_BRANCHThe name of the Git branch of the current execution
BUDDY_EXECUTION_TAGThe name of the Git tag of the current execution (provided the execution is tagged)
BUDDY_EXECUTION_PULL_REQUEST_IDThe ID of the currently executed pull request
BUDDY_EXECUTION_PULL_REQUEST_NOThe number of the currently executed pull request
BUDDY_EXECUTION_PULL_REQUEST_BASE_BRANCHThe name of the Git BASE branch of the currently executed Pull Request
BUDDY_EXECUTION_PULL_REQUEST_HEAD_BRANCHThe name of the Git HEAD branch of the currently executed Pull Request
BUDDY_EXECUTION_PREVIOUS_REVISIONThe SHA1 hash of the commit of the previous execution
BUDDY_EXECUTION_PREVIOUS_REVISION_MESSAGEThe commit message of the previously executed revision
BUDDY_EXECUTION_PREVIOUS_REVISION_SUBJECTThe commit subject of the previously executed revision
BUDDY_EXECUTION_REVISIONThe SHA1 hash of the commit of the current execution
BUDDY_EXECUTION_REVISION_URLThe URL of the commit of the current execution
BUDDY_EXECUTION_REVISION_SHORTThe short hash of the commit of the current execution
BUDDY_EXECUTION_REVISION_MESSAGEThe commit message of the currently executed revision
BUDDY_EXECUTION_REVISION_SUBJECTThe commit subject of the currently executed revision
BUDDY_EXECUTION_REVISION_COMMITTER_EMAILThe email address of the commiter email of the currently executed revision
BUDDY_EXECUTION_REVISION_COMMITTER_NAMEThe email address of the commiter of the currently executed revision
BUDDY_EXECUTION_CHANGELOGThe list of commits pushed since the last executed revision (one commit per line)
BUDDY_EXECUTION_CHANGELOG_ADDEDThe list of files added since the last executed revision (comma-separated)
BUDDY_EXECUTION_CHANGELOG_DELETEDThe list of files deleted since the last executed revision (comma-separated)
BUDDY_EXECUTION_CHANGELOG_CHANGEDThe list of files changed since the last executed revision (comma-separated)
BUDDY_EXECUTION_TIMEThe total time of the current execution until the action is triggered (in seconds)
BUDDY_FAILED_ACTION_LOGSThe logs from the action that has failed in the current execution (one entry per line)
BUDDY_FAILED_ACTION_NAMEThe name of the action that has failed in the current execution
BUDDY_EXECUTION_WARNINGS_COUNTThe total number of warnings that appeared in the current execution
CIRepresents whether the current environment is another CI environment
BUDDY_WORKERSThe JSON with information about all installed workers
BUDDY_WORKERS_CONCURRENT_SLOTSThe total number of pipeline slots across all workers
BUDDY_WORKERS_COUNT_${TAG}The number of workers tagged with ${TAG}
BUDDY_WORKERS_COUNT_NOT_TAGGEDThe number of untagged workers
BUDDY_WORKERS_FREE_SLOTS_${TAG}The number of free pipeline slots on workers tagged with ${TAG}
BUDDY_WORKERS_FREE_SLOTS_NOT_TAGGEDThe number of free pipeline slots on all untagged workers
BUDDY_WORKERS_AVG_LOAD_${TAG}The average load from the last minute on all workers tagged with ${TAG}
BUDDY_WORKERS_AVG_LOAD_NOT_TAGGEDThe average load from the last minute on all untagged workers
BUDDY_WORKER_ADDRESS_${TAG}The list of IP addresses of workers tagged with ${TAG}
BUDDY_WORKER_ADDRESS_NOT_TAGGEDThe list of IP addresses of untagged workers

示范值

BUDDY=true
BUDDY_WORKSPACE_URL=https://app.buddy.works/myworkspace
BUDDY_WORKSPACE_ID=1
BUDDY_WORKSPACE_NAME=My Workspace
BUDDY_WORKSPACE_DOMAIN=myworkspace
BUDDY_PROJECT_URL=https://app.buddy.works/myworkspace/my-project
BUDDY_PROJECT_NAME=my-project
BUDDY_PROJECT_NAME_ID=my-project
BUDDY_REPO_SLUG=buddyworks/my-project
BUDDY_REPO_SSH_URL=git@github.com:buddyworks/my-project
BUDDY_SCM_URL=https://github.com/buddyworks/my-project
BUDDY_SCM_PROVIDER=GITHUB
BUDDY_PIPELINE_URL=https://app.buddy.works/myworkspace/my-project/pipelines/pipeline/1
BUDDY_PIPELINE_ID=1
BUDDY_PIPELINE_NAME=Deploy to Production
BUDDY_PIPELINE_REF_NAME=master
BUDDY_PIPELINE_TRIGGER_MODE=CLICK
BUDDY_PIPELINE_TARGET_SITE_URL=https://buddy.works/
BUDDY_INVOKER_URL=https://app.buddy.works/myworkspace/profile/1
BUDDY_INVOKER_ID=1
BUDDY_INVOKER_NAME=Mike Benson
BUDDY_INVOKER_EMAIL=mikebenson@buddy.works
BUDDY_INVOKER_AVATAR_URL=https://app.buddy.works/image-server/user/0/0/0/0/0/0/1/05d20f3d58ce09a3b4e9g3334a810603/w/32/32/AVATAR.png
BUDDY_EXECUTION_URL=https://app.buddy.works/myworkspace/my-project/pipelines/pipeline/1/execution/5d9dc42c422f5a268b389d08
BUDDY_EXECUTION_ID=1
BUDDY_EXECUTION_START_DATE=2019-01-01T11:27:40.140Z
BUDDY_EXECUTION_MODE=CLICK
BUDDY_EXECUTION_CLEAR_CACHE=false
BUDDY_EXECUTION_REFRESH=false
BUDDY_EXECUTION_COMMENT=My first execution
BUDDY_EXECUTION_BRANCH=master
BUDDY_EXECUTION_TAG=v1.0
BUDDY_EXECUTION_PULL_REQUEST_ID=pull/1
BUDDY_EXECUTION_PULL_REQUEST_NO=1
BUDDY_EXECUTION_PULL_REQUEST_BASE_BRANCH=master
BUDDY_EXECUTION_PULL_REQUEST_HEAD_BRANCH=master/feature1
BUDDY_EXECUTION_REVISION=e5e13f8b7f8d5c6096a0501dc09b48eef05fea96
BUDDY_EXECUTION_REVISION_URL=https://app.buddy.works/myworkspace/my-project/repository/commit/e5e13f8b7f8d5c6096a0501dc09b48eef05fea96
BUDDY_EXECUTION_REVISION_SHORT=e5e13f8
BUDDY_EXECUTION_REVISION_MESSAGE=Create buddy.yml
BUDDY_EXECUTION_REVISION_SUBJECT=Create buddy.yml
BUDDY_EXECUTION_REVISION_COMMITTER_EMAIL=mikebenson@buddy.works
BUDDY_EXECUTION_REVISION_COMMITTER_NAME=Mike Benson
BUDDY_EXECUTION_CHANGELOG=[e5e13f8] Create buddy.yml | mikebenson | 2019-01-01T07:14:58
BUDDY_FAILED_ACTION_NAME=Upload files

将变量组合成词组

变量可以与其他词组合以创建比可用更长的短语,例如,自动命名您的基础架构、版本或沙盒。但您需要遵守以下规则:

  • 变量之前: 允许任何字符
  • 变量之后: 不允许字母、数字和下杠(_)

正确用法:

release-$BUDDY_EXECUTION_ID-staging
release$BUDDY_EXECUTION_ID:staging
release_$BUDDY_EXECUTION_ID[staging]

错误用法:

release-$BUDDY_EXECUTION_ID_staging (下杠)
release-$BUDDY_EXECUTION_IDstaging (字母)
release-$BUDDY_EXECUTION_ID1staging (数字)

传递变量

操作

在一个流水线中传递变量很有用。例如,当我们要将文件内容分配给变量并在通知中发送时:

信息
  1. 第一个操作构建应用程序并将日志输出保存到logs.txt
  2. 如果构建失败,SSH操作命令会用export LOGS=$(cat log.txt)将logs.txt的内容分配给一个变量。
  3. 然后将日志以$LOGS作为消息内容发送到微软Teams。
提醒

确保将变量设置为可设置,否则将无法更改其值。

执行运行

您也可以在执行运行之间传递变量值。这些值可以在一次执行中保存,然后在另一次执行中使用,例如,您可以使用版本类型设置一个变量并在每次执行时递增。

流水线

您可以使用运行下一个流水线操作在流水线之间传递变量:

  1. 进入操作中的变量标签页
  2. 定义变量名称和要传递的值:

信息

您可以将目标流水线中的任何变量分配为值

重置变量

要在流水线执行运行之间重置可设置的变量,您可以添加一个带有命令的操作,该命令将清除变量的值(最好在流水线的开头或结尾处):

export mySettableEnv=

运行开始时传递变量

您可以使用传递实参操作参数化您的执行 - 它会暂停流水线并等待您的输入以继续进一步执行:

编辑日期:2023/02/18