From 2c54c3108fefc285d44381cd01acd71cf2a34f10 Mon Sep 17 00:00:00 2001 From: Suwako Moriya Date: Tue, 28 Apr 2020 00:42:13 +0200 Subject: [PATCH] [Jenkins] Deploy a cleaned up archive --- .gitignore | 1 + .releaseignore | 9 ++++++ Dockerfile | 3 +- Jenkinsfile | 76 ++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 .releaseignore diff --git a/.gitignore b/.gitignore index bbcdfc5..de37f8a 100644 --- a/.gitignore +++ b/.gitignore @@ -77,3 +77,4 @@ data/* /src/datas/ /src/doctest_config.toml /datas +requirements.txt diff --git a/.releaseignore b/.releaseignore new file mode 100644 index 0000000..b5e4d69 --- /dev/null +++ b/.releaseignore @@ -0,0 +1,9 @@ +.git +.gitignore +.releaseignore +Dockerfile +Jenkinsfile +Pipfile +Pipfile.lock +doc +scripts diff --git a/Dockerfile b/Dockerfile index 42acd50..29c3846 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,8 @@ FROM python:3.8 RUN apt-get update && apt-get install -y\ rsync \ - openssh-client + openssh-client \ + zip RUN pip install pipenv diff --git a/Jenkinsfile b/Jenkinsfile index aeb8b4d..c4b4878 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -7,10 +7,31 @@ pipeline { environment { SPHINXOPTS = '-w sphinx-build.log' DEPLOY_HOST = 'docs@moriya.zapto.org' - DEPLOY_PATH = 'www/docs/bot-base/' + PROJECT_NAME = 'bot-base' + DEPLOY_DOC_PATH = "www/docs/${env.PROJECT_NAME}/" + DEPLOY_REL_PATH = "www/releases/${env.PROJECT_NAME}/" + TAG_NAME = """${TAG_NAME ?: ""}""" + ARTIFACTS = "${WORKSPACE}/.artifacts" } - stages { + stage('Generate release archives') { + steps { + sh 'git clean -fxd' + sh 'mkdir -p ${ARTIFACTS}/build' + sh 'mkdir -p /tmp/build' + sh 'pipenv lock -r | tee requirements.txt' + sh 'echo .artifacts >> .releaseignore' + sh 'rsync -avr --exclude-from=.releaseignore ./ /tmp/build' + sh 'tar -C /tmp/build -cvzf ${ARTIFACTS}/build/${TAG_NAME:-${GIT_BRANCH#*/}}.tar.gz --owner=0 --group=0 .' + sh 'cd /tmp/build && zip ${ARTIFACTS}/build/${TAG_NAME:-${GIT_BRANCH#*/}}.zip -r .' + } + post { + always { + archiveArtifacts artifacts: ".artifacts/build/*", fingerprint: true + } + } + } + stage('Install Dependencies') { steps { sh 'pipenv sync --verbose --sequential --dev' @@ -31,14 +52,14 @@ pipeline { stage('Build Documentation') { steps { - sh 'rm -f doc/sphinx-build.log' sh '''cd doc pipenv run make html''' - sh 'tar -C doc/build/html -czf docs-${BUILD_TAG}.tar.gz .' + sh 'mkdir -p ${ARTIFACTS}/doc' + sh 'tar -C doc/build/html -czf ${ARTIFACTS}/doc/html.tar.gz .' } post { success { - archiveArtifacts artifacts: "docs-${env.BUILD_TAG}.tar.gz", fingerprint: true + archiveArtifacts artifacts: ".artifacts/doc/*", fingerprint: true } failure { sh 'cat doc/sphinx-build.log' @@ -47,9 +68,6 @@ pipeline { } stage('Deploy Documentation') { - environment { - TAG_NAME = """${TAG_NAME ?: ""}""" - } when { anyOf { branch 'stable' @@ -58,22 +76,52 @@ pipeline { } } steps { - sh 'rm -f rsync.log' sshagent(credentials: ['1cf72f47-b70c-4f90-a958-020956099d19']) { sh 'echo ${TAG_NAME:-${GIT_BRANCH#*/}}' - sh 'echo ${DEPLOY_HOST}:${DEPLOY_PATH}${TAG_NAME:-${GIT_BRANCH#*/}}/ >> debug.log' - sh 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes ${DEPLOY_HOST} mkdir -p ${DEPLOY_PATH}${TAG_NAME:-${GIT_BRANCH#*/}}/' + sh 'echo ${DEPLOY_HOST}:${DEPLOY_DOC_PATH}${TAG_NAME:-${GIT_BRANCH#*/}}/' + sh 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes ${DEPLOY_HOST} mkdir -p ${DEPLOY_DOC_PATH}${TAG_NAME:-${GIT_BRANCH#*/}}/' sh '''rsync -aze 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes' \ - --log-file=rsync.log \ + --log-file=rsync-doc.log \ --delete \ - doc/build/html/ ${DEPLOY_HOST}:${DEPLOY_PATH}${TAG_NAME:-${GIT_BRANCH#*/}}/''' + doc/build/html/ ${DEPLOY_HOST}:${DEPLOY_DOC_PATH}${TAG_NAME:-${GIT_BRANCH#*/}}/''' } } post { failure { - sh 'cat doc/debug.log doc/rsync.log' + sh 'cat rsync-doc.log' + } + } + } + + stage('Deploy Release Files') { + when { + anyOf { + branch 'stable' + branch 'master' + buildingTag() + } + } + steps { + sshagent(credentials: ['1cf72f47-b70c-4f90-a958-020956099d19']) { + sh 'echo ${TAG_NAME:-${GIT_BRANCH#*/}}' + sh 'echo ${DEPLOY_HOST}:${DEPLOY_REL_PATH}${TAG_NAME:-${GIT_BRANCH#*/}}/' + sh 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes ${DEPLOY_HOST} mkdir -p ${DEPLOY_REL_PATH}${TAG_NAME:-${GIT_BRANCH#*/}}/' + sh '''rsync -aze 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes' \ + --log-file=rsync-rel.log \ + --delete \ + ${ARTIFACTS}/ ${DEPLOY_HOST}:${DEPLOY_REL_PATH}${TAG_NAME:-${GIT_BRANCH#*/}}/''' + } + } + post { + failure { + sh 'cat rsync-rel.log' } } } } + post { + always { + sh 'git clean -fxd' + } + } }