diff --git a/share/gitea-tekton-org/datas.tf b/share/gitea-tekton-org/datas.tf index 0ea324e..cb16bf1 100644 --- a/share/gitea-tekton-org/datas.tf +++ b/share/gitea-tekton-org/datas.tf @@ -40,6 +40,43 @@ data "kustomization_overlay" "data" { - op: add path: /spec/params/2/default value: ${var.domain_name} + - op: add + path: /spec/params/3/default + value: ${jsonencode(var.stages)} + EOF + } + patches { + target { + kind = "Task" + name = "auto-ci-activate" + } + patch = <<-EOF + - op: replace + path: /spec/params/1/default + value: ${var.issuer} + - op: add + path: /spec/params/2/default + value: ${var.domain_name} + - op: add + path: /spec/params/3/default + value: ${jsonencode(var.stages)} + EOF + } + patches { + target { + kind = "Task" + name = "auto-ci-delete" + } + patch = <<-EOF + - op: replace + path: /spec/params/1/default + value: ${var.issuer} + - op: add + path: /spec/params/2/default + value: ${var.domain_name} + - op: add + path: /spec/params/3/default + value: ${jsonencode(var.stages)} EOF } patches { diff --git a/share/gitea-tekton-org/index.yaml b/share/gitea-tekton-org/index.yaml index 9ac89ae..5d90b80 100644 --- a/share/gitea-tekton-org/index.yaml +++ b/share/gitea-tekton-org/index.yaml @@ -4,7 +4,7 @@ kind: Component category: share metadata: name: gitea-tekton-org - description: null + description: Setup tekton-cicd base objects and set the webhook for gitea options: autoCD: default: true @@ -36,6 +36,11 @@ options: examples: - your-org type: string + stages: + default: [] + items: + type: string + type: array storage: default: volume: diff --git a/share/gitea-tekton-org/tekton.dev_v1_Pipeline_auto-ci-push.yaml b/share/gitea-tekton-org/tekton.dev_v1_Pipeline_auto-ci-push.yaml index 39a1eb2..3266799 100644 --- a/share/gitea-tekton-org/tekton.dev_v1_Pipeline_auto-ci-push.yaml +++ b/share/gitea-tekton-org/tekton.dev_v1_Pipeline_auto-ci-push.yaml @@ -34,7 +34,7 @@ spec: finally: - name: cleanup taskRef: - name: auto-ci-cleaup + name: auto-ci-cleanup workspaces: - name: source - name: inform-gitea-success diff --git a/share/gitea-tekton-org/tekton.dev_v1_Pipeline_auto-ci-tag.yaml b/share/gitea-tekton-org/tekton.dev_v1_Pipeline_auto-ci-tag.yaml index 94079cb..54557fb 100644 --- a/share/gitea-tekton-org/tekton.dev_v1_Pipeline_auto-ci-tag.yaml +++ b/share/gitea-tekton-org/tekton.dev_v1_Pipeline_auto-ci-tag.yaml @@ -31,7 +31,7 @@ spec: finally: - name: cleanup taskRef: - name: auto-ci-cleaup + name: auto-ci-cleanup workspaces: - name: source - name: inform-gitea-success diff --git a/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-activate.yaml b/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-activate.yaml new file mode 100644 index 0000000..2b3fbb0 --- /dev/null +++ b/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-activate.yaml @@ -0,0 +1,77 @@ +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: auto-ci-activate +spec: + params: + - name: toolbox-image + default: sebt3/basic-toolbox-image:1.30.0 + description: The name of the toolbox image + type: string + - name: issuer-name + default: letsencrypt-prod + type: string + - name: domain-name + type: string + - name: stages + type: array + - name: artifactory-url + description: The url of the current artifactory + - name: project-name + description: The git repository name + - name: project-path + description: The path of the current project + - name: git-repository-url + description: The git repository url + - name: deploy-url + description: The git repository url for the deploy project + steps: + - name: activate + image: $(params.toolbox-image) + workingDir: $(workspaces.source.path) + args: + - $(params.stages) + env: + - name: ARTIFACTORY_URL + value: $(params.artifactory-url) + - name: DOMAIN_NAME + value: $(params.domain-name) + - name: ISSUER_NAME + value: $(params.issuer-name) + - name: PROJECT_NAME + value: $(params.project-name) + - name: PROJECT_PATH + value: $(params.project-path) + - name: GIT_REPOSITORY_URL + value: $(params.git-repository-url) + - name: DEPLOY_URL + value: $(params.deploy-url) + - name: TEMPLATE_ROOT + value: /etc/templates + - name: WORKSPACE_SSH_DIRECTORY_PATH + value: $(workspaces.ssh-directory.path) + script: |- + #!/usr/bin/env bash + . ${TEMPLATE_ROOT}/functions.sh + git_prepare "$(params.deploy-url)" "Auto CD" "autocd@$(params.domain-name)" + install_base + create_prj + activate_prj + git_push "Activating $(params.project-name)" + cleanup + volumeMounts: + - mountPath: /etc/templates + name: templates + volumes: + - name: templates + configmap: + name: auto-cd-templates + workspaces: + - name: source + mountPath: /data + - description: | + A .ssh directory with private key, known_hosts, config, etc. Copied to + the user's home before git commands are executed. Used to authenticate + with the git remote when performing the clone. Binding a Secret to this + Workspace is strongly recommended over other volume types. + name: ssh-directory \ No newline at end of file diff --git a/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-cleanup.yaml b/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-cleanup.yaml index 689bab6..8a3846f 100644 --- a/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-cleanup.yaml +++ b/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-cleanup.yaml @@ -13,7 +13,7 @@ spec: image: $(params.toolbox-image) workingDir: $(workspaces.source.path) script: |- - #!/usr/bin/env ash + #!/usr/bin/env bash rm -rf . || true workspaces: - name: source diff --git a/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-create.yaml b/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-create.yaml index eb1c87d..b364bb0 100644 --- a/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-create.yaml +++ b/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-create.yaml @@ -13,6 +13,8 @@ spec: type: string - name: domain-name type: string + - name: stages + type: array - name: artifactory-url description: The url of the current artifactory - name: project-name @@ -24,71 +26,45 @@ spec: - name: deploy-url description: The git repository url for the deploy project steps: - - name: cleanup + - name: create image: $(params.toolbox-image) workingDir: $(workspaces.source.path) + args: + - $(params.stages) env: - name: ARTIFACTORY_URL value: $(params.artifactory-url) - - name: PROJECT_NAME - value: $(params.project-name) - name: DOMAIN_NAME value: $(params.domain-name) - name: ISSUER_NAME value: $(params.issuer-name) + - name: PROJECT_NAME + value: $(params.project-name) - name: PROJECT_PATH value: $(params.project-path) - name: GIT_REPOSITORY_URL value: $(params.git-repository-url) - name: DEPLOY_URL value: $(params.deploy-url) + - name: TEMPLATE_ROOT + value: /etc/templates + - name: WORKSPACE_SSH_DIRECTORY_PATH + value: $(workspaces.ssh-directory.path) script: |- #!/usr/bin/env bash - git clone "$(params.deploy-url)" --depth 1 . - mkdir -p "projects/$(params.project-name)" bases/project bases/install bases/deploy - added=0 - copy() { - local src=/etc/templates/$1 dest=$2 - if [ ! -f $dest ];then - cp "$src" "$dest" - git add "$dest" - added=$(($added+1)) - fi - } - template() { - local src=/etc/templates/$1 dest=$2 - if [ ! -f $dest ];then - envsubst <"$src" >"$dest" - git add "$dest" - added=$(($added+1)) - fi - } - template base-update.yaml.tmpl bases/project/base-update.yaml - copy base-repo.yaml bases/project/repo.yaml - copy base-cert.yaml bases/project/cert.yaml - copy base-ingress.yaml bases/project/ingress.yaml - copy base-policy.yaml bases/project/policy.yaml - copy base-deploy.yaml bases/project/deploy.yaml - copy base-secret.yaml bases/project/secret.yaml - copy base-config.yaml bases/project/config.yaml - copy base-service.yaml bases/project/service.yaml - copy base-kusto.yaml bases/project/kustomization.yaml - copy install-install.yaml bases/install/install.yaml - copy install-kusto.yaml bases/install/kustomization.yaml - copy deploy-kusto.yaml bases/deploy/kustomization.yaml - template deploy-repo.yaml.tmpl bases/deploy/repo.yaml - template project-kusto.yaml.tmpl "projects/$(params.project-name)/kustomization.yaml" - if [ $added -ne 0 ];then - git commit -am "Adding $(params.project-name)" - git push - fi + . ${TEMPLATE_ROOT}/functions.sh + git_prepare "$(params.deploy-url)" "Auto CD" "autocd@$(params.domain-name)" + install_base + create_prj + git_push "Adding $(params.project-name)" + cleanup volumeMounts: - mountPath: /etc/templates name: templates volumes: - name: templates configmap: - name: auto-cd-create-templates + name: auto-cd-templates workspaces: - name: source mountPath: /data @@ -97,5 +73,4 @@ spec: the user's home before git commands are executed. Used to authenticate with the git remote when performing the clone. Binding a Secret to this Workspace is strongly recommended over other volume types. - name: ssh-directory - optional: true \ No newline at end of file + name: ssh-directory \ No newline at end of file diff --git a/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-delete.yaml b/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-delete.yaml new file mode 100644 index 0000000..efddf50 --- /dev/null +++ b/share/gitea-tekton-org/tekton.dev_v1_Task_auto-ci-delete.yaml @@ -0,0 +1,75 @@ +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: auto-ci-delete +spec: + params: + - name: toolbox-image + default: sebt3/basic-toolbox-image:1.30.0 + description: The name of the toolbox image + type: string + - name: issuer-name + default: letsencrypt-prod + type: string + - name: domain-name + type: string + - name: stages + type: array + - name: artifactory-url + description: The url of the current artifactory + - name: project-name + description: The git repository name + - name: project-path + description: The path of the current project + - name: git-repository-url + description: The git repository url + - name: deploy-url + description: The git repository url for the deploy project + steps: + - name: delete + image: $(params.toolbox-image) + workingDir: $(workspaces.source.path) + args: + - $(params.stages) + env: + - name: ARTIFACTORY_URL + value: $(params.artifactory-url) + - name: DOMAIN_NAME + value: $(params.domain-name) + - name: ISSUER_NAME + value: $(params.issuer-name) + - name: PROJECT_NAME + value: $(params.project-name) + - name: PROJECT_PATH + value: $(params.project-path) + - name: GIT_REPOSITORY_URL + value: $(params.git-repository-url) + - name: DEPLOY_URL + value: $(params.deploy-url) + - name: TEMPLATE_ROOT + value: /etc/templates + - name: WORKSPACE_SSH_DIRECTORY_PATH + value: $(workspaces.ssh-directory.path) + script: |- + #!/usr/bin/env bash + . ${TEMPLATE_ROOT}/functions.sh + git_prepare "$(params.deploy-url)" "Auto CD" "autocd@$(params.domain-name)" + delete_prj + git_push "Removing $(params.project-name)" + cleanup + volumeMounts: + - mountPath: /etc/templates + name: templates + volumes: + - name: templates + configmap: + name: auto-cd-templates + workspaces: + - name: source + mountPath: /data + - description: | + A .ssh directory with private key, known_hosts, config, etc. Copied to + the user's home before git commands are executed. Used to authenticate + with the git remote when performing the clone. Binding a Secret to this + Workspace is strongly recommended over other volume types. + name: ssh-directory \ No newline at end of file diff --git a/share/gitea-tekton-org/v1_ConfigMap_auto-cd-create-templates.yaml b/share/gitea-tekton-org/v1_ConfigMap_auto-cd-templates.yaml similarity index 75% rename from share/gitea-tekton-org/v1_ConfigMap_auto-cd-create-templates.yaml rename to share/gitea-tekton-org/v1_ConfigMap_auto-cd-templates.yaml index 51ae266..377014b 100644 --- a/share/gitea-tekton-org/v1_ConfigMap_auto-cd-create-templates.yaml +++ b/share/gitea-tekton-org/v1_ConfigMap_auto-cd-templates.yaml @@ -1,8 +1,72 @@ apiVersion: v1 kind: ConfigMap metadata: - name: auto-cd-create-templates + name: auto-cd-templates data: + functions.sh: |- + added=0 + copy() { + local src=${TEMPLATE_ROOT}/$1 dest=$2 + if [ ! -f $dest ];then + cp "$src" "$dest" + git add "$dest" + added=$(($added+1)) + fi + } + template() { + local src=${TEMPLATE_ROOT}/$1 dest=$2 + if [ ! -f $dest ];then + envsubst <"$src" >"$dest" + git add "$dest" + added=$(($added+1)) + fi + } + git_push() { + local message=$1 + if [ $added -ne 0 ];then + git commit -am "$message" + git push + fi + } + cleanup() { + rm -rf . || true + } + git_prepare() { + local url=$1 username=$2 email=$3 + mkdir -p "${HOME}/.ssh" + cp -Rv "${WORKSPACE_SSH_DIRECTORY_PATH}" "${HOME}/.ssh" + chmod 700 "${HOME}/.ssh" + chmod 400 "${HOME}/.ssh"/* + git config --global user.name "$username" + git config --global user.email "$email" + git clone "$url" --depth 1 . + } + install_base() { + mkdir -p bases/project bases/install bases/deploy + template base-update.yaml.tmpl bases/project/base-update.yaml + copy base-repo.yaml bases/project/repo.yaml + copy base-cert.yaml bases/project/cert.yaml + copy base-ingress.yaml bases/project/ingress.yaml + copy base-policy.yaml bases/project/policy.yaml + copy base-deploy.yaml bases/project/deploy.yaml + copy base-secret.yaml bases/project/secret.yaml + copy base-config.yaml bases/project/config.yaml + copy base-service.yaml bases/project/service.yaml + copy base-kusto.yaml bases/project/kustomization.yaml + copy install-install.yaml bases/install/install.yaml + copy install-kusto.yaml bases/install/kustomization.yaml + copy deploy-kusto.yaml bases/deploy/kustomization.yaml + template deploy-repo.yaml.tmpl bases/deploy/repo.yaml + } + create_prj() { + mkdir -p "projects/${PROJECT_NAME}" + template project-kusto.yaml.tmpl "projects/${PROJECT_NAME}/kustomization.yaml" + } + activate_prj() { + } + delete_prj() { + rm -rf "projects/${PROJECT_NAME}" + } base-kusto.yaml: |- --- apiVersion: kustomize.config.k8s.io/v1beta1