Use a custom image for building and more scripts

This commit is contained in:
Nikolai Rodionov 2023-04-24 18:01:13 +02:00
parent 27b1b4acc0
commit b5e304bc3c
No known key found for this signature in database
GPG Key ID: 19DB54039EBF8F10
10 changed files with 213 additions and 48 deletions

View File

@ -32,62 +32,46 @@ name: Build badhouseplants.net
trigger: trigger:
event: event:
- push - push
clone:
disable: true
steps: steps:
- name: Download submodules with themes and remove lfs - name: clone
image: alpine/git image: alpine/git
environment:
GIT_LFS_SKIP_SMUDGE: 1
commands: commands:
- git submodule update --init --recursive - git clone $DRONE_REMOTE_URL --recurse-submodules .
- rm -rf static - git checkout $DRONE_COMMIT
- name: Test a build - name: Test a build
image: git.badhouseplants.net/badhouseplants/hugo-builder image: git.badhouseplants.net/badhouseplants/hugo-builder
depends_on:
- clone
commands: commands:
- hugo - hugo
- name: Build and push the docker image - name: Build and push the docker image
image: plugins/docker image: git.badhouseplants.net/badhouseplants/badhouseplants-builder:80ffd53372652576fa3c36a56b351b448a025c6a
when: privileged: true
branch: depends_on:
exclude: - Test a build
- main environment:
settings: GITEA_TOKEN:
registry: git.badhouseplants.net
username: allanger
password:
from_secret: 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: commands:
- git lfs pull - ./scripts/build-container.pl
- name: Sync pictures from lfs to Minio - 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: environment:
RCLONE_CONFIG_CONTENT: RCLONE_CONFIG_CONTENT:
from_secret: RCLONE_CONFIG_CONTENT_PRIVATE from_secret: RCLONE_CONFIG_CONTENT_PRIVATE
RCLONE_CONFIG: /tmp/rclone.conf RCLONE_CONFIG: /tmp/rclone.conf
commands: commands:
- echo "$RCLONE_CONFIG_CONTENT" > $RCLONE_CONFIG - echo "$RCLONE_CONFIG_CONTENT" > $RCLONE_CONFIG
- apk update && apk add git perl
- ./scripts/upload-media.pl - ./scripts/upload-media.pl
- name: Deploy a preview ApplicationSet - name: Deploy a preview ApplicationSet
@ -96,6 +80,9 @@ steps:
branch: branch:
exclude: exclude:
- main - main
depends_on:
- Build and push the docker image
- Sync pictures from lfs to Minio
environment: environment:
KUBECONFIG_CONTENT: KUBECONFIG_CONTENT:
from_secret: KUBECONFIG_CONTENT from_secret: KUBECONFIG_CONTENT
@ -123,6 +110,9 @@ steps:
when: when:
branch: branch:
- main - main
depends_on:
- Build and push the docker image
- Sync pictures from lfs to Minio
environment: environment:
KUBECONFIG_CONTENT: KUBECONFIG_CONTENT:
from_secret: KUBECONFIG_CONTENT from_secret: KUBECONFIG_CONTENT
@ -151,6 +141,9 @@ steps:
- name: Sync application - name: Sync application
image: argoproj/argocd image: argoproj/argocd
depends_on:
- Deploy a main ApplicationSet
- Deploy a preview ApplicationSet
environment: environment:
ARGOCD_SERVER: ARGOCD_SERVER:
from_secret: ARGOCD_SERVER from_secret: ARGOCD_SERVER
@ -162,6 +155,8 @@ steps:
- name: Cleanup everything - name: Cleanup everything
image: rclone/rclone:latest image: rclone/rclone:latest
depends_on:
- Sync application
environment: environment:
RCLONE_CONFIG_CONTENT: RCLONE_CONFIG_CONTENT:
from_secret: RCLONE_CONFIG_CONTENT_PRIVATE from_secret: RCLONE_CONFIG_CONTENT_PRIVATE

View File

@ -1,5 +1,4 @@
FROM git.badhouseplants.net/badhouseplants/hugo-builder FROM git.badhouseplants.net/badhouseplants/hugo-builder
WORKDIR /src WORKDIR /src
COPY . /src COPY . /src
RUN ls -la /src
ENTRYPOINT ["hugo"] ENTRYPOINT ["hugo"]

52
kube/application.yaml Normal file
View File

@ -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

24
kube/values-main.yaml Normal file
View File

@ -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

32
kube/values-preview.yaml Normal file
View File

@ -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"

32
scripts/build-container.pl Executable file
View File

@ -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";

View File

@ -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";

View File

@ -15,7 +15,7 @@ my $gitea_user=$ENV{'GITEA_USER'} || $ENV{'DRONE_COMMIT_AUTHOR'};
# --------------------------------------- # ---------------------------------------
# -- Get tags from Gitea # -- 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`; my @builds_out = `$builds`;
chomp @builds_out; chomp @builds_out;
# --------------------------------------- # ---------------------------------------

27
scripts/deploy-app.pl Executable file
View File

@ -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
#

View File

@ -5,13 +5,23 @@ my $main_branch = "main";
my $common_bucket = "badhouseplants-minio:/badhouseplants-net"; my $common_bucket = "badhouseplants-minio:/badhouseplants-net";
my $main_bucket = "badhouseplants-minio:/badhouseplants-net-main"; 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 "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) { if ( $git_branch eq $main_branch) {
print "Syncing to the production bucket\n"; 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;
} }