---
title: Tanzu Application PlatformでPrivate Git Repository上のソースコードをデプロイする
tags: ["Kubernetes", "Cartographer", "Tanzu", "TAP"]
categories: ["Dev", "CaaS", "Kubernetes", "TAP"]
date: 2022-09-12T04:50:35Z
updated: 2022-09-12T08:46:47Z
---

Tanzu Application Platform (TAP) で、Private Git Repository上のソースコードからアプリをデプロイする際のメモ。

> ⚠️ `ootb_supply_chain_basic.gitops.commit_strategy` が `pull_request` の場合、この通りではありません。


ドキュメントは次を参照。

* https://docs.vmware.com/en/VMware-Tanzu-Application-Platform/1.2/tap/GUID-scc-building-from-source.html#private-gitrepository-1
* https://docs.vmware.com/en/VMware-Tanzu-Application-Platform/1.2/tap/GUID-scc-git-auth.html


ここではsshではなく、basic認証を使用します。

**目次**
<!-- toc -->

### GitHubの場合

次の図の様にrepoに対するアクセストークンを作成します。

<img width="1024" alt="image" src="https://user-images.githubusercontent.com/106908/189514504-5b13ed5b-a859-4866-befa-fefd5dab2d36.png">


次のSecretを対象のnamespaceに作成します。

```yaml
GIT_SERVER=github.com
GIT_USERNAME=making
GIT_PASSWORD=ghp_Nnh*******

cat <<EOF > git-basic.yaml
apiVersion: v1
kind: Secret
metadata:
  name: git-basic
  annotations:
    tekton.dev/git-0: https://${GIT_SERVER}
type: kubernetes.io/basic-auth
stringData:
  username: ${GIT_USERNAME}
  password: ${GIT_PASSWORD}
EOF

kubectl apply -f git-basic.yaml -n demo
```

Workloadが使用するService AccountにもこのSecretを設定します。

```
kubectl patch serviceaccount default -p "{\"secrets\":[{\"name\":\"git-basic\"}]}" -n demo
```

Private Git Repository上のソースコードを用意します。ここでは次のリポジトリを使用します。

https://github.com/making/helloworld


<img width="1024" alt="image" src="https://user-images.githubusercontent.com/106908/189575501-5a583455-40fc-49de-b5b6-99eae38ce7d5.png">


次のようにWorkload作成時に、`gitops_ssh_secret`パラメータにこのSecretを渡します。


```
tanzu apps workload apply helloworld \
  --app helloworld \
  --git-repo https://github.com/making/helloworld \
  --git-branch main \
  --type web \
  --param gitops_ssh_secret=git-basic \
  --build-env BP_JVM_VERSION=17 \
  -n demo \
  -y
```

> TAPインストール時の`tap-values.yml`の`ootb_supply_chain_basic.gitops.ssh_secret`に`gitops_ssh_secret`パラメータのデフォルト値を設定できます


gitrepoリソースを見て、READYがTrueになっていればソースコードを取得できています。

```
$ kubectl get gitrepo -n demo helloworld 
NAME         URL                                    READY   STATUS                                                            AGE
helloworld   https://github.com/making/helloworld   True    Fetched revision: main/78e806b95a842748c1f6f3db13212330e120f80d   26s
```

Workloadが無事デプロイされればOKです。

```
$ tanzu apps workload get -n demo helloworld
---
# helloworld: Ready
---
Source
type:     git
url:      https://github.com/making/helloworld
branch:   main

Supply Chain
name:          source-to-url
last update:   94s
ready:         True

RESOURCE           READY   TIME
source-provider    True    10m
deliverable        True    10m
image-provider     True    2m3s
config-provider    True    114s
app-config         True    114s
config-writer      True    94s

Issues
No issues reported.

Pods
NAME                                           STATUS      RESTARTS   AGE
helloworld-00001-deployment-58f7fd4764-2m5dx   Running     0          49s
helloworld-build-1-build-pod                   Succeeded   0          10m
helloworld-config-writer-cgv7d-pod             Succeeded   0          110s

Knative Services
NAME         READY   URL
helloworld   Ready   http://helloworld-demo.vcap.me

To see logs: "tanzu apps workload tail helloworld --namespace demo"
```


### Bitbucketの場合

次の図の様にrepoに対するアクセストークンを作成します。

<img width="1024" alt="image" src="https://user-images.githubusercontent.com/106908/189605008-4d947ccf-6d59-4d04-90a9-b711de3539f7.png">

> Git Repositoryからソースコードを取得するだけであれば、Read権限だけでも良いですが、このSecretはGitOpsでmanifestを書き込む場合にも使用されるので、Write権限もあった方が良いです。

次のSecretを対象のnamespaceに作成します。

```yaml
GIT_SERVER=bitbucket.org
GIT_USERNAME=tmaki_vmware
GIT_PASSWORD=ATBBg************

cat <<EOF > git-basic.yaml
apiVersion: v1
kind: Secret
metadata:
  name: git-basic
  annotations:
    tekton.dev/git-0: https://${GIT_SERVER}
type: kubernetes.io/basic-auth
stringData:
  username: ${GIT_USERNAME}
  password: ${GIT_PASSWORD}
EOF

kubectl apply -f git-basic.yaml -n demo
```

Workloadが使用するService AccountにもこのSecretを設定します。

```
kubectl patch serviceaccount default -p "{\"secrets\":[{\"name\":\"git-basic\"}]}" -n demo
```

Private Git Repository上のソースコードを用意します。ここでは次のリポジトリを使用します。

https://bitbucket.org/tmaki_vmware/helloworld


<img width="1024" alt="image" src="https://user-images.githubusercontent.com/106908/189605706-ed41fe04-4ee7-4c3f-8d65-22af11dff2d0.png">


次のようにWorkload作成時に、`gitops_ssh_secret`パラメータにこのSecretを渡します。


```
tanzu apps workload apply helloworld \
  --app helloworld \
  --git-repo https://bitbucket.org/tmaki_vmware/helloworld \
  --git-branch main \
  --type web \
  --param gitops_ssh_secret=git-basic \
  --build-env BP_JVM_VERSION=17 \
  -n demo \
  -y
```

> TAPインストール時の`tap-values.yml`の`ootb_supply_chain_basic.gitops.ssh_secret`に`gitops_ssh_secret`パラメータのデフォルト値を設定できます


gitrepoリソースを見て、READYがTrueになっていればソースコードを取得できています。

```
$ kubectl get gitrepo -n demo helloworld 
NAME         URL                                             READY   STATUS                                                            AGE
helloworld   https://bitbucket.org/tmaki_vmware/helloworld   True    Fetched revision: main/78e806b95a842748c1f6f3db13212330e120f80d   26s
```


Workloadが無事デプロイされればOKです。

```
$ tanzu apps workload get -n demo helloworld
---
# helloworld: Ready
---
Source
type:     git
url:      https://bitbucket.org/tmaki_vmware/helloworld
branch:   main

Supply Chain
name:          source-to-url
last update:   94s
ready:         True

RESOURCE           READY   TIME
source-provider    True    10m
deliverable        True    10m
image-provider     True    2m3s
config-provider    True    114s
app-config         True    114s
config-writer      True    94s

Issues
No issues reported.

Pods
NAME                                           STATUS      RESTARTS   AGE
helloworld-00001-deployment-58f7fd4764-2m5dx   Running     0          49s
helloworld-build-1-build-pod                   Succeeded   0          10m
helloworld-config-writer-cgv7d-pod             Succeeded   0          110s

Knative Services
NAME         READY   URL
helloworld   Ready   http://helloworld-demo.vcap.me

To see logs: "tanzu apps workload tail helloworld --namespace demo"
```
