From 0bf4b761daa6e880c01f4574f1f4ce347d882f97 Mon Sep 17 00:00:00 2001 From: Arie Peterson Date: Thu, 22 Apr 2021 16:07:06 +0200 Subject: [PATCH 1/5] Building and deploying from CI --- .gitlab-ci.yml | 52 +++++++++++++++++++++++++++++++++++++++++++++--- build.Dockerfile | 9 +++++++++ deploy/deploy.sh | 21 +++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 build.Dockerfile create mode 100755 deploy/deploy.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0b3abd5..9a39071 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,16 +1,35 @@ stages: + - build-container - test - build + - deploy + +variables: + IMAGENAME: helpful-contact-form-builder + +# Use this image unless specified otherwise. Note that this image is actually +# built by the build-container job. +default: + image: "${CI_REGISTRY_IMAGE}/${IMAGENAME}:${CI_COMMIT_REF_NAME}" + +build-container: + stage: build-container + image: + # We need a shell to provide the registry credentials, so we need to use the + # kaniko debug image (https://github.com/GoogleContainerTools/kaniko#debug-image) + name: gcr.io/kaniko-project/executor:debug + entrypoint: [""] + script: + - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json + - /kaniko/executor --cache=true --context ${CI_PROJECT_DIR}/ --dockerfile ${CI_PROJECT_DIR}/build.Dockerfile --destination ${CI_REGISTRY_IMAGE}/${IMAGENAME}:${CI_COMMIT_REF_NAME} + test: stage: test - image: node:15-buster-slim cache: key: "pnpm-cache" paths: - "$CI_PROJECT_DIR/.pnpm-store" before_script: - - npm install -g pnpm - - pnpm install - pnpm install -g js-yaml jest script: - jest src/ --coverage --coverageReporters cobertura @@ -18,3 +37,30 @@ test: artifacts: reports: cobertura: "$CI_PROJECT_DIR/coverage/cobertura-coverage.xml" + +build: + stage: build + script: + - npm build + artifacts: + paths: + - build + +# Deploy main branch to contact.greenhost.net +deploy-production-net: + stage: deploy + environment: + name: production + url: https://contact.greenhost.net + variables: + ENVIRONMENT: 'production' + FTP_USER: 'webmaster_greenhost_nl' + FTP_HOST: 'ftp.greenhost.nl' + DOMAIN_NAME: 'greenhost.net' + SUBDOMAIN: 'contact' + BUILD_FOLDER: './build' + # TODO: commented out for testing + # rules: + # - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH + script: + - deploy/deploy.sh diff --git a/build.Dockerfile b/build.Dockerfile new file mode 100644 index 0000000..0bdac3c --- /dev/null +++ b/build.Dockerfile @@ -0,0 +1,9 @@ +FROM node:15-buster-slim + +RUN npm install -g pnpm + +WORKDIR /workspaces/outage-form + +ENV PATH $PATH:/workspaces/outage-form/node_modules/.bin + +RUN pnpm install diff --git a/deploy/deploy.sh b/deploy/deploy.sh new file mode 100755 index 0000000..cc0ad31 --- /dev/null +++ b/deploy/deploy.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +set -euvo pipefail + +# Run ssh-agent and add ssh key. +eval $(ssh-agent -s) +ssh-add <(echo "$SSH_KEY") + +# Add server's ssh host key to known hosts. +mkdir -p ~/.ssh +[[ -f /.dockerenv ]] && echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts + +sshHost="${FTP_USER}@${FTP_HOST}" +targetDir='${HOME}'"${DOMAIN_NAME}/${SUBDOMAIN}" + +rsyncTarget="${sshHost}:${targetDir}" + +# Upload site. +# rsync -HAXa --delete ${BUILD_FOLDER}/ "${rsyncTarget}" +# TODO: dry run for testing +rsync -HAXa --delete ${BUILD_FOLDER}/ "${rsyncTarget}" --dry-run -- GitLab From b6fa95296e9d0f35d4dc0c419718b9ef96a66bab Mon Sep 17 00:00:00 2001 From: Arie Peterson Date: Tue, 27 Apr 2021 17:25:04 +0200 Subject: [PATCH 2/5] Store node cache in CI cache instead of docker image --- .gitlab-ci.yml | 60 ++++++++++++++++++++++++----------------------- build.Dockerfile | 9 ------- deploy/deploy.sh | 16 ++++++------- public/index.html | 4 ++++ 4 files changed, 43 insertions(+), 46 deletions(-) delete mode 100644 build.Dockerfile diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9a39071..4370c2b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,38 +1,26 @@ stages: - - build-container - test - build - deploy -variables: - IMAGENAME: helpful-contact-form-builder - -# Use this image unless specified otherwise. Note that this image is actually -# built by the build-container job. -default: - image: "${CI_REGISTRY_IMAGE}/${IMAGENAME}:${CI_COMMIT_REF_NAME}" - -build-container: - stage: build-container - image: - # We need a shell to provide the registry credentials, so we need to use the - # kaniko debug image (https://github.com/GoogleContainerTools/kaniko#debug-image) - name: gcr.io/kaniko-project/executor:debug - entrypoint: [""] - script: - - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json - - /kaniko/executor --cache=true --context ${CI_PROJECT_DIR}/ --dockerfile ${CI_PROJECT_DIR}/build.Dockerfile --destination ${CI_REGISTRY_IMAGE}/${IMAGENAME}:${CI_COMMIT_REF_NAME} - test: stage: test + image: node:15-buster-slim cache: - key: "pnpm-cache" + key: "pnpm-store" paths: - - "$CI_PROJECT_DIR/.pnpm-store" + - ".pnpm-store" + # multiple cache are enabled from gitlab 13.11 on + # - key: "node-modules" + # paths: + # - "node_modules" before_script: - - pnpm install -g js-yaml jest + - npm install --global pnpm + - pnpm config set store-dir .pnpm-store + - pnpm install + - pnpm install js-yaml jest script: - - jest src/ --coverage --coverageReporters cobertura + - pnpx jest src/ --coverage --coverageReporters cobertura coverage: /All\sfiles.*?\s+(\d+.\d+)/ artifacts: reports: @@ -40,15 +28,28 @@ test: build: stage: build + image: node:15-buster-slim + cache: + key: "pnpm-store" + paths: + - ".pnpm-store" + before_script: + - npm install --global pnpm + - pnpm config set store-dir .pnpm-store + - pnpm install script: - - npm build + - npm run build artifacts: paths: - build # Deploy main branch to contact.greenhost.net -deploy-production-net: +deploy-production: stage: deploy + # Only run this job for the main branch. + rules: + - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH + image: debian:10 environment: name: production url: https://contact.greenhost.net @@ -58,9 +59,10 @@ deploy-production-net: FTP_HOST: 'ftp.greenhost.nl' DOMAIN_NAME: 'greenhost.net' SUBDOMAIN: 'contact' + SUBFOLDER: '/development' BUILD_FOLDER: './build' - # TODO: commented out for testing - # rules: - # - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH + before_script: + - apt-get update + - apt-get install -y rsync openssh-client script: - deploy/deploy.sh diff --git a/build.Dockerfile b/build.Dockerfile deleted file mode 100644 index 0bdac3c..0000000 --- a/build.Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -FROM node:15-buster-slim - -RUN npm install -g pnpm - -WORKDIR /workspaces/outage-form - -ENV PATH $PATH:/workspaces/outage-form/node_modules/.bin - -RUN pnpm install diff --git a/deploy/deploy.sh b/deploy/deploy.sh index cc0ad31..7319c8e 100755 --- a/deploy/deploy.sh +++ b/deploy/deploy.sh @@ -1,21 +1,21 @@ #!/bin/bash +set -o errexit set -euvo pipefail -# Run ssh-agent and add ssh key. -eval $(ssh-agent -s) -ssh-add <(echo "$SSH_KEY") +# Store ssh key in file. +touch /tmp/key +chmod 0600 /tmp/key +printf "%s" "$SSH_KEY" > /tmp/key # Add server's ssh host key to known hosts. mkdir -p ~/.ssh -[[ -f /.dockerenv ]] && echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts +[[ -f /.dockerenv ]] && printf "%s" "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts sshHost="${FTP_USER}@${FTP_HOST}" -targetDir='${HOME}'"${DOMAIN_NAME}/${SUBDOMAIN}" +targetDir='${HOME}'"${DOMAIN_NAME}/${SUBDOMAIN}${SUBFOLDER}" rsyncTarget="${sshHost}:${targetDir}" # Upload site. -# rsync -HAXa --delete ${BUILD_FOLDER}/ "${rsyncTarget}" -# TODO: dry run for testing -rsync -HAXa --delete ${BUILD_FOLDER}/ "${rsyncTarget}" --dry-run +rsync -e 'ssh -i /tmp/key' -HAXa -vi --delete ${BUILD_FOLDER}/ "${rsyncTarget}" diff --git a/public/index.html b/public/index.html index 08db1bb..7419aad 100644 --- a/public/index.html +++ b/public/index.html @@ -8,6 +8,10 @@ Snowpack App +
Warning: this is a development version of + this contact form, and any messages sent using it will be ignored! Please + visit our live contact form + for actual support.
-- GitLab From f155c238063867d9987445505091b66faf2eb2dd Mon Sep 17 00:00:00 2001 From: Arie Peterson Date: Thu, 29 Apr 2021 10:21:00 +0200 Subject: [PATCH 3/5] Process comments --- .gitlab-ci.yml | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4370c2b..1727ae4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,15 +10,10 @@ test: key: "pnpm-store" paths: - ".pnpm-store" - # multiple cache are enabled from gitlab 13.11 on - # - key: "node-modules" - # paths: - # - "node_modules" before_script: - npm install --global pnpm - pnpm config set store-dir .pnpm-store - pnpm install - - pnpm install js-yaml jest script: - pnpx jest src/ --coverage --coverageReporters cobertura coverage: /All\sfiles.*?\s+(\d+.\d+)/ @@ -44,22 +39,21 @@ build: - build # Deploy main branch to contact.greenhost.net -deploy-production: +deploy-testing: stage: deploy # Only run this job for the main branch. rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH image: debian:10 environment: - name: production - url: https://contact.greenhost.net + name: testing + url: https://contact.greenhost.net/testing variables: - ENVIRONMENT: 'production' FTP_USER: 'webmaster_greenhost_nl' FTP_HOST: 'ftp.greenhost.nl' DOMAIN_NAME: 'greenhost.net' SUBDOMAIN: 'contact' - SUBFOLDER: '/development' + SUBFOLDER: '/testing' BUILD_FOLDER: './build' before_script: - apt-get update -- GitLab From d5be1963407f31da4487ad025ae437f297f0d88f Mon Sep 17 00:00:00 2001 From: Arie Peterson Date: Thu, 29 Apr 2021 14:38:48 +0200 Subject: [PATCH 4/5] Use ssh-agent in deploy script --- deploy/deploy.sh | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/deploy/deploy.sh b/deploy/deploy.sh index 7319c8e..8495456 100755 --- a/deploy/deploy.sh +++ b/deploy/deploy.sh @@ -3,10 +3,9 @@ set -o errexit set -euvo pipefail -# Store ssh key in file. -touch /tmp/key -chmod 0600 /tmp/key -printf "%s" "$SSH_KEY" > /tmp/key +eval $(ssh-agent -s) +# Register ssh key with ssh-agent. +ssh-add - <<<"$SSH_KEY" # Add server's ssh host key to known hosts. mkdir -p ~/.ssh @@ -14,8 +13,7 @@ mkdir -p ~/.ssh sshHost="${FTP_USER}@${FTP_HOST}" targetDir='${HOME}'"${DOMAIN_NAME}/${SUBDOMAIN}${SUBFOLDER}" - rsyncTarget="${sshHost}:${targetDir}" # Upload site. -rsync -e 'ssh -i /tmp/key' -HAXa -vi --delete ${BUILD_FOLDER}/ "${rsyncTarget}" +rsync -HAXa -vi --delete ${BUILD_FOLDER}/ "${rsyncTarget}" -- GitLab From 25ff9bc327cf29aaac1a86e9f64ada0cda95d295 Mon Sep 17 00:00:00 2001 From: Arie Peterson Date: Thu, 29 Apr 2021 14:45:31 +0200 Subject: [PATCH 5/5] Rename `testing` env to `staging` --- .gitlab-ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1727ae4..43f787a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -39,21 +39,21 @@ build: - build # Deploy main branch to contact.greenhost.net -deploy-testing: +deploy-staging: stage: deploy # Only run this job for the main branch. rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH image: debian:10 environment: - name: testing - url: https://contact.greenhost.net/testing + name: staging + url: https://contact.greenhost.net/staging variables: FTP_USER: 'webmaster_greenhost_nl' FTP_HOST: 'ftp.greenhost.nl' DOMAIN_NAME: 'greenhost.net' SUBDOMAIN: 'contact' - SUBFOLDER: '/testing' + SUBFOLDER: '/staging' BUILD_FOLDER: './build' before_script: - apt-get update -- GitLab