Skip to content

Commit 43d7557

Browse files
authored
add ci integration docs (#2259)
1 parent 136dc4a commit 43d7557

File tree

3 files changed

+118
-83
lines changed

3 files changed

+118
-83
lines changed

docs/ce/howto/managing-state.mdx

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,119 @@
11
---
22
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+
![](/images/configuration/state-bucket-example.png)

docs/mint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"ce/state-management/cloud-backend",
5757
"ce/state-management/rbac",
5858
"ce/state-management/sso",
59+
"ce/state-management/digger-integration",
5960
"ce/state-management/development",
6061
"ce/state-management/analytics"
6162
]

0 commit comments

Comments
 (0)