hedera-web/Jenkinsfile

131 lines
4.0 KiB
Groovy

#!/usr/bin/env groovy
def BRANCH_ENV = [
test: 'test',
master: 'production'
]
def remote = [:]
def changesFile
node {
stage('Setup') {
env.NODE_ENV = BRANCH_ENV[env.BRANCH_NAME] ?: 'dev'
echo "NODE_NAME: ${env.NODE_NAME}"
echo "WORKSPACE: ${env.WORKSPACE}"
}
}
pipeline {
agent any
environment {
PROJECT_NAME = 'hedera-web'
}
stages {
stage('Debuild') {
when {
anyOf {
branch 'master'
branch 'test'
}
}
agent {
docker {
image 'registry.verdnatura.es/debuild:2.23.4-vn6'
registryUrl 'https://registry.verdnatura.es/'
registryCredentialsId 'docker-registry'
}
}
steps {
sh 'debuild -us -uc -b'
sh 'mkdir -p output'
sh 'mv ../*.deb ../*.build ../*.changes ../*.buildinfo output'
sleep 86400
stash includes: 'output/*', name: 'debuildOutput'
}
}
stage('Reprepro') {
when {
anyOf {
branch 'master'
branch 'test'
}
}
environment {
REPREPRO_BASE_DIR = '/var/lib/reprepro/repo'
}
steps {
unstash 'debuildOutput'
script {
def files = findFiles(glob: 'output/*.changes')
files.each { file -> changesFile = file.name }
}
withCredentials([sshUserPrivateKey(
credentialsId: 'jenkins-agent',
usernameVariable: 'user',
keyFileVariable: 'keyFile'
)]) {
script {
remote.user = user
remote.identityFile = keyFile
remote.name = 'reprepro'
remote.host = 'reprepro.reprepro'
remote.allowAnyHosts = true
}
sshPut([
remote: remote,
from: "output/$changesFile",
into: "$REPREPRO_BASE_DIR/incoming"
])
sshCommand([
remote: remote,
command: "reprepro processincoming default $changesFile"
])
}
}
}
stage('Deploy') {
when {
anyOf {
branch 'master'
branch 'test'
}
}
environment {
CREDS = credentials('docker-registry')
IMAGE = "$REGISTRY/verdnatura/hedera-web"
}
steps {
script {
def packageJson = readJSON file: 'package.json'
env.VERSION = "${packageJson.version}"
env.TAG = "${packageJson.version}-build${env.BUILD_ID}"
}
sh 'docker-compose build --build-arg BUILD_ID=$BUILD_ID --parallel'
sh 'docker tag $IMAGE:$TAG $IMAGE:latest'
sh 'docker login --username $CREDS_USR --password $CREDS_PSW $REGISTRY'
sh 'docker push $IMAGE:$TAG'
sh 'docker push $IMAGE:latest'
withKubeConfig([
serverUrl: "$KUBERNETES_API",
credentialsId: 'kubernetes',
namespace: 'salix'
]) {
sh 'kubectl set image deployment/hedera-web-$BRANCH_NAME hedera-web-$BRANCH_NAME=$IMAGE:$TAG'
sh 'kubectl set image deployment/hedera-web-cron-$BRANCH_NAME hedera-web-cron-$BRANCH_NAME=$IMAGE:$TAG'
}
}
}
}
post {
unsuccessful {
setEnv()
sendEmail()
}
}
}