From b5e304bc3cf865d9fa5919c802d389a0566462f2 Mon Sep 17 00:00:00 2001 From: Nikolai Rodionov Date: Mon, 24 Apr 2023 18:01:13 +0200 Subject: [PATCH] Use a custom image for building and more scripts --- .drone.yml | 67 ++++++++++++++++++-------------------- Dockerfile | 1 - kube/application.yaml | 52 +++++++++++++++++++++++++++++ kube/values-main.yaml | 24 ++++++++++++++ kube/values-preview.yaml | 32 ++++++++++++++++++ scripts/build-container.pl | 32 ++++++++++++++++++ scripts/check.pl | 6 ---- scripts/cleanup.pl | 2 +- scripts/deploy-app.pl | 27 +++++++++++++++ scripts/upload-media.pl | 18 +++++++--- 10 files changed, 213 insertions(+), 48 deletions(-) create mode 100644 kube/application.yaml create mode 100644 kube/values-main.yaml create mode 100644 kube/values-preview.yaml create mode 100755 scripts/build-container.pl delete mode 100755 scripts/check.pl create mode 100755 scripts/deploy-app.pl diff --git a/.drone.yml b/.drone.yml index 0d43941..6ee51f5 100644 --- a/.drone.yml +++ b/.drone.yml @@ -32,62 +32,46 @@ name: Build badhouseplants.net trigger: event: - push +clone: + disable: true steps: - - name: Download submodules with themes and remove lfs + - name: clone image: alpine/git + environment: + GIT_LFS_SKIP_SMUDGE: 1 commands: - - git submodule update --init --recursive - - rm -rf static + - git clone $DRONE_REMOTE_URL --recurse-submodules . + - git checkout $DRONE_COMMIT - name: Test a build image: git.badhouseplants.net/badhouseplants/hugo-builder + depends_on: + - clone commands: - hugo - + - name: Build and push the docker image - image: plugins/docker - when: - branch: - exclude: - - main - settings: - registry: git.badhouseplants.net - username: allanger - password: + image: git.badhouseplants.net/badhouseplants/badhouseplants-builder:80ffd53372652576fa3c36a56b351b448a025c6a + privileged: true + depends_on: + - Test a build + environment: + GITEA_TOKEN: from_secret: GITEA_TOKEN - repo: git.badhouseplants.net/${DRONE_REPO} - tags: ${DRONE_COMMIT_SHA} - - - name: Build and push the docker image with latest - image: plugins/docker - when: - branch: - - main - settings: - registry: git.badhouseplants.net - username: allanger - password: - from_secret: GITEA_TOKEN - repo: git.badhouseplants.net/${DRONE_REPO} - tags: - - ${DRONE_COMMIT_SHA} - - latest - - - name: Download media files - image: alpine/git commands: - - git lfs pull + - ./scripts/build-container.pl - name: Sync pictures from lfs to Minio - image: rclone/rclone:latest + image: git.badhouseplants.net/badhouseplants/badhouseplants-builder:80ffd53372652576fa3c36a56b351b448a025c6a + depends_on: + - Test a build environment: RCLONE_CONFIG_CONTENT: from_secret: RCLONE_CONFIG_CONTENT_PRIVATE RCLONE_CONFIG: /tmp/rclone.conf commands: - echo "$RCLONE_CONFIG_CONTENT" > $RCLONE_CONFIG - - apk update && apk add git perl - ./scripts/upload-media.pl - name: Deploy a preview ApplicationSet @@ -96,6 +80,9 @@ steps: branch: exclude: - main + depends_on: + - Build and push the docker image + - Sync pictures from lfs to Minio environment: KUBECONFIG_CONTENT: from_secret: KUBECONFIG_CONTENT @@ -123,6 +110,9 @@ steps: when: branch: - main + depends_on: + - Build and push the docker image + - Sync pictures from lfs to Minio environment: KUBECONFIG_CONTENT: from_secret: KUBECONFIG_CONTENT @@ -151,6 +141,9 @@ steps: - name: Sync application image: argoproj/argocd + depends_on: + - Deploy a main ApplicationSet + - Deploy a preview ApplicationSet environment: ARGOCD_SERVER: from_secret: ARGOCD_SERVER @@ -162,6 +155,8 @@ steps: - name: Cleanup everything image: rclone/rclone:latest + depends_on: + - Sync application environment: RCLONE_CONFIG_CONTENT: from_secret: RCLONE_CONFIG_CONTENT_PRIVATE diff --git a/Dockerfile b/Dockerfile index 5597fbc..388a6cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,4 @@ FROM git.badhouseplants.net/badhouseplants/hugo-builder WORKDIR /src COPY . /src -RUN ls -la /src ENTRYPOINT ["hugo"] diff --git a/kube/application.yaml b/kube/application.yaml new file mode 100644 index 0000000..aca1874 --- /dev/null +++ b/kube/application.yaml @@ -0,0 +1,52 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + labels: + application: $APPLICATON + branch: $GIT_BRANCH + commit_sha: $GIT_COMMIT_SHA + name: $APPLICATON-$GIT_BRANCH + namespace: argo-system +spec: + destination: + namespace: $APPLICATON-$GIT_BRANCH + server: https://kubernetes.default.svc + project: default + source: + chart: badhouseplants-net + targetRevision: $HELM_CHART_VERSION + helm: + valueFiles: + - values.yaml + values: | + namespace: + name: badhouseplants-create-wubs-in-vital + labels: + istio-injection: disabled + istio: + hosts: + - create-wubs-in-vital-dev.badhouseplants.net + annotations: + link.argocd.argoproj.io/env: https://create-wubs-in-vital-dev.badhouseplants.net/ + link.argocd.argoproj.io/remark42: https://remark42-create-wubs-in-vital-dev.badhouseplants.net/web + link.argocd.argoproj.io/build: DRONE_BUILD_LINK + hugo: + image: + tag: ee4efc6107541657f405a3dd9b7ee238d0e69e7b + baseURL: https://create-wubs-in-vital-dev.badhouseplants.net/ + buildDrafts: true + env: + HUGO_PARAMS_GITBRANCH: create-wubs-in-vital + HUGO_PARAMS_REMARK42URL: https://remark42-create-wubs-in-vital-dev.badhouseplants.net + remark42: + istio: + hosts: + - remark42-create-wubs-in-vital-dev.badhouseplants.net + settings: + url: https://remark42-create-wubs-in-vital-dev.badhouseplants.net/ + auth: + anonymous: true + secretKey: b8a41781b2c1ba2ecf49e4e7 + rclone: + command: "rclone copy -P badhouseplants-public:/badhouseplants-net/ee4efc6107541657f405a3dd9b7ee238d0e69e7b /static" + repoURL: https://git.badhouseplants.net/api/packages/badhouseplants/helm diff --git a/kube/values-main.yaml b/kube/values-main.yaml new file mode 100644 index 0000000..1d90eba --- /dev/null +++ b/kube/values-main.yaml @@ -0,0 +1,24 @@ +- value: | + hugo: + image: + tag: $ARGO_APP_IMAGE_TAG + istio: + annotations: + link.argocd.argoproj.io/env: https://badhouseplants.net/ + link.argocd.argoproj.io/build: $DRONE_BUILD_LINK + link.argocd.argoproj.io/remark42: https://remark42.badhouseplants.net/web + remark42: + settings: + secret: $ARGO_REMARK_SECRET + oauth: + github: + enabled: true + key: 1fb3d33a62b074bc0ea0 + secret: $ARGO_GITHUB_OAUTH_KEY + google: + enabled: true + key: 966815964055-m9t7leuj1arqimqu2pvbs1k10cg54gcg.apps.googleusercontent.com + secret: $ARGO_GOOGLE_OAUTH_KEY + storage: + requestedSize: 300Mi + diff --git a/kube/values-preview.yaml b/kube/values-preview.yaml new file mode 100644 index 0000000..46b774d --- /dev/null +++ b/kube/values-preview.yaml @@ -0,0 +1,32 @@ +--- +- value: | + namespace: + name: badhouseplants-$ARGO_APP_BRANCH + labels: + istio-injection: disabled + istio: + hosts: + - $ARGO_APP_HOSTNAME + annotations: + link.argocd.argoproj.io/env: https://$ARGO_APP_HOSTNAME/ + link.argocd.argoproj.io/remark42: https://remark42-$ARGO_APP_HOSTNAME/web + link.argocd.argoproj.io/build: $DRONE_BUILD_LINK + hugo: + image: + tag: $ARGO_APP_IMAGE_TAG + baseURL: https://$ARGO_APP_HOSTNAME/ + buildDrafts: true + env: + HUGO_PARAMS_GITBRANCH: $ARGO_APP_BRANCH + HUGO_PARAMS_REMARK42URL: https://remark42-$ARGO_APP_HOSTNAME + remark42: + istio: + hosts: + - remark42-$ARGO_APP_HOSTNAME + settings: + url: https://remark42-$ARGO_APP_HOSTNAME/ + auth: + anonymous: true + secretKey: $ARGO_REMARK42_SECRET + rclone: + command: "rclone copy -P badhouseplants-public:/badhouseplants-net/$ARGO_APP_IMAGE_TAG /static" diff --git a/scripts/build-container.pl b/scripts/build-container.pl new file mode 100755 index 0000000..9cf6765 --- /dev/null +++ b/scripts/build-container.pl @@ -0,0 +1,32 @@ +#! /usr/bin/perl +use strict; +use warnings; +# ------------------------------------------------- +# -- Setup Git variables +# ------------------------------------------------- +my $git_branch = `git rev-parse --abbrev-ref HEAD`; +my $git_commit_sha = `git rev-parse HEAD`; +my $main_branch = "main"; +chomp($git_branch); +chomp($git_commit_sha); +# ------------------------------------------------- +# -- Build the image with SHA tag +# ------------------------------------------------- +my $container_registry = $ENV{'CONTAINER_REGISTRY'} || 'git.badhouseplants.net'; +my $image_name = $ENV{'DRONE_REPO'} || "badhouseplants/badhouseplants-net"; +my $tag = "$container_registry/$image_name:$git_commit_sha"; +my $username = $ENV{'DRONE_USERNAME'} || "allanger"; +my $password = $ENV{'GITEA_TOKEN'} || "YOU NOT AUTHORIZED, PAL"; +print `buildah login --username $username --password $password $container_registry` or die; +print `buildah build -t $tag .` or die; +print `buildah push $tag` or die; +# ------------------------------------------------- +# -- Push the lates if the branch is main +# ------------------------------------------------- +if ( $git_branch eq $main_branch) { + my $latest_tag = "$container_registry/$image_name:latest"; + print `buildah tag $tag $latest_tag` or die; + print `buildah push $latest_tag` or die; +} + +print "Thanks!\n"; diff --git a/scripts/check.pl b/scripts/check.pl deleted file mode 100755 index 8457a56..0000000 --- a/scripts/check.pl +++ /dev/null @@ -1,6 +0,0 @@ -#! /usr/bin/perl -my $commits = "argocd app list -o yaml -l application=badhouseplants | yq '.[].metadata.labels.commit_sha'"; -my @commits_out = `$commits`; -chomp @commits_out; -push @commits_out, 'latest'; -print "@commits_out"; diff --git a/scripts/cleanup.pl b/scripts/cleanup.pl index 8903703..4cb416d 100755 --- a/scripts/cleanup.pl +++ b/scripts/cleanup.pl @@ -15,7 +15,7 @@ my $gitea_user=$ENV{'GITEA_USER'} || $ENV{'DRONE_COMMIT_AUTHOR'}; # --------------------------------------- # -- Get tags from Gitea # --------------------------------------- -my $builds = "curl -X 'GET' \"$gitea_list_api\" -H 'accept: application/json' -H \"Authorization: token $gitea_token\" | jq '.[].version'"; +my $builds = "curl -X 'GET' \"$gitea_list_api\" -H 'accept: application/json' -H \"Authorization: token $gitea_token\" | jq -r '.[].version'"; my @builds_out = `$builds`; chomp @builds_out; # --------------------------------------- diff --git a/scripts/deploy-app.pl b/scripts/deploy-app.pl new file mode 100755 index 0000000..ee6cc98 --- /dev/null +++ b/scripts/deploy-app.pl @@ -0,0 +1,27 @@ +#! /usr/bin/perl +use strict; +use warnings; + +my $chart_version = `cat chart/Chart.yaml | yq '.version'` or die; +chomp($chart_version); + +my $git_branch = `git rev-parse --abbrev-ref HEAD`; +chomp($git_branch); +my $git_commit_sha = `git rev-parse HEAD`; +chomp($git_commit_sha); +my $main_branch = "main"; + +print +print $chart_version; + # - kubectl get -f ./kube/applicationset.yaml -o yaml > /tmp/appset.yaml + # - yq -i "del(.metadata.resourceVersion)" /tmp/appset.yaml + # - yq -i "del(.metadata.generation)" /tmp/appset.yaml + # - yq -i "del(.metadata.uid)" /tmp/appset.yaml + # - yq -i "del(.status)" /tmp/appset.yaml + # - yq -i "del(.spec.generators[].list.elements[] | select(.branch == \"$ARGO_APP_BRANCH\"))" /tmp/appset.yaml + # - yq -i "del(.spec.generators[].list.elements[] | select(.commit_sha == \"$ARGO_APP_IMAGE_TAG\"))" /tmp/appset.yaml + # - yq -i '. *= load("./kube/applicationset.yaml")' /tmp/appset.yaml + # - envsubst < ./kube/main.yaml > /tmp/elements.yaml + # - yq -i '.spec.generators[].list.elements += load("/tmp/elements.yaml")' /tmp/appset.yaml + # - kubectl apply -f /tmp/appset.yaml +# \ No newline at end of file diff --git a/scripts/upload-media.pl b/scripts/upload-media.pl index cc0bc40..bc33462 100755 --- a/scripts/upload-media.pl +++ b/scripts/upload-media.pl @@ -5,13 +5,23 @@ my $main_branch = "main"; my $common_bucket = "badhouseplants-minio:/badhouseplants-net"; my $main_bucket = "badhouseplants-minio:/badhouseplants-net-main"; -chop($git_branch); -chop($git_commit_sha); +# -------------------------------------------------- +# -- I'm doing all of it because I can't undestand +# -- how not to send 300Mb to the buildah context +# -------------------------------------------------- +chomp($git_branch); +chomp($git_commit_sha); + +print `cp -r . /tmp/$git_commit_sha` or die; + +print "Getting the lfs data\n"; +print `git -C /tmp/$git_commit_sha lfs pull` or die; print "Creating a new hashed dir in the common bucket\n"; -print `rclone copy -P static "$common_bucket/$git_commit_sha"`; + +print `rclone copy -P /tmp/$git_commit_sha/static "$common_bucket/$git_commit_sha"` or die; if ( $git_branch eq $main_branch) { print "Syncing to the production bucket\n"; - print `rclone sync -P "$common_bucket/$git_commit_sha" "$main_bucket/"`; + print `rclone sync -P "$common_bucket/$git_commit_sha" "$main_bucket/"` or die; }