github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/website/source/docs/enterprise/vcs/git.html.md (about)

     1  ---
     2  layout: "enterprise"
     3  page_title: "Git - VCS Integrations - Terraform Enterprise"
     4  sidebar_current: "docs-enterprise-vcs-git-"
     5  description: |-
     6    Git repositories can be integrated with Terraform Enterprise by using push command.
     7  ---
     8  
     9  # Git Integration
    10  
    11  Git repositories can be integrated with Terraform Enterprise by using
    12  [`terraform push`](/docs/commands/push.html) to import Terraform configuration
    13  when changes are committed. When Terraform configuration is imported using
    14  `terraform push` a plan is automatically queued.
    15  
    16  -> This integration is for Git repositories **not** hosted on GitHub. For GitHub, please see the GitHub documentation instead.
    17  
    18  ## Setup
    19  
    20  Terraform configuration can be manually imported by running `terraform push`
    21  like below:
    22  
    23  ```shell
    24  $ terraform push -name=$USERNAME/ENV_NAME
    25  ```
    26  
    27  A better option than having to manually run `terraform push` is to run it
    28  using a git commit hook. A client-side `pre-push` hook is suitable and will
    29  push your Terraform configuration when you push local changes to your Git
    30  server.
    31  
    32  ### Client-side Commit Hook
    33  
    34  The script below will execute `terraform push` when you push local changes to
    35  your Git server. Place the script at `.git/pre-push` in your local Git
    36  repository, set the necessary variables, and ensure the script is executable.
    37  
    38  ```shell
    39  #!/bin/bash
    40  #
    41  # An example hook script to push Terraform configuration to Terraform Enterprise.
    42  #
    43  # Set the following variables for your project:
    44  # - ENV_NAME - your environment name (e.g. org/env)
    45  # - TERRAFORM_DIR - the local directory to push
    46  # - DEFAULT_BRANCH - the branch to push. Other branches will be ignored.
    47  
    48  ENV_NAME="YOUR_ORG/YOUR_ENV"
    49  TERRAFORM_DIR="terraform"
    50  DEFAULT_BRANCH=""
    51  
    52  if [[ -z "$ENV_NAME" || -z "$TERRAFORM_DIR" || -z "$DEFAULT_BRANCH" ]]; then
    53    echo 'pre-push hook: One or more variables are undefined. Canceling push.'
    54    exit 1
    55  fi
    56  
    57  current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
    58  
    59  if [ "$current_branch" == "$DEFAULT_BRANCH" ]; then
    60    echo "pre-push hook: Pushing branch [$current_branch] to environment [$ENV_NAME]."
    61    terraform push -name="$ENV_NAME" $TERRAFORM_DIR
    62  else
    63    echo "pre-push hook: NOT pushing branch [$current_branch] to environment [$ENV_NAME]."
    64  fi
    65  
    66  ```