momo's Blog.

Atlantis使用

字数统计: 431阅读时长: 2 min
2023/02/21 Share

前言

通过git管理Terraform

安装

创建Gitlab Token

Role: maintainer

Score: api

创建WebHooks Token

随便生成一串随机字符

安装

完整yaml

  • deployment.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: atlantis
    labels:
    app: atlantis
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: atlantis
    template:
    metadata:
    annotations:
    checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
    labels:
    app: atlantis
    spec:
    containers:
    - name: atlantis
    image: ghcr.io/runatlantis/atlantis:{{.Values.version}} # 1. Replace <VERSION> with the most recent release.
    env:
    - name: ATLANTIS_REPO_ALLOWLIST
    value: {{ .Values.ATLANTIS_REPO_ALLOWLIST }} # 2. Replace this with your own repo allowlist.
    - name: ATLANTIS_GITLAB_USER
    value: git
    - name: ATLANTIS_GITLAB_HOSTNAME
    value: {{ .Values.ATLANTIS_GITLAB_HOSTNAME }}
    - name: TF_HTTP_USERNAME
    value: {{ .Values.TF_HTTP_USERNAME }}
    - name: TF_HTTP_PASSWORD
    value: {{ .Values.TF_HTTP_PASSWORD }}
    - name: ALICLOUD_ACCESS_KEY
    value: {{ .Values.ALICLOUD_ACCESS_KEY }}
    - name: ALICLOUD_SECRET_KEY
    value: {{ .Values.ALICLOUD_SECRET_KEY }}
    - name: ATLANTIS_AUTOMERGE
    value: "true"
    - name: ATLANTIS_REPO_CONFIG
    value: /work/config/repos.yaml
    - name: ATLANTIS_GITLAB_TOKEN
    valueFrom:
    secretKeyRef:
    name: atlantis-vcs
    key: token
    - name: ATLANTIS_GITLAB_WEBHOOK_SECRET
    valueFrom:
    secretKeyRef:
    name: atlantis-vcs
    key: webhook-secret

    - name: ATLANTIS_PORT
    value: "4141" # Kubernetes sets an ATLANTIS_PORT variable so we need to override.
    ports:
    - name: atlantis
    containerPort: 4141
    volumeMounts:
    - mountPath: /work/config
    name: config
    resources:
    requests:
    memory: 256Mi
    cpu: 100m
    limits:
    memory: 256Mi
    cpu: 100m
    livenessProbe:
    # We only need to check every 60s since Atlantis is not a
    # high-throughput service.
    periodSeconds: 60
    httpGet:
    path: /healthz
    port: 4141
    # If using https, change this to HTTPS
    scheme: HTTP
    readinessProbe:
    periodSeconds: 60
    httpGet:
    path: /healthz
    port: 4141
    # If using https, change this to HTTPS
    scheme: HTTP
    volumes:
    - name: config
    projected:
    sources:
    - configMap:
    name: repos.yml
    ---
    apiVersion: v1
    kind: Service
    metadata:
    name: atlantis
    spec:
    type: NodePort
    ports:
    - name: atlantis
    port: 80
    targetPort: 4141
    nodePort: 31001
    selector:
    app: atlantis
  • configmap.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    apiVersion: v1
    kind: ConfigMap
    metadata:
    name: repos.yml
    data:
    # 类属性键;每一个键都映射到一个简单的值
    repos.yaml: |
    ---
    repos:
    - id: /.*/
    branch: /.*/
    apply_requirements: [approved, mergeable]
    import_requirements: [approved, mergeable]
    delete_source_branch_on_merge: true
  • token.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    apiVersion: v1
    data:
    token: {{ .Values.gitToken | b64enc }}
    webhook-secret: {{ .Values.webhookToken | b64enc }}
    kind: Secret
    metadata:
    name: atlantis-vcs
    type: Opaque
  • values.yaml

1
2
3
4
5
6
7
8
9
10
version: "v0.22.3"
ATLANTIS_REPO_ALLOWLIST: xxxxxxxxx/*
gitToken: "xxxxxxxxx"
webhookToken: "xxxxxxxxx"
ATLANTIS_GITLAB_HOSTNAME: "xxxxxxxxx"

TF_HTTP_USERNAME: "xxxxxxxxx"
TF_HTTP_PASSWORD: "xxxxxxxxx"
ALICLOUD_ACCESS_KEY: "xxxxxxxxx"
ALICLOUD_SECRET_KEY: "xxxxxxxxx"

配置webhook

Gitlab

API地址: http://$URL/events

  • Push events
  • Comments
  • Merge Request events

Provider 配置

配置直接在容器中映射环境变量即可;

完整yaml

CATALOG
  1. 1. 前言
  2. 2. 安装
    1. 2.1. 创建Gitlab Token
    2. 2.2. 创建WebHooks Token
    3. 2.3. 安装
      1. 2.3.1. 完整yaml
    4. 2.4. 配置webhook
    5. 2.5. Provider 配置
  3. 3. 完整yaml