🕸️ .env 배포 오류(build error)

이 블로그는 푸터에 링크🔗된 테마를 기반으로 만들어진 Gatsby 블로그입니다.

제작자분께서 배포해 주신 테마에 검색 플러그인을 추가하고자 커스텀하였지만 로컬에서 gatsby develop 등은 가능하되 NetfliyVercel 에서 배포하려고 할 때는 계속해서 실패했습니다.

그 이유는 간단했습니다. 바로 .gitignore 파일에 .env가 추가되어 있어 github repo에는 push될 수 없기 때문입니다..🔞

위에서 언급한 NetfliyVercel 에서 배포할 때는 .gitignore 파일에서 .env를 삭제하고 .env를 포함해서 Private repo로 업로드해도 되겠지만

지금은 Github Pages에 커스텀 도메인을 연결하여 배포했기 때문에 Private repo를 사용할수가 없습니다.

git-secret을 통해 .env파일을 암호화시키고 업로드합니다.⬆️


🍎 Mac에서 설치

🔧 사전 설치

기본적으로 git은 당연히 설치되어 있어야 하며 (brew install git) key를 만들어야 하기 때문에 gpg (GnuPG) 설치가 필요합니다.

터미널에서 gpg —help 를 입력하면 다음과 같이 gpg 설치여부를 알 수 있습니다.

입력 후 아래와 같이 버전이 확인되지 않는다면 brew install gnupg 를 통해 gpg 를 설치합니다.

gpg (GnuPG) 2.3.1
libgcrypt 1.9.3
Copyright (C) 2021 Free Software Foundation, Inc.
License GNU GPL-3.0-or-later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: /Users/jamie/.gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
        CAMELLIA128, CAMELLIA192, CAMELLIA256
AEAD: EAX, OCB
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2

Syntax: gpg [options] [files]
Sign, check, encrypt or decrypt
Default operation depends on the input data

Commands: ...

🤐 git-secret 설치

brew install git-secret

🔏 설치 후 기본 세팅

🔑 gpg 기본 세팅

gpg --full-generate-key

위 코드를 입력하면 아래와 같이 출력됩니다.


gpg (GnuPG) 2.3.1; Copyright (C) 2021 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
   (1) RSA and RSA
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
   (9) ECC (sign and encrypt) *default*
  (10) ECC (sign only)
  (14) Existing key from card
Your selection?

1번 RSA 키를 만들고자 합니다. 1을 입력합니다.


RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (3072)

기본 설정대로 만들고자 하면 그냥 enter를 눌러 넘어갑니다.


Requested keysize is 3072 bits
Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0)
Key does not expire at all
Is this correct? (y/N)

필요에 따라 다르겠지만 굳이 소멸됨을 바라지 않으므로 기본설정 0으로 둡니다.


GnuPG needs to construct a user ID to identify your key.

Real name:

Real name과 email을 입력합니다.


generator a better chance to gain enough entropy.
gpg: key ******** marked as ultimately trusted
gpg: directory '*****' created
gpg: revocation certificate stored as '*****'
public and secret key created and signed.

pub   rsa3072 2021-0*-0* [SC]
*********
uid                      **** (gitsecret) [***@***](mailto:***@***)
sub   rsa3072 2021-0*-0* [E]

키 생성이 완료되었습니다.


📁 .env 파일 선택

🔃 파일 경로 설정

터미널에서 cd ~/ 등을 통해 .env 파일이 있는 폴더에 진입합니다.

해당 파일이 있는 폴더에서 git-secret init 을 통해 git-secret 저장소를 생성합니다.

이어서 사용자를 추가합니다. gpg key 생성 시 입력했던 e-mail을 입력합니다.

git-secret tell ***@***

입력을 마쳤으면 암호화를 원하는 파일을 지정합니다.

git-secret add .env

암호화합니다.

git-secret hide

이제 작업을 진행했던 경로에 .env.secret .gitsecret 가 생성된 것을 확인할 수 있습니다.


암호화 이후 파일 수정이 필요하다면 이 링크🔗를 통해 복호화 옵션에 대해 확인하거나, .env.secret.gitsecret

을 모두 지우고 git-secret init 부터 다시 시작합니다. 키는 다시 생성할 필요가 없습니다.

중요도가 낮은 코드(어차피 소스보기시 확인이 가능한 것) 들로 직접 수정 후 배포해보면 정상적으로 build가 가능한 것을 확인할 수 있습니다.