From df47c21265f945c114231b1818444df769005662 Mon Sep 17 00:00:00 2001 From: Nikolai Rodionov Date: Sat, 25 Feb 2023 21:20:07 +0100 Subject: [PATCH] Ready --- .drone.yml | 22 +++++++++ .../index.md | 47 ++++++++++++++++++- kube/main.yaml | 8 ++++ kube/template.yaml | 1 + 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 kube/main.yaml diff --git a/.drone.yml b/.drone.yml index 2c91fa0..684124e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -65,6 +65,28 @@ steps: - yq '.spec.generators[].list.elements += load("/tmp/elements.yaml")' /tmp/clean_appset.yaml > /tmp/new_appset.yaml - kubectl apply -f /tmp/new_appset.yaml +- name: Deploy a main ApplicationSet + image: alpine/k8s:1.24.10 + when: + branch: + - main + environment: + KUBECONFIG_CONTENT: + from_secret: KUBECONFIG_CONTENT + commands: + - mkdir $HOME/.kube + - echo $KUBECONFIG_CONTENT | base64 -d > $HOME/.kube/config + - apk update --no-cache && apk add yq gettext + - export ARGO_APP_CHART_VERSION=`cat chart/Chart.yaml | yq '.version'` + - export ARGO_APP_BRANCH=$DRONE_BRANCH + - export ARGO_APP_IMAGE_TAG=$DRONE_COMMIT_SHA + - kubectl get -f ./kube/applicationset.yaml -o yaml > /tmp/old_appset.yaml + - yq "del(.spec.generators[].list.elements[] | select(.name == \"$ARGO_APP_BRANCH\"))" /tmp/old_appset.yaml > /tmp/clean_appset1.yaml + - yq "del(.spec.generators[].list.elements[] | select(.commit_sha == \"$ARGO_APP_IMAGE_TAG\"))" /tmp/clean_appset1.yaml > /tmp/clean_appset.yaml + - envsubst < ./kube/template.yaml > /tmp/elements.yaml + - yq '.spec.generators[].list.elements += load("/tmp/elements.yaml")' /tmp/clean_appset.yaml > /tmp/new_appset.yaml + - kubectl apply -f /tmp/new_appset.yaml + - name: Sync application image: argoproj/argocd environment: diff --git a/content/posts/argocd-dynamic-environment-per-branch-part-1/index.md b/content/posts/argocd-dynamic-environment-per-branch-part-1/index.md index fadb280..d7239e3 100644 --- a/content/posts/argocd-dynamic-environment-per-branch-part-1/index.md +++ b/content/posts/argocd-dynamic-environment-per-branch-part-1/index.md @@ -376,6 +376,49 @@ And now let's create a job like that: - argocd app wait -l app=badhouseplants -l branch=$DRONE_BRANCH ``` -And the last step would be to remove an application when branch is removed. +And the last step would be to remove an application when branch is removed. It could be easy with `Gitlab` because there you can use `environments` and `triggers` for removing branch *(as I remember)* But with drone it can be harder. Because drone won't be triggered by a removed branch. So I has to be an additional step for the `main` pipeline. + +I'm always using squash commits that means that after merging a Pull Request the commit will have the same `SHA`. So when merging to the main branch, I can use the commit hash to remove a generator. + +So I've created a file `./kube/main-template.yaml`, that looks like that: +```YAML +- name: application + app: badhouseplants + branch: main + chart_version: $ARGO_APP_CHART_VERSION + value: | + hugo: + image: + tag: $ARGO_APP_IMAGE_TAG +``` + +And a job: +```YAML +- name: Deploy a main ApplicationSet + image: alpine/k8s:1.24.10 + when: + branch: + - main + environment: + KUBECONFIG_CONTENT: + from_secret: KUBECONFIG_CONTENT + commands: + - mkdir $HOME/.kube + - echo $KUBECONFIG_CONTENT | base64 -d > $HOME/.kube/config + - apk update --no-cache && apk add yq gettext + - export ARGO_APP_CHART_VERSION=`cat chart/Chart.yaml | yq '.version'` + - export ARGO_APP_BRANCH=$DRONE_BRANCH + - export ARGO_APP_IMAGE_TAG=$DRONE_COMMIT_SHA + - kubectl get -f ./kube/applicationset.yaml -o yaml > /tmp/old_appset.yaml + - yq "del(.spec.generators[].list.elements[] | select(.name == \"$ARGO_APP_BRANCH\"))" /tmp/old_appset.yaml > /tmp/clean_appset1.yaml + - yq "del(.spec.generators[].list.elements[] | select(.commit_sha == \"$ARGO_APP_IMAGE_TAG\"))" /tmp/clean_appset1.yaml > /tmp/clean_appset.yaml + - envsubst < ./kube/main.yaml > /tmp/elements.yaml + - yq '.spec.generators[].list.elements += load("/tmp/elements.yaml")' /tmp/clean_appset.yaml > /tmp/new_appset.yaml + - kubectl apply -f /tmp/new_appset.yaml + +``` + +Then I just need to upgrade `./kube/template.yaml`, so it contains `commit_sha: $ARGO_APP_IMAGE_TAG`. + +> Also, I've found out that `ArgoCD` won't remove a namespace if it was created by a `SyncPolicy`, so I've added it to the helm chart, and add a new `value` to provide a name. -> Also, I've found out that `ArgoCD` won't remove a namespace if it was created by a `SyncPolicy`, so I've added it to the helm chart, and add a new `value` to provide a name. \ No newline at end of file diff --git a/kube/main.yaml b/kube/main.yaml new file mode 100644 index 0000000..8a5c1c6 --- /dev/null +++ b/kube/main.yaml @@ -0,0 +1,8 @@ +- name: application + app: badhouseplants + branch: $ARGO_APP_BRANCH + chart_version: $ARGO_APP_CHART_VERSION + value: | + hugo: + image: + tag: $ARGO_APP_IMAGE_TAG diff --git a/kube/template.yaml b/kube/template.yaml index 5a06c79..b2e7bcd 100644 --- a/kube/template.yaml +++ b/kube/template.yaml @@ -1,6 +1,7 @@ - name: $ARGO_APP_BRANCH app: badhouseplants branch: $ARGO_APP_BRANCH + commit_sha: $ARGO_APP_IMAGE_TAG chart_version: $ARGO_APP_CHART_VERSION value: | namespace: