TIL,WIL(일간,주간 회고)

[TIL][리눅스] .env 파일 변경확인 법(aws 코드디플로이)

worldint 2023. 10. 25. 18:56

아래의 shell 스크립트의 의미는 sha256 암호화 알고리즘을 사용하여 저 /var/app/.env파일을 암호화하면
c3e2a7d1f4b9e6f8c0b2a9e5d7f6c8a3 /var/app/.env

이런식으로 나오는데 이중에서 앞에있는 부분만 빼서 hash변수에 담으라는 뜻이다
그리고 그걸 tmp/old_hash.txt에 넣으라는 뜻

hash=$(sha256sum /var/app/.env | awk '{ print $1 }')

echo $hash > /tmp/old_hash.txt

나는 환경변수 파일이 변경되면 특정 동작을 하고
변경되지 않으면 동작하지 않도록 구현할 때 사용하였다

aws의 코드디플로이를 사용하면
before_install -> afterInstall -> applicationStart
이런 단계들이 있는데
before_install 단계에서 위와 같이 스크립트를 사용하여 기존 환경변수 파일을
old_hash.txt에 담고
afterInstall 단계에서 새로 만들어진 환경변수 파일을
new_hash.txt에 담고
applicationStart 단계에서
이 둘을 아래와 같이 비교하여 원하는 작업을 할 수 있다

old_hash=$(cat /tmp/old_hash.txt)
new_hash=$(cat /tmp/new_hash.txt)

# Compare hashes and act accordingly if they are different.
if [ "$old_hash" != "$new_hash" ]; then 
    echo "File has changed."
else 
    echo "File has not changed." 
fi