From 454792c7e2a8045c82270c484ed40722f60238f8 Mon Sep 17 00:00:00 2001 From: Kristof Willaert Date: Wed, 20 Nov 2024 21:12:34 +0100 Subject: [PATCH 1/2] Update build and deploy tasks for Ubuntu 20.04 --- lib/tasks/build.rake | 7 ++++ lib/tasks/build_artifact.rake | 34 ++++++++++++++++++ lib/tasks/{uitdatabank-frontend => }/prerm | 0 .../uitdatabank-frontend.service | 6 ++-- lib/tasks/uitdatabank-frontend/build.rake | 9 ----- .../uitdatabank-frontend/build_artifact.rake | 36 ------------------- 6 files changed, 44 insertions(+), 48 deletions(-) create mode 100644 lib/tasks/build.rake create mode 100644 lib/tasks/build_artifact.rake rename lib/tasks/{uitdatabank-frontend => }/prerm (100%) rename lib/tasks/{uitdatabank-frontend => }/uitdatabank-frontend.service (75%) delete mode 100644 lib/tasks/uitdatabank-frontend/build.rake delete mode 100644 lib/tasks/uitdatabank-frontend/build_artifact.rake diff --git a/lib/tasks/build.rake b/lib/tasks/build.rake new file mode 100644 index 000000000..b3c978e1a --- /dev/null +++ b/lib/tasks/build.rake @@ -0,0 +1,7 @@ +desc "Build binaries" +task :build do |task| + system('yarn install') or exit 1 + system('yarn build') or exit 1 + system('rm -rf node_modules') or exit 1 + system('yarn install --production') or exit 1 +end diff --git a/lib/tasks/build_artifact.rake b/lib/tasks/build_artifact.rake new file mode 100644 index 000000000..5c4f58294 --- /dev/null +++ b/lib/tasks/build_artifact.rake @@ -0,0 +1,34 @@ +desc "Create a debian package from the binaries." +task :build_artifact do |task| + + calver_version = ENV['PIPELINE_VERSION'].nil? ? Time.now.strftime("%Y.%m.%d.%H%M%S") : ENV['PIPELINE_VERSION'] + git_short_ref = `git rev-parse --short HEAD`.strip + version = ENV['ARTIFACT_VERSION'].nil? ? "#{calver_version}+sha.#{git_short_ref}" : ENV['ARTIFACT_VERSION'] + artifact_name = 'uitdatabank-frontend' + vendor = 'publiq VZW' + maintainer = 'Infra publiq ' + license = 'Apache-2.0' + description = 'Frontend for UiTdatabank' + source = 'https://github.com/cultuurnet/udb3-frontend' + build_url = ENV['JOB_DISPLAY_URL'].nil? ? '' : ENV['JOB_DISPLAY_URL'] + + FileUtils.mkdir_p('pkg') + + system('touch .env') or exit 1 + + system("fpm -s dir -t deb -n #{artifact_name} -v #{version} -a all -p pkg \ + -x '.git*' -x pkg -x vendor -x lib -x Rakefile -x Gemfile -x Gemfile.lock \ + -x .bundle -x 'Jenkinsfile*' \ + --prefix /var/www/udb3-frontend \ + --config-files /var/www/udb3-frontend/.env \ + --deb-systemd lib/tasks/uitdatabank-frontend.service \ + --before-remove lib/tasks/prerm \ + --deb-user www-data --deb-group www-data \ + --description '#{description}' --url '#{source}' --vendor '#{vendor}' \ + --license '#{license}' -m '#{maintainer}' \ + --deb-field 'Pipeline-Version: #{calver_version}' \ + --deb-field 'Git-Ref: #{git_short_ref}' \ + --deb-field 'Build-Url: #{build_url}' \ + ." + ) or exit 1 +end diff --git a/lib/tasks/uitdatabank-frontend/prerm b/lib/tasks/prerm similarity index 100% rename from lib/tasks/uitdatabank-frontend/prerm rename to lib/tasks/prerm diff --git a/lib/tasks/uitdatabank-frontend/uitdatabank-frontend.service b/lib/tasks/uitdatabank-frontend.service similarity index 75% rename from lib/tasks/uitdatabank-frontend/uitdatabank-frontend.service rename to lib/tasks/uitdatabank-frontend.service index 81703ecaf..2521e0fe1 100644 --- a/lib/tasks/uitdatabank-frontend/uitdatabank-frontend.service +++ b/lib/tasks/uitdatabank-frontend.service @@ -5,10 +5,10 @@ After=syslog.target network.target remote-fs.target nss-lookup.target [Service] User=www-data Group=www-data -PIDFile=/var/run/udb3-frontend.pid -Environment=NEXT_HOST=localhost +PIDFile=/var/run/uitdatabank-frontend.pid +Environment=NEXT_HOST=127.0.0.1 Environment=NEXT_PORT=4000 -EnvironmentFile=-/etc/default/udb3-frontend +EnvironmentFile=-/etc/default/uitdatabank-frontend WorkingDirectory=/var/www/udb3-frontend ExecStart=/usr/bin/node node_modules/next/dist/bin/next start -H ${NEXT_HOST} -p ${NEXT_PORT} Restart=on-failure diff --git a/lib/tasks/uitdatabank-frontend/build.rake b/lib/tasks/uitdatabank-frontend/build.rake deleted file mode 100644 index 4e07ccffe..000000000 --- a/lib/tasks/uitdatabank-frontend/build.rake +++ /dev/null @@ -1,9 +0,0 @@ -namespace 'uitdatabank-frontend' do - desc "Build binaries" - task :build do |task| - system('yarn install') or exit 1 - system('yarn build') or exit 1 - system('rm -rf node_modules') or exit 1 - system('yarn install --production') or exit 1 - end -end diff --git a/lib/tasks/uitdatabank-frontend/build_artifact.rake b/lib/tasks/uitdatabank-frontend/build_artifact.rake deleted file mode 100644 index 330cd491b..000000000 --- a/lib/tasks/uitdatabank-frontend/build_artifact.rake +++ /dev/null @@ -1,36 +0,0 @@ -namespace 'uitdatabank-frontend' do - desc "Create a debian package from the binaries." - task :build_artifact do |task| - - calver_version = ENV['PIPELINE_VERSION'].nil? ? Time.now.strftime("%Y.%m.%d.%H%M%S") : ENV['PIPELINE_VERSION'] - git_short_ref = `git rev-parse --short HEAD`.strip - version = ENV['ARTIFACT_VERSION'].nil? ? "#{calver_version}+sha.#{git_short_ref}" : ENV['ARTIFACT_VERSION'] - artifact_name = 'uitdatabank-frontend' - vendor = 'publiq VZW' - maintainer = 'Infra publiq ' - license = 'Apache-2.0' - description = 'Frontend for UiTdatabank 3' - source = 'https://github.com/cultuurnet/udb3-frontend' - build_url = ENV['JOB_DISPLAY_URL'].nil? ? "" : ENV['JOB_DISPLAY_URL'] - - FileUtils.mkdir_p('pkg') - - system('touch .env') or exit 1 - - system("fpm -s dir -t deb -n #{artifact_name} -v #{version} -a all -p pkg \ - -x '.git*' -x pkg -x vendor -x lib -x Rakefile -x Gemfile -x Gemfile.lock \ - -x .bundle -x 'Jenkinsfile*' \ - --prefix /var/www/udb3-frontend \ - --config-files /var/www/udb3-frontend/.env \ - --deb-systemd lib/tasks/uitdatabank-frontend/uitdatabank-frontend.service \ - --before-remove lib/tasks/uitdatabank-frontend/prerm \ - --deb-user www-data --deb-group www-data \ - --description '#{description}' --url '#{source}' --vendor '#{vendor}' \ - --license '#{license}' -m '#{maintainer}' \ - --deb-field 'Pipeline-Version: #{calver_version}' \ - --deb-field 'Git-Ref: #{git_short_ref}' \ - --deb-field 'Build-Url: #{build_url}' \ - ." - ) or exit 1 - end -end From 363fd5e1354a9ceee670950ac50f8bf4dec74a9f Mon Sep 17 00:00:00 2001 From: Kristof Willaert Date: Wed, 20 Nov 2024 21:13:02 +0100 Subject: [PATCH 2/2] Update Jenkinsfile for Ubuntu 20.04 --- Jenkinsfile | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 0f68b5bd5..f81cee2c2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -15,7 +15,7 @@ pipeline { } stage('Setup and build') { - agent { label 'ubuntu && 16.04 && nodejs16' } + agent { label 'ubuntu && 20.04 && nodejs16' } environment { GIT_SHORT_COMMIT = build.shortCommitRef() ARTIFACT_VERSION = "${env.PIPELINE_VERSION}" + '+sha.' + "${env.GIT_SHORT_COMMIT}" @@ -28,12 +28,12 @@ pipeline { } stage('Build') { steps { - sh label: 'Build binaries', script: 'bundle exec rake uitdatabank-frontend:build' + sh label: 'Build binaries', script: 'bundle exec rake build' } } stage('Build artifact') { steps { - sh label: 'Build artifact', script: "bundle exec rake uitdatabank-frontend:build_artifact ARTIFACT_VERSION=${env.ARTIFACT_VERSION}" + sh label: 'Build artifact', script: "bundle exec rake build_artifact ARTIFACT_VERSION=${env.ARTIFACT_VERSION}" archiveArtifacts artifacts: "pkg/*${env.ARTIFACT_VERSION}*.deb", onlyIfSuccessful: true } } @@ -67,59 +67,59 @@ pipeline { APPLICATION_ENVIRONMENT = 'development' } steps { - publishAptlySnapshot snapshotName: "${env.REPOSITORY_NAME}-${env.PIPELINE_VERSION}", publishTarget: "${env.REPOSITORY_NAME}-${env.APPLICATION_ENVIRONMENT}", distributions: 'xenial' + publishAptlySnapshot snapshotName: "${env.REPOSITORY_NAME}-${env.PIPELINE_VERSION}", publishTarget: "${env.REPOSITORY_NAME}-${env.APPLICATION_ENVIRONMENT}", distributions: 'focal' } } stage('Deploy to acceptance') { - agent any + agent { label 'ubuntu && 20.04' } options { skipDefaultCheckout() } environment { APPLICATION_ENVIRONMENT = 'acceptance' } steps { - publishAptlySnapshot snapshotName: "${env.REPOSITORY_NAME}-${env.PIPELINE_VERSION}", publishTarget: "${env.REPOSITORY_NAME}-${env.APPLICATION_ENVIRONMENT}", distributions: 'xenial' - triggerDeployment nodeName: 'udb3-web-acc02', timeout: 600 + publishAptlySnapshot snapshotName: "${env.REPOSITORY_NAME}-${env.PIPELINE_VERSION}", publishTarget: "${env.REPOSITORY_NAME}-${env.APPLICATION_ENVIRONMENT}", distributions: 'focal' + triggerDeployment nodeName: 'uitdatabank-web-acc01', timeout: 600 } post { always { - sendBuildNotification to: '#upw-ops', message: "Pipeline <${env.RUN_DISPLAY_URL}|${env.JOB_NAME} [${currentBuild.displayName}]>: deployed to *${env.APPLICATION_ENVIRONMENT}*" + sendBuildNotification to: '#upw-ops', message: "Pipeline <${env.RUN_DISPLAY_URL}|${util.getJobDisplayName()} [${currentBuild.displayName}]>: deployed to *${env.APPLICATION_ENVIRONMENT}*" } } } stage('Deploy to testing') { input { message "Deploy to Testing?" } - agent any + agent { label 'ubuntu && 20.04' } options { skipDefaultCheckout() } environment { APPLICATION_ENVIRONMENT = 'testing' } steps { - publishAptlySnapshot snapshotName: "${env.REPOSITORY_NAME}-${env.PIPELINE_VERSION}", publishTarget: "${env.REPOSITORY_NAME}-${env.APPLICATION_ENVIRONMENT}", distributions: 'xenial' - triggerDeployment nodeName: 'udb3-web-test03', timeout: 600 + publishAptlySnapshot snapshotName: "${env.REPOSITORY_NAME}-${env.PIPELINE_VERSION}", publishTarget: "${env.REPOSITORY_NAME}-${env.APPLICATION_ENVIRONMENT}", distributions: 'focal' + triggerDeployment nodeName: 'uitdatabank-web-test01', timeout: 600 } post { always { - sendBuildNotification to: '#upw-ops', message: "Pipeline <${env.RUN_DISPLAY_URL}|${env.JOB_NAME} [${currentBuild.displayName}]>: deployed to *${env.APPLICATION_ENVIRONMENT}*" + sendBuildNotification to: '#upw-ops', message: "Pipeline <${env.RUN_DISPLAY_URL}|${util.getJobDisplayName()} [${currentBuild.displayName}]>: deployed to *${env.APPLICATION_ENVIRONMENT}*" } } } stage('Deploy to production') { input { message "Deploy to Production?" } - agent any + agent { label 'ubuntu && 20.04' } options { skipDefaultCheckout() } environment { APPLICATION_ENVIRONMENT = 'production' } steps { - publishAptlySnapshot snapshotName: "${env.REPOSITORY_NAME}-${env.PIPELINE_VERSION}", publishTarget: "${env.REPOSITORY_NAME}-${env.APPLICATION_ENVIRONMENT}", distributions: 'xenial' - triggerDeployment nodeName: 'udb3-web-prod03', timeout: 600 + publishAptlySnapshot snapshotName: "${env.REPOSITORY_NAME}-${env.PIPELINE_VERSION}", publishTarget: "${env.REPOSITORY_NAME}-${env.APPLICATION_ENVIRONMENT}", distributions: 'focal' + triggerDeployment nodeName: 'uitdatabank-web-prod01', timeout: 600 } post { always { - sendBuildNotification to: '#upw-ops', message: "Pipeline <${env.RUN_DISPLAY_URL}|${env.JOB_NAME} [${currentBuild.displayName}]>: deployed to *${env.APPLICATION_ENVIRONMENT}*" + sendBuildNotification to: '#upw-ops', message: "Pipeline <${env.RUN_DISPLAY_URL}|${util.getJobDisplayName()} [${currentBuild.displayName}]>: deployed to *${env.APPLICATION_ENVIRONMENT}*" } cleanup { cleanupAptlySnapshots repository: env.REPOSITORY_NAME @@ -128,7 +128,7 @@ pipeline { } stage('Tag release') { - agent { label 'ubuntu && 16.04' } + agent any steps { copyArtifacts filter: 'pkg/*.deb', projectName: env.JOB_NAME, flatten: true, selector: specific(env.BUILD_NUMBER) tagRelease commitHash: artifact.metadata(artifactFilter: '*.deb', field: 'git-ref')