Avoid evaluation of variables in a Makefile

I’m trying to create a Makefile, recovering the credentials from AWS Secrets manager, and pass it to Skaffold, but when I recover the password and I try sending it to the function it fails, because it is being evaluated.

And it happens again when I try to pass it as variables to skaffold with PG_USER=$(PG_USER) PG_PASSWORD=$(PG_PASSWORD) .

I tried fixing it with quotes, and double quotes, and adding $$ as other answers were saying, but at the end nothing worked. Not sure how to fix it, or where I could improve my skills in this area, I feel I only learn on these situations of doing, but then I’m in a hurry and don’t enjoy it.

Makefile

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>production-release:
$(eval creds := $(shell aws --profile $(AWS_PROFILE) secretsmanager get-secret-value --secret-id $(RDS_SECRET_ID) --query SecretString --output json))
$(eval user := $(shell echo $(creds) | jq -r .username))
$(eval password := $(shell echo $(creds) | jq -r .password))
@$(MAKE) .skaffold-build PROFILE=production PG_USER=$(user) PG_PASSWORD=$(password)
</code>
<code>production-release: $(eval creds := $(shell aws --profile $(AWS_PROFILE) secretsmanager get-secret-value --secret-id $(RDS_SECRET_ID) --query SecretString --output json)) $(eval user := $(shell echo $(creds) | jq -r .username)) $(eval password := $(shell echo $(creds) | jq -r .password)) @$(MAKE) .skaffold-build PROFILE=production PG_USER=$(user) PG_PASSWORD=$(password) </code>
production-release:
    $(eval creds := $(shell aws --profile $(AWS_PROFILE) secretsmanager get-secret-value --secret-id $(RDS_SECRET_ID) --query SecretString --output json))
    $(eval user := $(shell echo $(creds) | jq -r .username))
    $(eval password := $(shell echo $(creds) | jq -r .password))
    @$(MAKE) .skaffold-build PROFILE=production PG_USER=$(user) PG_PASSWORD=$(password)

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/bin/bash: line 1: GPhADd-6Y: No such file or directory
</code>
<code>/bin/bash: line 1: GPhADd-6Y: No such file or directory </code>
/bin/bash: line 1: GPhADd-6Y: No such file or directory

The whole password is O}Kyfg|42t$~*!.w_f<GPhADd-6Y

4

I’m trying to create a Makefile, recovering the credentials from AWS Secrets manager, and pass it to Skaffold, but when I recover the password and I try sending it to the function it fails, because it is being evaluated.

I presume you mean evaluated by the shell. Yes, it is. This is one reason why it can be problematic to forward make variables to the shell if you do not have tight control over their values. It can be hard to ensure that they are quoted appropriately.

The Makefile you show wants to be a shell script instead. Maybe this one:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#!/bin/sh
creds=$(aws --profile "$AWS_PROFILE" secretsmanager get-secret-value --secret-id "$RDS_SECRET_ID" --query SecretString --output json)
user=$(echo "$creds" | jq -r .username)
password=$(echo "$creds" | jq -r .password)
make .skaffold-build PROFILE=production PG_USER="$user" PG_PASSWORD="$password"
</code>
<code>#!/bin/sh creds=$(aws --profile "$AWS_PROFILE" secretsmanager get-secret-value --secret-id "$RDS_SECRET_ID" --query SecretString --output json) user=$(echo "$creds" | jq -r .username) password=$(echo "$creds" | jq -r .password) make .skaffold-build PROFILE=production PG_USER="$user" PG_PASSWORD="$password" </code>
#!/bin/sh

creds=$(aws --profile "$AWS_PROFILE" secretsmanager get-secret-value --secret-id "$RDS_SECRET_ID" --query SecretString --output json)
user=$(echo "$creds" | jq -r .username)
password=$(echo "$creds" | jq -r .password)
make .skaffold-build PROFILE=production PG_USER="$user" PG_PASSWORD="$password"

… or this one:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#!/bin/sh
creds=$(aws --profile "$AWS_PROFILE" secretsmanager get-secret-value --secret-id "$RDS_SECRET_ID" --query SecretString --output json)
make .skaffold-build
PROFILE=production
PG_USER="$(echo "$creds" | jq -r .username)"
PG_PASSWORD="$(echo "$creds" | jq -r .password)"
</code>
<code>#!/bin/sh creds=$(aws --profile "$AWS_PROFILE" secretsmanager get-secret-value --secret-id "$RDS_SECRET_ID" --query SecretString --output json) make .skaffold-build PROFILE=production PG_USER="$(echo "$creds" | jq -r .username)" PG_PASSWORD="$(echo "$creds" | jq -r .password)" </code>
#!/bin/sh

creds=$(aws --profile "$AWS_PROFILE" secretsmanager get-secret-value --secret-id "$RDS_SECRET_ID" --query SecretString --output json)
make .skaffold-build 
  PROFILE=production 
  PG_USER="$(echo "$creds" | jq -r .username)" 
  PG_PASSWORD="$(echo "$creds" | jq -r .password)"

Note that I’ve converted not only creds, user, and password into shell variables, which is probably needed, but also AWS_PROFILE and RDS_SECRET_ID, which is more a function of the conversion to (only) shell script.


If you want to have that inside a makefile, perhaps because there are also other targets in that file that are better suited for make, then don’t overthink it. Either of those variations can be adapted to serve as the recipe for a make target. For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>production-release:
creds=$$(aws --profile "$(AWS_PROFILE)" secretsmanager get-secret-value --secret-id "$(RDS_SECRET_ID)" --query SecretString --output json);
$(MAKE) .skaffold-build
PROFILE=production
PG_USER="$$(echo "$$creds" | jq -r .username)"
PG_PASSWORD="$$(echo "$$creds" | jq -r .password)"
.PHONY: production-release
</code>
<code>production-release: creds=$$(aws --profile "$(AWS_PROFILE)" secretsmanager get-secret-value --secret-id "$(RDS_SECRET_ID)" --query SecretString --output json); $(MAKE) .skaffold-build PROFILE=production PG_USER="$$(echo "$$creds" | jq -r .username)" PG_PASSWORD="$$(echo "$$creds" | jq -r .password)" .PHONY: production-release </code>
production-release:
        creds=$$(aws --profile "$(AWS_PROFILE)" secretsmanager get-secret-value --secret-id "$(RDS_SECRET_ID)" --query SecretString --output json); 
        $(MAKE) .skaffold-build 
            PROFILE=production 
            PG_USER="$$(echo "$$creds" | jq -r .username)" 
            PG_PASSWORD="$$(echo "$$creds" | jq -r .password)"

.PHONY: production-release

Note that I have switched AWS_PROFILE and RDS_SECRET_ID back to make variables. Other than that, I escaped all $ that are to be passed through to the shell by doubling them, and I put the assignment of shell variable creds on the same logical line as the sub-make command, but as a separate shell command.


Additional notes:

  • do not use $(eval). Especially not in recipes, but not outside them either. You can consider revisiting this advice when you’ve acquired enough expertise to feel safe ignoring me.

  • avoid $(shell) when you can, which should be most of the time. It’s usually preferable to just write a recipe. In fact, those who use makes other than GNU make don’t have $(shell) anyway, and they get along pretty well.

  • don’t suppress echo of recipe lines until you’re sure they’re working correctly, and consider not doing so even then.

The makefile‘s variables should not be defined inside a recipe.

If you do not want to obtain password always, and do it only if target is production-release, then you can do it inside conditional based on a goal.

It is also a good idea to quote strings:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ifeq "$(MAKECMDGOALS)" "production-release"
creds := $(shell aws --profile $(AWS_PROFILE) secretsmanager get-secret-value --secret-id $(RDS_SECRET_ID) --query SecretString --output json)
user := $(shell echo $(creds) | jq -r .username)
password := $(shell echo $(creds) | jq -r .password)
endif
production-release:
@$(MAKE) .skaffold-build PROFILE=production PG_USER="$(user)" PG_PASSWORD="$(password)"
</code>
<code>ifeq "$(MAKECMDGOALS)" "production-release" creds := $(shell aws --profile $(AWS_PROFILE) secretsmanager get-secret-value --secret-id $(RDS_SECRET_ID) --query SecretString --output json) user := $(shell echo $(creds) | jq -r .username) password := $(shell echo $(creds) | jq -r .password) endif production-release: @$(MAKE) .skaffold-build PROFILE=production PG_USER="$(user)" PG_PASSWORD="$(password)" </code>
ifeq "$(MAKECMDGOALS)" "production-release"
   creds := $(shell aws --profile $(AWS_PROFILE) secretsmanager get-secret-value --secret-id $(RDS_SECRET_ID) --query SecretString --output json)
   user := $(shell echo $(creds) | jq -r .username)
   password := $(shell echo $(creds) | jq -r .password)
endif

production-release:
    @$(MAKE) .skaffold-build PROFILE=production PG_USER="$(user)" PG_PASSWORD="$(password)"

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật