Golang: go build failing when called as part of makefile - go

I have the following make file:
default: builddocker
setup:
go get github.com/golang/protobuf/proto
go get github.com/golang/protobuf/protoc-gen-go
go get golang.org/x/net/context
go get google.golang.org/grpc
buildgo:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o main ./customer-server/
builddocker:
docker build -t johnwesonga/grpc/customer-server -f ./Dockerfile.build .
docker run -t johnwesonga/grpc/customer-server /bin/true
docker cp `docker ps -q -n=1`:/main .
chmod 755 ./main
docker build --rm=true --tag=johnwesonga/grpc/customer-server -f Dockerfile.static .
My Dockerfile.build has:
FROM golang
ADD Makefile /
WORKDIR /
RUN make setup
RUN make buildgo
CMD ["/bin/bash"]
But every time I run "make builddocker" I get the following error:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o main ./customer-server/
can't load package: package ./customer-server: open /customer-server: no such file or directory
make: *** [buildgo] Error 1
Makefile:10: recipe for target 'buildgo' failed
The command '/bin/sh -c make buildgo' returned a non-zero code: 2
make: *** [builddocker] Error 2
But if I run "Make buildgo" it works just fine, any idea why this is failing?
FYI my source tree as follows:
/go/src/github.com/johnwesonga/grpc/MakeFile
/go/src/github.com/johnwesonga/grpc/customer-server
/go/src/github.com/johnwesonga/grpc/customer-server/main.go

Related

Go get "get" unexpected EOF

Thank you for visiting here.
First of all, I apologize for my bad English, maybe a little wrong, hope you can help me.
Then I had a little problem when deploying a new CI/CD system on k8s platform (v1.23.5+1) with Gitlab runner (14.9.0) and dind (docker:dind)
When deploying CI to Golang apps with private repositories at https://gitlab.domain.com, (I did the go env -w GOPRIVATE configuration), I had a problem with the go mod tidy command. Specifically getting the unexpected EOF error. I've tried go mod tidy -v but it doesn't seem to give any more info.
I did a lot of work to figure out the problem. Specifically, I have done wget and git clone commands with my private repository and they are still able to download successfully. I tried adding a private repository at https://gitlab.com in go.mod, they can still be retrieved without any errors.
And actually, without using my new runner, I can still git clone and go mod tidy in another vps.
All of this leaves me wondering where am I actually getting the error? Is it my gitlab or my k8s gitlab runner
This is runner output
go: downloading gitlab.domain.com/nood/fountain v0.0.12
unexpected EOF
Cleaning up project directory and file based variables
ERROR: Job failed: command terminated with exit code 1
This is my .gitlab-ci.yml
image: docker:latest
stages:
- build
- deploy
variables:
GTV_ECR_REPOSITORY_URL: repo.domain.com
PROJECT: nood
APP_NAME: backend-super-system
APP_NAME_ECR: backend-super-system
IMAGE_TAG: $GTV_ECR_REPOSITORY_URL/$PROJECT/$APP_NAME_ECR
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
services:
- name: docker:dind
entrypoint: ["env", "-u", "DOCKER_HOST"]
command: ["dockerd-entrypoint.sh", "--tls=false"]
build:
stage: build
allow_failure: false
script:
- echo "Building image."
- docker pull $IMAGE_TAG || echo "Building runtime from scratch"
- >
docker build
--cache-from $IMAGE_TAG
-t $IMAGE_TAG --network host .
- docker push $IMAGE_TAG
Dockerfile
FROM golang:alpine3.15
LABEL maintainer="NoodExe <nood.pr#gmail.com>"
WORKDIR /app
ENV BIN_DIR=/app/bin
RUN apk add --no-cache gcc build-base git
ADD . .
RUN chmod +x scripts/env.sh scripts/build.sh \
&& ./scripts/env.sh \
&& ./scripts/build.sh
# stage 2
FROM alpine:latest
WORKDIR /app
ENV BIN_DIR=/app/bin
ENV SCRIPTS_DIR=/app/scripts
ENV DATA_DIR=/app/data
# Build Args
ARG LOG_DIR=/var/log/nood
# Create log directory
RUN mkdir -p ${BIN_DIR} \
mkdir -p ${SCRIPTS_DIR} \
mkdir -p ${DATA_DIR} \
mkdir -p ${LOG_DIR} \
&& apk update \
&& addgroup -S nood \
&& adduser -S nood -G nood \
&& chown nood:nood /app \
&& chown nood:nood ${LOG_DIR}
USER nood
COPY --chown=nood:nood --from=0 ${BIN_DIR} /app
COPY --chown=nood:nood --from=0 ${DATA_DIR} ${DATA_DIR}
COPY --chown=nood:nood --from=0 ${SCRIPTS_DIR} ${SCRIPTS_DIR}
RUN chmod +x ${SCRIPTS_DIR}/startup.sh
ENTRYPOINT ["/app/scripts/startup.sh"]
scripts/env.sh
#!/bin/sh
go env -w GOPRIVATE=gitlab.domain.com/*
git config --global --add url."https://nood_deploy:rvbsosecret_Hizt97zQSn#gitlab.domain.com".insteadOf "https://gitlab.domain.com"
scripts/build.sh
#!/bin/sh
grep -v "replace\s.*=>.*" go.mod > tmpfile && mv tmpfile go.mod
go mod tidy
set -e
BIN_DIR=${BIN_DIR:-/app/bin}
mkdir -p "$BIN_DIR"
files=`ls *.go`
echo "****************************************"
echo "******** building applications **********"
echo "****************************************"
for file in $files; do
echo building $file
go build -o "$BIN_DIR"/${file%.go} $file
done
Thank you for still being here :3
This is a known issue with installing go modules from gitlab in nested locations. The issue describes several workarounds/solutions. One solution is described as follows:
create a gitlab Personal Access Token with at least read_api and read_repository scopes.
create a .netrc file:
machine gitlab.com
login yourname#gitlab.com
password yourpersonalaccesstoken
use go get --insecure to get your module
do not use the .gitconfig insteadOf workaround
For self-hosted instances of GitLab, there is also the additional option of using the go proxy, which is what I do to resolve this problem.
For additional context, see this answer to What's the proper way to "go get" a private repository?

Cross compile gopacket for linux under MacOS

We want to build gopacket for Linux under MacOS.
What we have tried.
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build
Errors
..gopacket/pcap/pcap.go:30:22: undefined: pcapErrorNotActivated
..gopacket/pcap/pcap.go:52:17: undefined: pcapTPtr
..gopacket/pcap/pcap.go:64:10: undefined: pcapPkthdr
Try to do cross compile through docker golang-crossbuild
docker run -it --rm \
-v /Users/my/server:/go/src/my/server \
-w /go/src/my/server \
-e CGO_ENABLED=1 \
-e CGO_LDFLAGS+="-L/libpcap/libpcap-1.8.1-amd64" \
docker.elastic.co/beats-dev/golang-crossbuild:1.16.4-main \
--build-cmd "GOOS=linux GOARCH=amd64 go build . " \
-p "linux/amd64"
Errors
...pcap/pcap_unix.go:34:18: fatal error: pcap.h: No such file or directory
#include <pcap.h>
^
compilation terminated.
Is there any better way to do cross compile gopacket for linux under MacOS?
Finally, we solve it after installing libpcap-dev
First, we build one new docker based on golang-crossbuild
Dockerfile
FROM docker.elastic.co/beats-dev/golang-crossbuild:1.16.4-main
RUN apt-get update && apt-get install -y libpcap-dev
build script is
docker run -it --rm \
-v /Users/my/server:/go/src/my/server \
-w /go/src/my/server \
-e CGO_ENABLED=1 \
-e CC=aarch64-linux-gnu-gcc \
-e CGO_LDFLAGS+="-L/libpcap/libpcap-1.8.1" \
golang-new-builder:latest \
--build-cmd "GOOS=linux GOARCH=amd64 go build . " \
-p "linux/amd64"

What does a phony docker.* target mean in a Makefile?

What is the meaning of this make target:
.PHONY: docker.%
docker.%:
go mod tidy
docker build -t $* .
docker run -p 5751:5751 -v $$(pwd)/:/work $* /work/config.yaml
In particular, what is the meaning of % in docker.% and how is it being utilized here?

Docker build fails when executed within Makefile

So, I have a Docker build command that I have tested which works great
docker build \
-t app \
--no-cache --network host \
--build argssh_private_key="$(cat ~/.ssh/id_rsa)"\
--build-arg python_version="3.6.8" -f Dockerfile .
To ease the pain of the team learning Docker I encapsulated a few of the commands - build, start, stop - within a Makefile. However, within the Makefile I need to change the command slightly by modifying
$(cat ~/.ssh/id_rsa)
to
$(shell cat ~/.ssh/id_rsa)
When I execute the following:
make build
I receive the following message:
Step 13/20 : RUN git clone --depth 1 "${git_user}#${git_host}:${git_repo}" app
---> Running in d2eb41a71315
Cloning into 'app'...
Warning: Permanently added the ECDSA host key for IP address [ip_address] to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
However, I do not have the same issue when executing from the command-line. I I think it has something to do with the way the call the "cat" command but, I do not know a way to resolve.
Any ideas ?
Makefile:
APP_NAME=ccs_data_pipeline
DATA?="${HOME}/data"
DOCKER_FILE=Dockerfile
PYTHON_VERSION?=3.6.8
SRC?=$(shell dirname `pwd`)
PRIVATE_KEY?=$(shell echo $(shell cat ~/.ssh/id_rsa))
build: ## Build container for ccs data pipeline
docker build \
-t $(APP_NAME) \
--no-cache --network host \
--build-arg ssh_private_key="$(PRIVATE_KEY)" \
--build-arg python_version="$(PYTHON_VERSION)" \
-f $(DOCKER_FILE) .
start: ## Start the docker container
docker run \
-it -v $(DATA):/data \
--network host \
--rm \
--name="$(APP_NAME)" $(APP_NAME)
stop: ## Stop the docker container
docker stop $(APP_NAME); \
docker rm $(APP_NAME)
Please show your actual makefile, or at least the entire rule that is having the error. The single command you provided, with no context, is not enough to understand what you're doing or what might be wrong.
Note that it is often not correct to replace a shell operation like $(...) with a make shell command $(shell ...). However, sometimes it will work "by accident", where the real differences between those commands don't happen to matter.
In general you should never use $(shell ...) inside a recipe (I have no idea if this command appears in a recipe). Instead, you should escape all the dollar signs that you want to be passed verbatim to the shell when it runs your recipe:
$$(cat ~/.ssh/id_rsa)

During cppunit make install error for task install-m4DATA appeared

I've been building freedesktop cppunit with mingw
https://www.freedesktop.org/wiki/Software/cppunit/
I've made some fixes to source files and makefiles and been able to
successfully execute commmands:
./autogen.sh
./configure
make
without any errors.
There was a need to set
m4_DATA = m4
instead of value "cppunit.m4" in Makefile.am(there was no such dir in build and it looks like such dir is "m4" dir).
After successful make execution doing
make --debug install
failed with error:
File install-m4DATA not found
from Makefile.am:
m4dir = $(datadir)/aclocal //what is datadir btw?
automake generated Makefile task:
install-m4DATA: $(m4_DATA)
#$(NORMAL_INSTALL) // fails at this line
test -z "$(m4dir)" || $(MKDIR_P) "$(DESTDIR)$(m4dir)"
#list='$(m4_DATA)'; test -n "$(m4dir)" || list=; \
for p in $$list; do \
if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
echo "$$d$$p"; \
done | $(am__base_list) | \
while read files; do \
echo " $(INSTALL_DATA) $$files '$(DESTDIR)$(m4dir)'"; \
$(INSTALL_DATA) $$files "$(DESTDIR)$(m4dir)" || exit $$?; \
done
before that task NORMAL_INSTALL is set to:
NORMAL_INSTALL = :
How to fix "make install" execution.
Will it work if i have only mingw envieronment on my PC?
Version is 1.13.2 fresh from git repo.
I also post lines from "make --debug install" output
File 'install-m4DATA' does not exist.
Must remake target 'install-m4DATA'.
test -z "/usr/local/share/aclocal" || /usr/bin/mkdir -p "/usr/local/share/aclocal"
/usr/bin/install -c -m 644 ./m4 '/usr/local/share/aclocal'
Makefile:432: recipe for target 'install-m4DATA' failed
make[2]: Leaving directory 'c:/Users/user/projects/cppunit'
Makefile:799: recipe for target 'install-am' failed
make[1]: Leaving directory 'c:/Users/user/projects/cppunit'
Makefile:479: recipe for target 'install-recursive' failed
It also prints that that script omits directory at line
/usr/bin/install -c -m 644 ./m4 '/usr/local/share/aclocal'
Possibly here must go files from directory but not directory itself.
How to fix it to do that(i'm not familiar with Makefiles and bash)?

Resources