From 1bcada3008176790abe538cc4ffcfa46d510d20d Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Tue, 27 Jun 2023 14:56:31 +0200 Subject: [PATCH] Change ci-cd --- .gitlab-ci.yml | 76 ++--------------------------------- .gitlab/_common.gitlab-ci.yml | 11 +++++ .gitlab/build.gitlab-ci.yml | 24 +++++++++++ .gitlab/build_image.sh | 41 ------------------- .gitlab/deploy.gitlab-ci.yml | 15 +++++++ .gitlab/test.gitlab-ci.yml | 15 +++++++ docker/Dockerfile | 8 ++-- docker/Dockerfile.dev | 4 +- web/templates/layout.html | 4 +- 9 files changed, 76 insertions(+), 122 deletions(-) create mode 100644 .gitlab/build.gitlab-ci.yml delete mode 100755 .gitlab/build_image.sh create mode 100644 .gitlab/deploy.gitlab-ci.yml create mode 100644 .gitlab/test.gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ca3f932..7a8adb5 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,85 +1,15 @@ -variables: - LATEST_IMAGE: '$CI_REGISTRY_IMAGE:latest' - DOCKER_VERSION: '24.0.2' - GOLANG_VERSION: '1.20' - NODE_VERSION: '18' - ALPINE_VERSION: '3' - DEBIAN_VERSION: '12' - TELEPORT_VERSION: '13.1.1' - -image: docker:$DOCKER_VERSION-git - stages: - test - build - - analyse - deploy include: - local: .gitlab/_common.gitlab-ci.yml - local: .gitlab/_rules.gitlab-ci.yml + - local: /.gitlab/test.gitlab-ci.yml + - local: /.gitlab/build.gitlab-ci.yml + - local: /.gitlab/deploy.gitlab-ci.yml - template: Jobs/Secret-Detection.gitlab-ci.yml - - template: Jobs/Container-Scanning.gitlab-ci.yml secret_detection: rules: !reference [.rules:default, rules] - -unit_tests: - rules: !reference [.rules:default, rules] - stage: test - image: golang:$GOLANG_VERSION-alpine - extends: - - .go-cache - script: - - go install gotest.tools/gotestsum@latest - - gotestsum --junitfile report.xml --format testname -- ./... -coverprofile=profile.cov - - go tool cover -func profile.cov - coverage: '/\(statements\)(?:\s+)?(\d+(?:\.\d+)?%)/' - artifacts: - when: always - reports: - junit: report.xml - -build_release: - rules: !reference [.rules:release, rules] - stage: build - extends: .login_registry - services: - - name: docker:$DOCKER_VERSION-dind - alias: docker - variables: - DOCKER_BUILDKIT: '1' - DOCKER_TLS_CERTDIR: '/certs' - CURRENT_IMAGE: '$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG' - GO_BUILDER_IMAGE: '$CI_REGISTRY_IMAGE:go-builder' - NODE_BUILDER_IMAGE: '$CI_REGISTRY_IMAGE:node-builder' - LOGO_BUILDER_IMAGE: '$CI_REGISTRY_IMAGE:logo' - script: - - apk add bash - - .gitlab/build_image.sh - - docker push $CURRENT_IMAGE - - docker push $LATEST_IMAGE - -container_scanning: - rules: !reference [.rules:release, rules] - stage: analyse - variables: - GIT_STRATEGY: fetch - CS_IMAGE: $LATEST_IMAGE - CS_DOCKERFILE_PATH: docker/Dockerfile - -deploy_release: - rules: !reference [.rules:release, rules] - stage: deploy - image: debian:${DEBIAN_VERSION}-slim - id_tokens: - TBOT_GITLAB_JWT: - aud: tp.fhoss.de - script: - - apt-get update && apt-get install curl -y - - cd /tmp - - 'curl -O https://cdn.teleport.dev/teleport-v${TELEPORT_VERSION}-linux-amd64-bin.tar.gz' - - tar -xvf teleport-v${TELEPORT_VERSION}-linux-amd64-bin.tar.gz - - ./teleport/install - - 'tbot start --token=gitlab --destination-dir=/tmp/tbot-user --data-dir=/tmp/tbot-data --auth-server=tp.fhoss.de:443 --join-method=gitlab --oneshot' - - 'tsh -i /tmp/tbot-user/identity --proxy tp.fhoss.de:443 ssh bot@benelli "docker compose -f /opt/docker/home/docker-compose.yml up -d --pull always"' diff --git a/.gitlab/_common.gitlab-ci.yml b/.gitlab/_common.gitlab-ci.yml index 391499f..2cd2c34 100644 --- a/.gitlab/_common.gitlab-ci.yml +++ b/.gitlab/_common.gitlab-ci.yml @@ -1,3 +1,14 @@ +variables: + LATEST_IMAGE: '$CI_REGISTRY_IMAGE:latest' + DOCKER_VERSION: '24.0.2' + GOLANG_VERSION: '1.20' + NODE_VERSION: '18' + ALPINE_VERSION: '3' + DEBIAN_VERSION: '12' + TELEPORT_VERSION: '13.1.1' + +image: docker:$DOCKER_VERSION-git + .login_registry: before_script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY diff --git a/.gitlab/build.gitlab-ci.yml b/.gitlab/build.gitlab-ci.yml new file mode 100644 index 0000000..d34188e --- /dev/null +++ b/.gitlab/build.gitlab-ci.yml @@ -0,0 +1,24 @@ +build_release: + rules: !reference [.rules:release, rules] + stage: build + extends: .login_registry + services: + - name: docker:$DOCKER_VERSION-dind + alias: docker + variables: + DOCKER_TLS_CERTDIR: '/certs' + CURRENT_IMAGE: '$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG' + script: + - > + docker build . + --file docker/Dockerfile + --build-arg GOLANG_VERSION=$GOLANG_VERSION + --build-arg NODE_VERSION=$NODE_VERSION + --build-arg ALPINE_VERSION=$ALPINE_VERSION + --build-arg APP_VERSION=$CI_COMMIT_TAG + --build-arg BUILD_TIME=$CI_JOB_STARTED_AT + --tag $CURRENT_IMAGE + --tag $LATEST_IMAGE + - docker inspect $CURRENT_IMAGE + - docker push $CURRENT_IMAGE + - docker push $LATEST_IMAGE diff --git a/.gitlab/build_image.sh b/.gitlab/build_image.sh deleted file mode 100755 index 7d54f29..0000000 --- a/.gitlab/build_image.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash - -args=( - --file docker/Dockerfile - --build-arg GOLANG_VERSION=${GOLANG_VERSION} - --build-arg NODE_VERSION=${NODE_VERSION} - --build-arg ALPINE_VERSION=${ALPINE_VERSION} - --build-arg BUILDKIT_INLINE_CACHE=1 -) - -docker pull ${GO_BUILDER_IMAGE} || true -docker build . ${args[@]} \ - --target goBuilder \ - --cache-from ${GO_BUILDER_IMAGE} \ - --tag ${GO_BUILDER_IMAGE} -docker push ${GO_BUILDER_IMAGE} - -docker pull ${NODE_BUILDER_IMAGE} || true -docker build . ${args[@]} \ - --target nodeBuilder \ - --cache-from ${NODE_BUILDER_IMAGE} \ - --tag ${NODE_BUILDER_IMAGE} -docker push ${NODE_BUILDER_IMAGE} - -docker pull ${LOGO_BUILDER_IMAGE} || true -docker build . ${args[@]} \ - --target logo \ - --cache-from ${LOGO_BUILDER_IMAGE} \ - --tag ${LOGO_BUILDER_IMAGE} -docker push ${LOGO_BUILDER_IMAGE} - -docker pull ${LATEST_IMAGE} || true -docker build . ${args[@]} \ - --cache-from ${GO_BUILDER_IMAGE} \ - --cache-from ${NODE_BUILDER_IMAGE} \ - --cache-from ${LOGO_BUILDER_IMAGE} \ - --cache-from ${LATEST_IMAGE} \ - --build-arg VERSION=${CI_COMMIT_TAG} \ - --build-arg BUILDTIME=${CI_JOB_STARTED_AT} \ - --tag ${CURRENT_IMAGE} \ - --tag ${LATEST_IMAGE} diff --git a/.gitlab/deploy.gitlab-ci.yml b/.gitlab/deploy.gitlab-ci.yml new file mode 100644 index 0000000..1a1d2a0 --- /dev/null +++ b/.gitlab/deploy.gitlab-ci.yml @@ -0,0 +1,15 @@ +deploy_release: + rules: !reference [.rules:release, rules] + stage: deploy + image: debian:${DEBIAN_VERSION}-slim + id_tokens: + TBOT_GITLAB_JWT: + aud: tp.fhoss.de + script: + - apt-get update && apt-get install curl -y + - cd /tmp + - 'curl -O https://cdn.teleport.dev/teleport-v${TELEPORT_VERSION}-linux-amd64-bin.tar.gz' + - tar -xvf teleport-v${TELEPORT_VERSION}-linux-amd64-bin.tar.gz + - ./teleport/install + - 'tbot start --token=gitlab --destination-dir=/tmp/tbot-user --data-dir=/tmp/tbot-data --auth-server=tp.fhoss.de:443 --join-method=gitlab --oneshot' + - 'tsh -i /tmp/tbot-user/identity --proxy tp.fhoss.de:443 ssh bot@benelli "docker compose -f /opt/docker/home/docker-compose.yml up -d --pull always"' diff --git a/.gitlab/test.gitlab-ci.yml b/.gitlab/test.gitlab-ci.yml new file mode 100644 index 0000000..78333f8 --- /dev/null +++ b/.gitlab/test.gitlab-ci.yml @@ -0,0 +1,15 @@ +unit_tests: + rules: !reference [.rules:default, rules] + stage: test + image: golang:$GOLANG_VERSION-alpine + extends: + - .go-cache + script: + - go install gotest.tools/gotestsum@latest + - gotestsum --junitfile report.xml --format testname -- ./... -coverprofile=profile.cov + - go tool cover -func profile.cov + coverage: '/\(statements\)(?:\s+)?(\d+(?:\.\d+)?%)/' + artifacts: + when: always + reports: + junit: report.xml diff --git a/docker/Dockerfile b/docker/Dockerfile index 624b3a7..17fdd1f 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -39,9 +39,9 @@ COPY --from=goBuilder /app/internal/bookmarks/config.yaml .internal/bookmarks/co COPY --from=goBuilder /app/godash . # Envs -ARG VERSION -ENV VERSION=$VERSION -ARG BUILDTIME -ENV BUILDTIME=$BUILDTIME +ARG APP_VERSION +ENV APP_VERSION=$APP_VERSION +ARG BUILD_TIME +ENV BUILD_TIME=$BUILD_TIME ENTRYPOINT ["/app/entrypoint.sh"] diff --git a/docker/Dockerfile.dev b/docker/Dockerfile.dev index d32b788..8f7b915 100644 --- a/docker/Dockerfile.dev +++ b/docker/Dockerfile.dev @@ -13,5 +13,5 @@ RUN go install github.com/cosmtrek/air@latest COPY ./.air.toml . # Envs -ENV VERSION=v0.0.1-dev -ENV BUILDTIME=2023-04-05T20:48:07Z +ENV APP_VERSION=v0.0.1-dev +ENV BUILD_TIME=2023-04-05T20:48:07Z diff --git a/web/templates/layout.html b/web/templates/layout.html index e146740..88c973d 100644 --- a/web/templates/layout.html +++ b/web/templates/layout.html @@ -18,12 +18,12 @@ transition: opacity linear 0.15s; } - + {{ block "style". }}{{ end }} -
{{ env "VERSION" }}
+
{{ env "APP_VERSION" }}
{{ block "content" . }}{{ end }}
{{ block "js". }}{{ end }}