Skip to content

Commit 1431664

Browse files
authored
docs: Add steps to configure auth to source Terraform modules from private GitHub repos (antonbabenko#517)
1 parent 6604ccc commit 1431664

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

README.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ If you are using `pre-commit-terraform` already or want to support its developme
5151
* [terraform\_wrapper\_module\_for\_each](#terraform_wrapper_module_for_each)
5252
* [terrascan](#terrascan)
5353
* [tfupdate](#tfupdate)
54-
* [Docker Usage: File Permissions](#docker-usage-file-permissions)
54+
* [Docker Usage](#docker-usage)
55+
* [File Permissions](#file-permissions)
56+
* [Download Terraform modules from private GitHub repositories](#download-terraform-modules-from-private-github-repositories)
5557
* [Authors](#authors)
5658
* [License](#license)
5759
* [Additional information for users from Russia and Belarus](#additional-information-for-users-from-russia-and-belarus)
@@ -241,7 +243,7 @@ pre-commit run -a
241243

242244
Or, using Docker ([available tags](https://github.com/antonbabenko/pre-commit-terraform/pkgs/container/pre-commit-terraform/versions)):
243245

244-
> **Note**: This command uses your user id and group id for the docker container to use to access the local files. If the files are owned by another user, update the `USERID` environment variable. See [File Permissions section](#docker-usage-file-permissions) for more information.
246+
> **Note**: This command uses your user id and group id for the docker container to use to access the local files. If the files are owned by another user, update the `USERID` environment variable. See [File Permissions section](#file-permissions) for more information.
245247
246248
```bash
247249
TAG=latest
@@ -845,7 +847,9 @@ If the generated name is incorrect, set them by providing the `module-repo-short
845847
Check [`tfupdate` usage instructions](https://github.com/minamijoyo/tfupdate#usage) for other available options and usage examples.
846848
No need to pass `--recursive .` as it is added automatically.
847849

848-
## Docker Usage: File Permissions
850+
## Docker Usage
851+
852+
### File Permissions
849853

850854
A mismatch between the Docker container's user and the local repository file ownership can cause permission issues in the repository where `pre-commit` is run. The container runs as the `root` user by default, and uses a `tools/entrypoint.sh` script to assume a user ID and group ID if specified by the environment variable `USERID`.
851855

@@ -865,6 +869,41 @@ $ ls -aldn .
865869
drwxr-xr-x 9 1000 1000 4096 Sep 1 16:23 .
866870
```
867871

872+
### Download Terraform modules from private GitHub repositories
873+
874+
If you use a private Git repository as your Terraform module source, you are required to authenticate to GitHub using a [Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token).
875+
876+
When running pre-commit on Docker, both locally or on CI, you need to configure the [~/.netrc](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) file, which contains login and initialization information used by the auto-login process.
877+
878+
This can be achieved by firstly creating the `~/.netrc` file including your `GITHUB_PAT` and `GITHUB_SERVER_HOSTNAME`
879+
880+
```bash
881+
# set GH values (replace with your own values)
882+
GITHUB_PAT=ghp_bl481aBlabl481aBla
883+
GITHUB_SERVER_HOSTNAME=github.com
884+
885+
# create .netrc file
886+
echo -e "machine $GITHUB_SERVER_HOSTNAME\n\tlogin $GITHUB_PAT" >> ~/.netrc
887+
```
888+
889+
The `~/.netrc` file will look similar to the following:
890+
891+
```
892+
machine github.com
893+
login ghp_bl481aBlabl481aBla
894+
```
895+
896+
> **Note**: The value of `GITHUB_SERVER_HOSTNAME` can also refer to a GitHub Enterprise server (i.e. `github.my-enterprise.com`).
897+
898+
Finally, you can execute `docker run` with an additional volume mount so that the `~/.netrc` is accessible within the container
899+
900+
```bash
901+
# run pre-commit-terraform with docker
902+
# adding volume for .netrc file
903+
# .netrc needs to be in /root/ dir
904+
docker run --rm -e "USERID=$(id -u):$(id -g)" -v ~/.netrc:/root/.netrc -v $(pwd):/lint -w /lint ghcr.io/antonbabenko/pre-commit-terraform:latest run -a
905+
```
906+
868907
## Authors
869908

870909
This repository is managed by [Anton Babenko](https://github.com/antonbabenko) with help from these awesome contributors:

0 commit comments

Comments
 (0)