You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Github Actions向Octopus Deploy传递自定义变量失败问题求助

解决Github Actions向Octopus Deploy传递变量后脚本无法获取的问题

核心问题分析

你通过deploy-release-action传递的变量属于部署级临时变量(而非版本绑定变量),且脚本中引用方式不符合Octopus的变量作用域规则,同时版本创建后的immutable特性也会导致变量无法被版本内的脚本识别。


解决方案1:创建版本时绑定变量(推荐)

Octopus的版本是不可变的,建议在create-release-action阶段就传入变量,让变量与版本绑定,这样部署时脚本可直接引用:

修改Github Actions代码

[ ...Octopus Authentication Step ...]

- name: Create Release
  uses: OctopusDeploy/create-release-action@v3
  with:
    server: ${{ vars.MY_SERVER_URL }}
    space: "Default"
    project: "my-deploy-pipeline"
    release_number: "v${{ github.run_number }}"
    git_ref: "${{ github.ref }}"
    git_commit: "${{ github.sha }}"
    # 添加变量传递到版本中
    variables: |
      MyVar: <somevar>
      BuildNumber: ${{ github.run_number }}
      GitCommit: ${{ github.sha }}
      Branch: ${{ github.ref_name }}

- name: Deploy Release
  id: deploy_release
  uses: OctopusDeploy/deploy-release-action@v3
  with:
    server: ${{ vars.MY_SERVER_URL }}
    space: 'Default'
    project: "my-deploy-pipeline"
    release_number: "v${{ github.run_number }}"
    environments: "Development"

脚本中正确引用变量

Octopus会将变量自动注入为Shell环境变量(变量名转为大写,空格/点替换为下划线),或通过Octostache语法引用(需开启脚本变量替换):

#!/bin/sh

# 方式1:直接读取环境变量(推荐)
echo "$MYVAR"
echo "$BUILDNUMBER"
echo "$GITCOMMIT"
echo "$BRANCH"

# 方式2:Octostache替换(需开启脚本变量替换)
echo "#{MyVar}"
echo "#{BuildNumber}"

注意:使用Octostache语法时,需在Octopus的脚本步骤设置中开启Enable variable substitution in scripts选项。


解决方案2:部署时传递临时变量

若必须在部署阶段传递变量,需明确引用部署级变量的作用域:

脚本中引用方式

#!/bin/sh

# 方式1:环境变量读取
echo "$MYVAR"

# 方式2:Octostache指定部署变量作用域
echo "#{Octopus.Deployment.Variables[MyVar]}"

排查验证步骤

  1. 确认变量是否传递成功:进入Octopus部署详情页面,点击Variables标签,搜索你传递的变量名,确认变量存在于列表中。
  2. 检查YAML格式:确保variables块的缩进与其他with参数对齐,无语法错误。
  3. 验证脚本设置:确认脚本步骤开启了变量替换选项,执行环境为bash/sh(与脚本语法匹配)。

内容的提问来源于stack exchange,提问作者baconStrips

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 08:44:51