Add deployment and improve Dockerfiles and CICD

This commit is contained in:
jkreller
2024-02-06 20:16:27 +00:00
committed by ekresse
parent b22c6c010b
commit 130dfb9698
12 changed files with 188 additions and 125 deletions

View File

@ -2,7 +2,8 @@ stages:
- lint
- build
- test
- docker
- oci-build
- deploy
lint-frontend:
image: node:lts
@ -29,7 +30,7 @@ lint-backend:
build-backend:
image: golang:1.21-alpine
image: golang:alpine
stage: build
rules:
- changes:
@ -58,7 +59,7 @@ build-frontend:
- frontend/build
test-backend:
image: golang:1.21-alpine
image: golang:alpine
stage: test
rules:
- changes:
@ -83,11 +84,10 @@ test-frontend:
- lint-frontend
build-backend-image:
stage: docker
image: docker:20.10.16
stage: oci-build
image: docker:latest
services:
- name: docker:20.10.16-dind
alias: docker
- docker:dind
tags:
- image
variables:
@ -96,22 +96,19 @@ build-backend-image:
DOCKER_TLS_CERTDIR: "/certs"
DOCKER_TLS_VERIFY: 1
DOCKER_CERT_PATH: "/certs/client"
script:
- cd backend
before_script:
- docker login -u $CI_DOCKER_REGISTRY_USER -p $CI_DOCKER_REGISTRY_PASSWORD $CI_DOCKER_REGISTRY
- docker build -t htwkalender-backend$IMAGE_TAG .
- docker tag htwkalender-backend$IMAGE_TAG $CI_DOCKER_REGISTRY_USER/htwkalender:backend
- docker push $CI_DOCKER_REGISTRY_USER/htwkalender:backend
only:
- main
- development
script:
- docker build --pull -t $CI_DOCKER_REGISTRY_REPO:backend -f ./backend/Dockerfile --target prod ./backend
- docker push $CI_DOCKER_REGISTRY_REPO:backend
rules:
- if: $CI_COMMIT_BRANCH == "main"
build-frontend-image:
stage: docker
image: docker:20.10.16
stage: oci-build
image: docker:latest
services:
- name: docker:20.10.16-dind
alias: docker
- docker:dind
tags:
- image
variables:
@ -120,12 +117,36 @@ build-frontend-image:
DOCKER_TLS_CERTDIR: "/certs"
DOCKER_TLS_VERIFY: 1
DOCKER_CERT_PATH: "/certs/client"
script:
- cd frontend
before_script:
- docker login -u $CI_DOCKER_REGISTRY_USER -p $CI_DOCKER_REGISTRY_PASSWORD $CI_DOCKER_REGISTRY
- docker build -f Dockerfile_prod -t htwkalender-frontend$IMAGE_TAG .
- docker tag htwkalender-frontend$IMAGE_TAG $CI_DOCKER_REGISTRY_USER/htwkalender:frontend
- docker push $CI_DOCKER_REGISTRY_USER/htwkalender:frontend
only:
- main
- development
script:
- docker build --pull -t $CI_DOCKER_REGISTRY_REPO:frontend -f ./frontend/Dockerfile --target prod .
- docker push $CI_DOCKER_REGISTRY_REPO:frontend
rules:
- if: $CI_COMMIT_BRANCH == "main"
deploy-all:
stage: deploy
image: alpine:latest
before_script:
- apk add --no-cache openssh-client sed # install dependencies
- eval $(ssh-agent -s) # set some ssh variables
- ssh-add <(echo "$CI_SSH_KEY" | tr -d '\r')
script:
# replace some placeholders
- sed -i -e "s|DOCKER_REGISTRY_REPO|$CI_DOCKER_REGISTRY_REPO|" docker-compose.prod.yml
# upload necessary files to the server
- >
scp -P $CI_SSH_PORT -o StrictHostKeyChecking=no -o LogLevel=ERROR ./docker-compose.prod.yml ./reverseproxy.conf
$CI_SSH_USER@$CI_SSH_HOST:/home/$CI_SSH_USER/docker/htwkalender/
# ssh to the server and start the service
- >
ssh -p $CI_SSH_PORT -o StrictHostKeyChecking=no -o LogLevel=ERROR $CI_SSH_USER@$CI_SSH_HOST
"cd /home/$CI_SSH_USER/docker/htwkalender/ &&
docker login -u $CI_DOCKER_REGISTRY_USER -p $CI_DOCKER_REGISTRY_PASSWORD $CI_DOCKER_REGISTRY &&
docker compose -f ./docker-compose.prod.yml down && docker compose -f ./docker-compose.prod.yml up -d --remove-orphans && docker logout"
needs:
- build-frontend-image
- build-backend-image
rules:
- if: $CI_COMMIT_BRANCH == "main"