|
1 | 1 | --- |
2 | 2 | title: "CI Integration" |
3 | | ---- |
| 3 | +--- |
| 4 | + |
| 5 | +Using digger with statesman allows you to have a convinient way to manage your state files in a secure manner. It is especially handy in more complex |
| 6 | +multi account setups where it gets harder to configure terraform to access state in a centralised location. In this page we will guide you through the process |
| 7 | +of setting up digger with statesman. We will also show you how to use digger with an S3 bucket if you do not wish to install and configure statesman. |
| 8 | + |
| 9 | +### Using digger with statesman |
| 10 | + |
| 11 | +If you would like to use digger with statesman the first step is to generate a terraform enterprise token. You can do this by running: |
| 12 | + |
| 13 | +``` |
| 14 | +terraform login <hostname> # (OPENTACO_PUBLIC_BASE_URL) |
| 15 | +cat ~/.terraform.d/credentials.tfrc.json |
| 16 | +``` |
| 17 | + |
| 18 | +And copy the value of the token from the JSON file of your terraform credentials file. Store the token as a secret STATESMANE_TOKEN in your CI system. |
| 19 | +From there you would need to make some small tweaks to your digger workflow: |
| 20 | + |
| 21 | +``` |
| 22 | + - uses: hashicorp/setup-terraform@v3 |
| 23 | + with: |
| 24 | + cli_config_credentials_hostname: '[[opentaco-public-base-hostname]]' |
| 25 | + cli_config_credentials_token: ${{ secrets.STATESMAN_TOKEN }} |
| 26 | +
|
| 27 | + - uses: diggerhq/digger@vLatest |
| 28 | + with: |
| 29 | + digger-spec: ${{ inputs.spec }} |
| 30 | + setup-aws: true |
| 31 | + setup-terraform: false |
| 32 | +``` |
| 33 | + |
| 34 | +In your terraform you need to specify the cloud block as specifed in the [docs](/ce/state-management/cloud-backend). Digger will then invoke terraform in an authenticated mode |
| 35 | +which means that it will be able to pull/push the state from statesman and perform the operations successfully. |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +<Note> |
| 40 | + In CI system we would expect a bit longer lived tokens so that rotation doesn't need to occur as often. For that you can temporarily set |
| 41 | + `OPENTACO_TERRAFORM_TOKEN_TTL="720h"` as an environment variable in the statesman service so that it doesn't expire soon when you place it in CI |
| 42 | +</Note> |
| 43 | + |
| 44 | +### Using digger S3 bucket only |
| 45 | + |
| 46 | +You can connect digger directly to an S3 bucket if you do not wish to install and configure statesman. This would be useful in cases where you don't |
| 47 | +have usecases for fine-grained access control or the other upcoming features such as remote runs by your users. Or maybe you have your state hosted somehwere |
| 48 | +and are not yet ready to migrate to statesman. |
| 49 | + |
| 50 | +The example repo for this is here: https://github.com/diggerhq/states-test |
| 51 | + |
| 52 | +In this example we have the following directory structure: |
| 53 | + |
| 54 | +``` |
| 55 | +dev/ |
| 56 | + main.tf |
| 57 | + tf_backend.tfbackend |
| 58 | +staging/ |
| 59 | + main.tf |
| 60 | + tf_backend.tfbackend |
| 61 | +prod/ |
| 62 | + main.tf |
| 63 | + tf_backend.tfbackend |
| 64 | +``` |
| 65 | + |
| 66 | +within each main.tf root module we define a backend block: |
| 67 | +``` |
| 68 | +terraform { |
| 69 | + backend "s3" { |
| 70 | +
|
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +We ommit the backend state name, key and region on purpose since it is defined in the file tf_backend.tfbackend within the same directory: |
| 76 | + |
| 77 | +``` |
| 78 | +bucket="digger-state-test" |
| 79 | +key="/dev/terraform.tfstate" |
| 80 | +region="us-east-1" |
| 81 | +``` |
| 82 | + |
| 83 | +This is done in staging/ and prod/ as well. We consider it as a convention for all the root modules. |
| 84 | + |
| 85 | +With that in place, we can configure digger to pass this additional configuration while running terraform as follows: |
| 86 | + |
| 87 | +``` |
| 88 | +projects: |
| 89 | + - name: "dev" |
| 90 | + dir: "dev" |
| 91 | + - name: "staging" |
| 92 | + dir: "staging" |
| 93 | + - name: "prod" |
| 94 | + dir: "prod" |
| 95 | +
|
| 96 | +
|
| 97 | +workflows: |
| 98 | + default: |
| 99 | + workflow_configuration: |
| 100 | + on_pull_request_pushed: ["digger plan"] |
| 101 | + on_pull_request_closed: ["digger unlock"] |
| 102 | + on_commit_to_default: ["digger unlock"] |
| 103 | +
|
| 104 | + plan: |
| 105 | + steps: |
| 106 | + - init: |
| 107 | + extra_args: ["-backend-config=tf_backend.tfbackend" ] |
| 108 | + - plan: |
| 109 | + apply: |
| 110 | + steps: |
| 111 | + - init: |
| 112 | + extra_args: ["-backend-config=tf_backend.tfbackend" ] |
| 113 | + - apply: |
| 114 | +``` |
| 115 | + |
| 116 | +The key part here is that we override the default workflow and pass extra arguments of `-backend-config=tf_backend.tfbackend` to the `plan` and `apply` steps. |
| 117 | +In this way it is easy to add additional states simply by adding a record for them in digger.yml. Once a PR is created and applied we will end up with a bucket that has three state files: |
| 118 | + |
| 119 | + |
0 commit comments