首页 > 解决方案 > 运行代码部署挂钩时找不到 Go 命令

问题描述

您好,我正在尝试为我的 golang 应用程序创建 CodeDeploy 部署。我有一个自动缩放组,它使用我创建的 AMI,其中安装了我需要的所有库。当我尝试运行 CodeDeploy 它存在我的错误after_install

LifecycleEvent - AfterInstall
Script - scripts/after_install.sh
[stderr]/opt/codedeploy-agent/deployment-root/a65d9a2e-fddd-471c-8ea1-c018792d00bd/d-4IKP3PP4Y/deployment-archive/scripts/after_install.sh: 
line 4: go: command not found

我知道 go 已安装在服务器上,我可以通过 ssh 进入服务器并运行 go 命令来验证。最初我让我的after_install钩子以 root 身份运行,所以这就是为什么我认为它抱怨 go 没有被安装。

我将它更新为运行,因为ubuntu这里是 appspec 文件

version: 0.0
os: linux
files:
  - source: ./
    destination: ./home/ubuntu/code
hooks:
  AfterInstall:
    - location: scripts/after_install.sh
      timeout: 180
      runas: ubuntu
  ApplicationStart:
    - location: scripts/application_start.sh
      timeout: 180
      runas: root

但是我仍然得到 go command not found 的错误。我以用户身份通过​​ SSH 连接到服务器ubuntu,我可以清楚地看到 go 已安装。

我更进一步并运行了after_install.sh文件,它没有错误地工作。我在这里做错了什么?

只是为了特别好奇这里是我的after_install.sh文件

#!/bin/bash

cd /home/ubuntu/code/vibeify/cmd/vibeify
go build

标签: amazon-web-servicesgoaws-code-deploy

解决方案


如果您go只能在交互式 shell 中使用没有完整安装路径的命令,请检查$HOME/.bashrc.

它可能取决于操作系统默认设置,但某些操作系统默认 bashrc 文件包含不会在非交互式 shell 中加载配置文件的脚本。

# open $HOME/.bashrc file
# and comment out these lines
case $- in
    *i*) ;;
      *) return;;
esac

推荐阅读