aws / aws-lambda-builders

Python library to compile, build & package AWS Lambda functions for several runtimes & framework
Apache License 2.0
338 stars 139 forks source link

Go - Invalid Executable for path /usr/local/go/bin/go with github actions, am I able to supply the path to the binaries setter in BaseWorkflow? #198

Closed RichDavis1 closed 4 years ago

RichDavis1 commented 4 years ago

Description:

Trying to integrate sam build/sam deploy with github actions ci/cd for a golang project. When the Dockerfile in my .github workflow runs my entrypoint.sh script, which then calls sam build, the template file is recognized and the first function starts to build but then I get the following error:

Invalid executable for go at /usr/local/go/bin/go
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/aws_lambda_builders/workflow.py", line 58, in wrapper
    valid_path = binary_checker.validator.validate(executable_path)
  File "/usr/lib/python3.8/site-packages/aws_lambda_builders/workflows/go_modules/validator.py", line 57, in validate
    raise MisMatchRuntimeError(language=self.LANGUAGE, required_runtime=self.runtime, runtime_path=runtime_path)
aws_lambda_builders.exceptions.MisMatchRuntimeError: go executable found in your path does not match runtime. 
 Expected version: go1.x, Found version: /usr/local/go/bin/go. 
 Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30

This process works fine locally from cli.

The closest ticket I've found to the situation I'm encountering: https://github.com/aws/aws-lambda-builders/issues/30

It appears that the lambda_builder validator is searching for the go executable in path /usr/local/go/bin/go when the github action setup-go has the executable at Found in cache @ /opt/hostedtoolcache/go/1.15.2/x64 causing this error to be produced.

I guess my question is where, if possible, can I alter either the path that the BaseWorkflow looks for the golang executable or change my path in either the github action.yaml file/dockerfile?

Also, does BaseWorkflow use any go environment variables like GOROOT to search for the executable?

Steps to reproduce the issue:

  1. Create a new github workflow, add a step to build the go environment:
    jobs:
    checkout:
    name: Checkout
    runs-on: ubuntu-latest
    #container:
    #  image: golang    
    steps:
    - name: Prepare Environment
      uses: actions/setup-go@v2
      with:
        go-version: ^1.14
    - run: |       
        go version       
    - name: Checkout
      uses: actions/checkout@v2
  2. Create your actions dockerfile: I used the golang:alpine image, install aws-sam-cli, and run your entrypoint.sh script.
  3. Within entrypoint.sh, cd into your projects directory and call sam build.

Observed result: Fails to find the go executable path when calling sam build from entrypoint.sh when performing sam build.

Expected result: Successfully find the golang executable and builds the lambda functions/resources specified in template.yaml.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc) Snippet of my .github/workflows/go.yml:

name: Go

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  checkout:
    name: Checkout
    runs-on: ubuntu-latest
    steps:
    - name: Prepare Environment
      uses: actions/setup-go@v2
      with:
        go-version: ^1.14
    - run: |       
        go version       
    - name: Checkout
      uses: actions/checkout@v2
    - name: sam build
      uses: ./.github/actions/sam/package
      with:
        sam_command: "build --debug"
        directory: ./sam-testaroni      
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}

Dockerfile:

FROM golang:1.14.0-alpine

ENV SAM_CLI_TELEMETRY 0

RUN apk add bash
RUN apk add git

ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3-dev && ln -sf python3-dev /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools

RUN apk add --no-cache --virtual builddeps gcc musl-dev && \
   pip --no-cache-dir install aws-sam-cli awscli && \
   apk del builddeps

RUN sam --version

COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

Full sam build --debug logs:

cd ./sam-testaroni
Run sam build --debug
'build' command is called
No Parameters detected in the template
3 resources found in the template
Found Serverless function with name='HelloWorldFunction' and CodeUri='hello-world/'
Found Serverless function with name='DontTouchThis' and CodeUri='nana/'
No Parameters detected in the template
Building function 'HelloWorldFunction'
Loading workflow module 'aws_lambda_builders.workflows'
Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', application_framework=None)'
Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
Registering workflow 'CustomMakeBuilder' with capability 'Capability(language='provided', dependency_manager=None, application_framework=None)'
Found workflow 'GoModulesBuilder' to support capabilities 'Capability(language='go', dependency_manager='modules', application_framework=None)'
Invalid executable for go at /usr/local/go/bin/go
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/aws_lambda_builders/workflow.py", line 58, in wrapper
    valid_path = binary_checker.validator.validate(executable_path)
  File "/usr/lib/python3.8/site-packages/aws_lambda_builders/workflows/go_modules/validator.py", line 57, in validate
    raise MisMatchRuntimeError(language=self.LANGUAGE, required_runtime=self.runtime, runtime_path=runtime_path)
aws_lambda_builders.exceptions.MisMatchRuntimeError: go executable found in your path does not match runtime. 
 Expected version: go1.x, Found version: /usr/local/go/bin/go. 
 Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30

Build Failed
Error: GoModulesBuilder:Validation - Binary validation failed for go, searched for go in following locations  : ['/usr/local/go/bin/go'] which did not satisfy constraints for runtime: go1.x. Do you have go for runtime: go1.x on your PATH?
RichDavis1 commented 4 years ago

We can close this.

For reference if anyone else encounters this, the problem stemmed from using actions/setup-go to configure the go environment at the beginning of my github actions workflow yaml.

I removed that step and just used the golang:alpine docker image and it worked great.

Also, if anyone encounters the problem where sam deploy can't find the location of the built binaries, you need to add ENV CGO_ENABLED=0 to your dockerfile.

jfuss commented 4 years ago

@RichDavis1 Glad you were able to resolve this.

We do not look in GOROOT to find the go binary but instead look through the PATH on the system to try and find go. If you run into this again, you can also try adding where the go binary is to the beginning of the PATH. This should allow SAM CLI to pick this up.

Closing as the original poster solved the issue.