Jenkinsfile 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. pipeline {
  2. agent {
  3. label 'X86-64-MULTI'
  4. }
  5. // Input to determine if this is a package check
  6. parameters {
  7. string(defaultValue: 'false', description: 'package check run', name: 'PACKAGE_CHECK')
  8. }
  9. // Configuration for the variables used for this specific repo
  10. environment {
  11. BUILDS_DISCORD=credentials('build_webhook_url')
  12. GITHUB_TOKEN=credentials('498b4638-2d02-4ce5-832d-8a57d01d97ab')
  13. EXT_GIT_BRANCH = 'master'
  14. EXT_USER = 'thibaud-rohmer'
  15. EXT_REPO = 'PhotoShow'
  16. BUILD_VERSION_ARG = 'PHOTOSHOW_COMMIT'
  17. LS_USER = 'linuxserver'
  18. LS_REPO = 'docker-photoshow'
  19. CONTAINER_NAME = 'photoshow'
  20. DOCKERHUB_IMAGE = 'linuxserver/photoshow'
  21. DEV_DOCKERHUB_IMAGE = 'lsiodev/photoshow'
  22. PR_DOCKERHUB_IMAGE = 'lspipepr/photoshow'
  23. DIST_IMAGE = 'alpine'
  24. MULTIARCH='true'
  25. CI='true'
  26. CI_WEB='true'
  27. CI_PORT='80'
  28. CI_SSL='false'
  29. CI_DELAY='120'
  30. CI_DOCKERENV='TZ=US/Pacific'
  31. CI_AUTH='user:password'
  32. CI_WEBPATH=''
  33. }
  34. stages {
  35. // Setup all the basic environment variables needed for the build
  36. stage("Set ENV Variables base"){
  37. steps{
  38. script{
  39. env.EXIT_STATUS = ''
  40. env.LS_RELEASE = sh(
  41. script: '''docker run --rm alexeiled/skopeo sh -c 'skopeo inspect docker://docker.io/'${DOCKERHUB_IMAGE}':latest 2>/dev/null' | jq -r '.Labels.build_version' | awk '{print $3}' | grep '\\-ls' || : ''',
  42. returnStdout: true).trim()
  43. env.LS_RELEASE_NOTES = sh(
  44. script: '''cat readme-vars.yml | awk -F \\" '/date: "[0-9][0-9].[0-9][0-9].[0-9][0-9]:/ {print $4;exit;}' | sed -E ':a;N;$!ba;s/\\r{0,1}\\n/\\\\n/g' ''',
  45. returnStdout: true).trim()
  46. env.GITHUB_DATE = sh(
  47. script: '''date '+%Y-%m-%dT%H:%M:%S%:z' ''',
  48. returnStdout: true).trim()
  49. env.COMMIT_SHA = sh(
  50. script: '''git rev-parse HEAD''',
  51. returnStdout: true).trim()
  52. env.CODE_URL = 'https://github.com/' + env.LS_USER + '/' + env.LS_REPO + '/commit/' + env.GIT_COMMIT
  53. env.DOCKERHUB_LINK = 'https://hub.docker.com/r/' + env.DOCKERHUB_IMAGE + '/tags/'
  54. env.PULL_REQUEST = env.CHANGE_ID
  55. env.LICENSE_TAG = sh(
  56. script: '''#!/bin/bash
  57. if [ -e LICENSE ] ; then
  58. cat LICENSE | md5sum | cut -c1-8
  59. else
  60. echo none
  61. fi''',
  62. returnStdout: true).trim()
  63. }
  64. script{
  65. env.LS_RELEASE_NUMBER = sh(
  66. script: '''echo ${LS_RELEASE} |sed 's/^.*-ls//g' ''',
  67. returnStdout: true).trim()
  68. }
  69. script{
  70. env.LS_TAG_NUMBER = sh(
  71. script: '''#! /bin/bash
  72. tagsha=$(git rev-list -n 1 ${LS_RELEASE} 2>/dev/null)
  73. if [ "${tagsha}" == "${COMMIT_SHA}" ]; then
  74. echo ${LS_RELEASE_NUMBER}
  75. elif [ -z "${GIT_COMMIT}" ]; then
  76. echo ${LS_RELEASE_NUMBER}
  77. else
  78. echo $((${LS_RELEASE_NUMBER} + 1))
  79. fi''',
  80. returnStdout: true).trim()
  81. }
  82. }
  83. }
  84. /* #######################
  85. Package Version Tagging
  86. ####################### */
  87. // Grab the current package versions in Git to determine package tag
  88. stage("Set Package tag"){
  89. steps{
  90. script{
  91. env.PACKAGE_TAG = sh(
  92. script: '''#!/bin/bash
  93. if [ -e package_versions.txt ] ; then
  94. cat package_versions.txt | md5sum | cut -c1-8
  95. else
  96. echo none
  97. fi''',
  98. returnStdout: true).trim()
  99. }
  100. }
  101. }
  102. /* ########################
  103. External Release Tagging
  104. ######################## */
  105. // If this is a github commit trigger determine the current commit at head
  106. stage("Set ENV github_commit"){
  107. steps{
  108. script{
  109. env.EXT_RELEASE = sh(
  110. script: '''curl -s https://api.github.com/repos/${EXT_USER}/${EXT_REPO}/commits/${EXT_GIT_BRANCH} | jq -r '. | .sha' | cut -c1-8 ''',
  111. returnStdout: true).trim()
  112. }
  113. }
  114. }
  115. // If this is a github commit trigger Set the external release link
  116. stage("Set ENV commit_link"){
  117. steps{
  118. script{
  119. env.RELEASE_LINK = 'https://github.com/' + env.EXT_USER + '/' + env.EXT_REPO + '/commit/' + env.EXT_RELEASE
  120. }
  121. }
  122. }
  123. // Sanitize the release tag and strip illegal docker or github characters
  124. stage("Sanitize tag"){
  125. steps{
  126. script{
  127. env.EXT_RELEASE_CLEAN = sh(
  128. script: '''echo ${EXT_RELEASE} | sed 's/[~,%@+;:/]//g' ''',
  129. returnStdout: true).trim()
  130. }
  131. }
  132. }
  133. // If this is a master build use live docker endpoints
  134. stage("Set ENV live build"){
  135. when {
  136. branch "master"
  137. environment name: 'CHANGE_ID', value: ''
  138. }
  139. steps {
  140. script{
  141. env.IMAGE = env.DOCKERHUB_IMAGE
  142. if (env.MULTIARCH == 'true') {
  143. env.CI_TAGS = 'amd64-' + env.EXT_RELEASE_CLEAN + '-ls' + env.LS_TAG_NUMBER + '|arm32v7-' + env.EXT_RELEASE_CLEAN + '-ls' + env.LS_TAG_NUMBER + '|arm64v8-' + env.EXT_RELEASE_CLEAN + '-ls' + env.LS_TAG_NUMBER
  144. } else {
  145. env.CI_TAGS = env.EXT_RELEASE_CLEAN + '-ls' + env.LS_TAG_NUMBER
  146. }
  147. env.META_TAG = env.EXT_RELEASE_CLEAN + '-ls' + env.LS_TAG_NUMBER
  148. }
  149. }
  150. }
  151. // If this is a dev build use dev docker endpoints
  152. stage("Set ENV dev build"){
  153. when {
  154. not {branch "master"}
  155. environment name: 'CHANGE_ID', value: ''
  156. }
  157. steps {
  158. script{
  159. env.IMAGE = env.DEV_DOCKERHUB_IMAGE
  160. if (env.MULTIARCH == 'true') {
  161. env.CI_TAGS = 'amd64-' + env.EXT_RELEASE_CLEAN + '-pkg-' + env.PACKAGE_TAG + '-dev-' + env.COMMIT_SHA + '|arm32v7-' + env.EXT_RELEASE_CLEAN + '-pkg-' + env.PACKAGE_TAG + '-dev-' + env.COMMIT_SHA + '|arm64v8-' + env.EXT_RELEASE_CLEAN + '-pkg-' + env.PACKAGE_TAG + '-dev-' + env.COMMIT_SHA
  162. } else {
  163. env.CI_TAGS = env.EXT_RELEASE_CLEAN + '-pkg-' + env.PACKAGE_TAG + '-dev-' + env.COMMIT_SHA
  164. }
  165. env.META_TAG = env.EXT_RELEASE_CLEAN + '-pkg-' + env.PACKAGE_TAG + '-dev-' + env.COMMIT_SHA
  166. env.DOCKERHUB_LINK = 'https://hub.docker.com/r/' + env.DEV_DOCKERHUB_IMAGE + '/tags/'
  167. }
  168. }
  169. }
  170. // If this is a pull request build use dev docker endpoints
  171. stage("Set ENV PR build"){
  172. when {
  173. not {environment name: 'CHANGE_ID', value: ''}
  174. }
  175. steps {
  176. script{
  177. env.IMAGE = env.PR_DOCKERHUB_IMAGE
  178. if (env.MULTIARCH == 'true') {
  179. env.CI_TAGS = 'amd64-' + env.EXT_RELEASE_CLEAN + '-pkg-' + env.PACKAGE_TAG + '-pr-' + env.PULL_REQUEST + '|arm32v7-' + env.EXT_RELEASE_CLEAN + '-pkg-' + env.PACKAGE_TAG + '-pr-' + env.PULL_REQUEST + '|arm64v8-' + env.EXT_RELEASE_CLEAN + '-pkg-' + env.PACKAGE_TAG + '-pr-' + env.PULL_REQUEST
  180. } else {
  181. env.CI_TAGS = env.EXT_RELEASE_CLEAN + '-pkg-' + env.PACKAGE_TAG + '-pr-' + env.PULL_REQUEST
  182. }
  183. env.META_TAG = env.EXT_RELEASE_CLEAN + '-pkg-' + env.PACKAGE_TAG + '-pr-' + env.PULL_REQUEST
  184. env.CODE_URL = 'https://github.com/' + env.LS_USER + '/' + env.LS_REPO + '/pull/' + env.PULL_REQUEST
  185. env.DOCKERHUB_LINK = 'https://hub.docker.com/r/' + env.PR_DOCKERHUB_IMAGE + '/tags/'
  186. }
  187. }
  188. }
  189. // Run ShellCheck
  190. stage('ShellCheck') {
  191. when {
  192. environment name: 'CI', value: 'true'
  193. }
  194. steps {
  195. withCredentials([
  196. string(credentialsId: 'spaces-key', variable: 'DO_KEY'),
  197. string(credentialsId: 'spaces-secret', variable: 'DO_SECRET')
  198. ]) {
  199. script{
  200. env.SHELLCHECK_URL = 'https://lsio-ci.ams3.digitaloceanspaces.com/' + env.IMAGE + '/' + env.META_TAG + '/shellcheck-result.xml'
  201. }
  202. sh '''curl -sL https://raw.githubusercontent.com/linuxserver/docker-shellcheck/master/checkrun.sh | /bin/bash'''
  203. sh '''#! /bin/bash
  204. set -e
  205. docker pull lsiodev/spaces-file-upload:latest
  206. docker run --rm \
  207. -e DESTINATION=\"${IMAGE}/${META_TAG}/shellcheck-result.xml\" \
  208. -e FILE_NAME="shellcheck-result.xml" \
  209. -e MIMETYPE="text/xml" \
  210. -v ${WORKSPACE}:/mnt \
  211. -e SECRET_KEY=\"${DO_SECRET}\" \
  212. -e ACCESS_KEY=\"${DO_KEY}\" \
  213. -t lsiodev/spaces-file-upload:latest \
  214. python /upload.py'''
  215. }
  216. }
  217. }
  218. // Use helper containers to render templated files
  219. stage('Update-Templates') {
  220. when {
  221. branch "master"
  222. environment name: 'CHANGE_ID', value: ''
  223. expression {
  224. env.CONTAINER_NAME != null
  225. }
  226. }
  227. steps {
  228. sh '''#! /bin/bash
  229. set -e
  230. TEMPDIR=$(mktemp -d)
  231. docker pull linuxserver/jenkins-builder:latest
  232. docker run --rm -e CONTAINER_NAME=${CONTAINER_NAME} -e GITHUB_BRANCH=master -v ${TEMPDIR}:/ansible/jenkins linuxserver/jenkins-builder:latest
  233. docker pull linuxserver/doc-builder:latest
  234. docker run --rm -e CONTAINER_NAME=${CONTAINER_NAME} -e GITHUB_BRANCH=master -v ${TEMPDIR}:/ansible/readme linuxserver/doc-builder:latest
  235. if [ "$(md5sum ${TEMPDIR}/${LS_REPO}/Jenkinsfile | awk '{ print $1 }')" != "$(md5sum Jenkinsfile | awk '{ print $1 }')" ] || \
  236. [ "$(md5sum ${TEMPDIR}/${CONTAINER_NAME}/README.md | awk '{ print $1 }')" != "$(md5sum README.md | awk '{ print $1 }')" ] || \
  237. [ "$(cat ${TEMPDIR}/${LS_REPO}/LICENSE | md5sum | cut -c1-8)" != "${LICENSE_TAG}" ]; then
  238. mkdir -p ${TEMPDIR}/repo
  239. git clone https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/repo/${LS_REPO}
  240. git --git-dir ${TEMPDIR}/repo/${LS_REPO}/.git checkout -f master
  241. cp ${TEMPDIR}/${CONTAINER_NAME}/README.md ${TEMPDIR}/repo/${LS_REPO}/
  242. cp ${TEMPDIR}/docker-${CONTAINER_NAME}/Jenkinsfile ${TEMPDIR}/repo/${LS_REPO}/
  243. cp ${TEMPDIR}/docker-${CONTAINER_NAME}/LICENSE ${TEMPDIR}/repo/${LS_REPO}/
  244. cd ${TEMPDIR}/repo/${LS_REPO}/
  245. git --git-dir ${TEMPDIR}/repo/${LS_REPO}/.git add Jenkinsfile README.md LICENSE
  246. git --git-dir ${TEMPDIR}/repo/${LS_REPO}/.git commit -m 'Bot Updating Templated Files'
  247. git --git-dir ${TEMPDIR}/repo/${LS_REPO}/.git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git --all
  248. echo "true" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
  249. else
  250. echo "false" > /tmp/${COMMIT_SHA}-${BUILD_NUMBER}
  251. fi
  252. mkdir -p ${TEMPDIR}/gitbook
  253. git clone https://github.com/linuxserver/docker-documentation.git ${TEMPDIR}/gitbook/docker-documentation
  254. if [[ "${BRANCH_NAME}" == "master" ]] && [[ (! -f ${TEMPDIR}/gitbook/docker-documentation/images/docker-${CONTAINER_NAME}.md) || ("$(md5sum ${TEMPDIR}/gitbook/docker-documentation/images/docker-${CONTAINER_NAME}.md | awk '{ print $1 }')" != "$(md5sum ${TEMPDIR}/${CONTAINER_NAME}/docker-${CONTAINER_NAME}.md | awk '{ print $1 }')") ]]; then
  255. cp ${TEMPDIR}/${CONTAINER_NAME}/docker-${CONTAINER_NAME}.md ${TEMPDIR}/gitbook/docker-documentation/images/
  256. cd ${TEMPDIR}/gitbook/docker-documentation/
  257. git add images/docker-${CONTAINER_NAME}.md
  258. git commit -m 'Bot Updating Templated Files'
  259. git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/linuxserver/docker-documentation.git --all
  260. fi
  261. rm -Rf ${TEMPDIR}'''
  262. script{
  263. env.FILES_UPDATED = sh(
  264. script: '''cat /tmp/${COMMIT_SHA}-${BUILD_NUMBER}''',
  265. returnStdout: true).trim()
  266. }
  267. }
  268. }
  269. // Exit the build if the Templated files were just updated
  270. stage('Template-exit') {
  271. when {
  272. branch "master"
  273. environment name: 'CHANGE_ID', value: ''
  274. environment name: 'FILES_UPDATED', value: 'true'
  275. expression {
  276. env.CONTAINER_NAME != null
  277. }
  278. }
  279. steps {
  280. script{
  281. env.EXIT_STATUS = 'ABORTED'
  282. }
  283. }
  284. }
  285. /* ###############
  286. Build Container
  287. ############### */
  288. // Build Docker container for push to LS Repo
  289. stage('Build-Single') {
  290. when {
  291. environment name: 'MULTIARCH', value: 'false'
  292. environment name: 'EXIT_STATUS', value: ''
  293. }
  294. steps {
  295. sh "docker build --no-cache --pull -t ${IMAGE}:${META_TAG} \
  296. --build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
  297. }
  298. }
  299. // Build MultiArch Docker containers for push to LS Repo
  300. stage('Build-Multi') {
  301. when {
  302. environment name: 'MULTIARCH', value: 'true'
  303. environment name: 'EXIT_STATUS', value: ''
  304. }
  305. parallel {
  306. stage('Build X86') {
  307. steps {
  308. sh "docker build --no-cache --pull -t ${IMAGE}:amd64-${META_TAG} \
  309. --build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
  310. }
  311. }
  312. stage('Build ARMHF') {
  313. agent {
  314. label 'ARMHF'
  315. }
  316. steps {
  317. withCredentials([
  318. [
  319. $class: 'UsernamePasswordMultiBinding',
  320. credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
  321. usernameVariable: 'DOCKERUSER',
  322. passwordVariable: 'DOCKERPASS'
  323. ]
  324. ]) {
  325. echo 'Logging into DockerHub'
  326. sh '''#! /bin/bash
  327. echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
  328. '''
  329. sh "docker build --no-cache --pull -f Dockerfile.armhf -t ${IMAGE}:arm32v7-${META_TAG} \
  330. --build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
  331. sh "docker tag ${IMAGE}:arm32v7-${META_TAG} lsiodev/buildcache:arm32v7-${COMMIT_SHA}-${BUILD_NUMBER}"
  332. sh "docker push lsiodev/buildcache:arm32v7-${COMMIT_SHA}-${BUILD_NUMBER}"
  333. sh '''docker rmi \
  334. ${IMAGE}:arm32v7-${META_TAG} \
  335. lsiodev/buildcache:arm32v7-${COMMIT_SHA}-${BUILD_NUMBER} || :'''
  336. }
  337. }
  338. }
  339. stage('Build ARM64') {
  340. agent {
  341. label 'ARM64'
  342. }
  343. steps {
  344. withCredentials([
  345. [
  346. $class: 'UsernamePasswordMultiBinding',
  347. credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
  348. usernameVariable: 'DOCKERUSER',
  349. passwordVariable: 'DOCKERPASS'
  350. ]
  351. ]) {
  352. echo 'Logging into DockerHub'
  353. sh '''#! /bin/bash
  354. echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
  355. '''
  356. sh "docker build --no-cache --pull -f Dockerfile.aarch64 -t ${IMAGE}:arm64v8-${META_TAG} \
  357. --build-arg ${BUILD_VERSION_ARG}=${EXT_RELEASE} --build-arg VERSION=\"${META_TAG}\" --build-arg BUILD_DATE=${GITHUB_DATE} ."
  358. sh "docker tag ${IMAGE}:arm64v8-${META_TAG} lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}"
  359. sh "docker push lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}"
  360. sh '''docker rmi \
  361. ${IMAGE}:arm64v8-${META_TAG} \
  362. lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER} || :'''
  363. }
  364. }
  365. }
  366. }
  367. }
  368. // Take the image we just built and dump package versions for comparison
  369. stage('Update-packages') {
  370. when {
  371. branch "master"
  372. environment name: 'CHANGE_ID', value: ''
  373. environment name: 'EXIT_STATUS', value: ''
  374. }
  375. steps {
  376. sh '''#! /bin/bash
  377. set -e
  378. TEMPDIR=$(mktemp -d)
  379. if [ "${MULTIARCH}" == "true" ]; then
  380. LOCAL_CONTAINER=${IMAGE}:amd64-${META_TAG}
  381. else
  382. LOCAL_CONTAINER=${IMAGE}:${META_TAG}
  383. fi
  384. if [ "${DIST_IMAGE}" == "alpine" ]; then
  385. docker run --rm --entrypoint '/bin/sh' -v ${TEMPDIR}:/tmp ${LOCAL_CONTAINER} -c '\
  386. apk info -v > /tmp/package_versions.txt && \
  387. sort -o /tmp/package_versions.txt /tmp/package_versions.txt && \
  388. chmod 777 /tmp/package_versions.txt'
  389. elif [ "${DIST_IMAGE}" == "ubuntu" ]; then
  390. docker run --rm --entrypoint '/bin/sh' -v ${TEMPDIR}:/tmp ${LOCAL_CONTAINER} -c '\
  391. apt list -qq --installed | sed "s#/.*now ##g" | cut -d" " -f1 > /tmp/package_versions.txt && \
  392. sort -o /tmp/package_versions.txt /tmp/package_versions.txt && \
  393. chmod 777 /tmp/package_versions.txt'
  394. fi
  395. NEW_PACKAGE_TAG=$(md5sum ${TEMPDIR}/package_versions.txt | cut -c1-8 )
  396. echo "Package tag sha from current packages in buit container is ${NEW_PACKAGE_TAG} comparing to old ${PACKAGE_TAG} from github"
  397. if [ "${NEW_PACKAGE_TAG}" != "${PACKAGE_TAG}" ]; then
  398. git clone https://github.com/${LS_USER}/${LS_REPO}.git ${TEMPDIR}/${LS_REPO}
  399. git --git-dir ${TEMPDIR}/${LS_REPO}/.git checkout -f master
  400. cp ${TEMPDIR}/package_versions.txt ${TEMPDIR}/${LS_REPO}/
  401. cd ${TEMPDIR}/${LS_REPO}/
  402. wait
  403. git add package_versions.txt
  404. git commit -m 'Bot Updating Package Versions'
  405. git push https://LinuxServer-CI:${GITHUB_TOKEN}@github.com/${LS_USER}/${LS_REPO}.git --all
  406. echo "true" > /tmp/packages-${COMMIT_SHA}-${BUILD_NUMBER}
  407. echo "Package tag updated, stopping build process"
  408. else
  409. echo "false" > /tmp/packages-${COMMIT_SHA}-${BUILD_NUMBER}
  410. echo "Package tag is same as previous continue with build process"
  411. fi
  412. rm -Rf ${TEMPDIR}'''
  413. script{
  414. env.PACKAGE_UPDATED = sh(
  415. script: '''cat /tmp/packages-${COMMIT_SHA}-${BUILD_NUMBER}''',
  416. returnStdout: true).trim()
  417. }
  418. }
  419. }
  420. // Exit the build if the package file was just updated
  421. stage('PACKAGE-exit') {
  422. when {
  423. branch "master"
  424. environment name: 'CHANGE_ID', value: ''
  425. environment name: 'PACKAGE_UPDATED', value: 'true'
  426. environment name: 'EXIT_STATUS', value: ''
  427. }
  428. steps {
  429. script{
  430. env.EXIT_STATUS = 'ABORTED'
  431. }
  432. }
  433. }
  434. // Exit the build if this is just a package check and there are no changes to push
  435. stage('PACKAGECHECK-exit') {
  436. when {
  437. branch "master"
  438. environment name: 'CHANGE_ID', value: ''
  439. environment name: 'PACKAGE_UPDATED', value: 'false'
  440. environment name: 'EXIT_STATUS', value: ''
  441. expression {
  442. params.PACKAGE_CHECK == 'true'
  443. }
  444. }
  445. steps {
  446. script{
  447. env.EXIT_STATUS = 'ABORTED'
  448. }
  449. }
  450. }
  451. /* #######
  452. Testing
  453. ####### */
  454. // Run Container tests
  455. stage('Test') {
  456. when {
  457. environment name: 'CI', value: 'true'
  458. environment name: 'EXIT_STATUS', value: ''
  459. }
  460. steps {
  461. withCredentials([
  462. string(credentialsId: 'spaces-key', variable: 'DO_KEY'),
  463. string(credentialsId: 'spaces-secret', variable: 'DO_SECRET')
  464. ]) {
  465. script{
  466. env.CI_URL = 'https://lsio-ci.ams3.digitaloceanspaces.com/' + env.IMAGE + '/' + env.META_TAG + '/index.html'
  467. }
  468. sh '''#! /bin/bash
  469. set -e
  470. docker pull lsiodev/ci:latest
  471. if [ "${MULTIARCH}" == "true" ]; then
  472. docker pull lsiodev/buildcache:arm32v7-${COMMIT_SHA}-${BUILD_NUMBER}
  473. docker pull lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}
  474. docker tag lsiodev/buildcache:arm32v7-${COMMIT_SHA}-${BUILD_NUMBER} ${IMAGE}:arm32v7-${META_TAG}
  475. docker tag lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER} ${IMAGE}:arm64v8-${META_TAG}
  476. fi
  477. docker run --rm \
  478. --shm-size=1gb \
  479. -v /var/run/docker.sock:/var/run/docker.sock \
  480. -e IMAGE=\"${IMAGE}\" \
  481. -e DELAY_START=\"${CI_DELAY}\" \
  482. -e TAGS=\"${CI_TAGS}\" \
  483. -e META_TAG=\"${META_TAG}\" \
  484. -e PORT=\"${CI_PORT}\" \
  485. -e SSL=\"${CI_SSL}\" \
  486. -e BASE=\"${DIST_IMAGE}\" \
  487. -e SECRET_KEY=\"${DO_SECRET}\" \
  488. -e ACCESS_KEY=\"${DO_KEY}\" \
  489. -e DOCKER_ENV=\"${CI_DOCKERENV}\" \
  490. -e WEB_SCREENSHOT=\"${CI_WEB}\" \
  491. -e WEB_AUTH=\"${CI_AUTH}\" \
  492. -e WEB_PATH=\"${CI_WEBPATH}\" \
  493. -e DO_REGION="ams3" \
  494. -e DO_BUCKET="lsio-ci" \
  495. -t lsiodev/ci:latest \
  496. python /ci/ci.py'''
  497. }
  498. }
  499. }
  500. /* ##################
  501. Release Logic
  502. ################## */
  503. // If this is an amd64 only image only push a single image
  504. stage('Docker-Push-Single') {
  505. when {
  506. environment name: 'MULTIARCH', value: 'false'
  507. environment name: 'EXIT_STATUS', value: ''
  508. }
  509. steps {
  510. withCredentials([
  511. [
  512. $class: 'UsernamePasswordMultiBinding',
  513. credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
  514. usernameVariable: 'DOCKERUSER',
  515. passwordVariable: 'DOCKERPASS'
  516. ]
  517. ]) {
  518. echo 'Logging into DockerHub'
  519. sh '''#! /bin/bash
  520. echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
  521. '''
  522. sh "docker tag ${IMAGE}:${META_TAG} ${IMAGE}:latest"
  523. sh "docker push ${IMAGE}:latest"
  524. sh "docker push ${IMAGE}:${META_TAG}"
  525. sh '''docker rmi \
  526. ${IMAGE}:${META_TAG} \
  527. ${IMAGE}:latest || :'''
  528. }
  529. }
  530. }
  531. // If this is a multi arch release push all images and define the manifest
  532. stage('Docker-Push-Multi') {
  533. when {
  534. environment name: 'MULTIARCH', value: 'true'
  535. environment name: 'EXIT_STATUS', value: ''
  536. }
  537. steps {
  538. withCredentials([
  539. [
  540. $class: 'UsernamePasswordMultiBinding',
  541. credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
  542. usernameVariable: 'DOCKERUSER',
  543. passwordVariable: 'DOCKERPASS'
  544. ]
  545. ]) {
  546. sh '''#! /bin/bash
  547. echo $DOCKERPASS | docker login -u $DOCKERUSER --password-stdin
  548. '''
  549. sh '''#! /bin/bash
  550. if [ "${CI}" == "false" ]; then
  551. docker pull lsiodev/buildcache:arm32v7-${COMMIT_SHA}-${BUILD_NUMBER}
  552. docker pull lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER}
  553. docker tag lsiodev/buildcache:arm32v7-${COMMIT_SHA}-${BUILD_NUMBER} ${IMAGE}:arm32v7-${META_TAG}
  554. docker tag lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER} ${IMAGE}:arm64v8-${META_TAG}
  555. fi'''
  556. sh "docker tag ${IMAGE}:amd64-${META_TAG} ${IMAGE}:amd64-latest"
  557. sh "docker tag ${IMAGE}:arm32v7-${META_TAG} ${IMAGE}:arm32v7-latest"
  558. sh "docker tag ${IMAGE}:arm64v8-${META_TAG} ${IMAGE}:arm64v8-latest"
  559. sh "docker push ${IMAGE}:amd64-${META_TAG}"
  560. sh "docker push ${IMAGE}:arm32v7-${META_TAG}"
  561. sh "docker push ${IMAGE}:arm64v8-${META_TAG}"
  562. sh "docker push ${IMAGE}:amd64-latest"
  563. sh "docker push ${IMAGE}:arm32v7-latest"
  564. sh "docker push ${IMAGE}:arm64v8-latest"
  565. sh "docker manifest push --purge ${IMAGE}:latest || :"
  566. sh "docker manifest create ${IMAGE}:latest ${IMAGE}:amd64-latest ${IMAGE}:arm32v7-latest ${IMAGE}:arm64v8-latest"
  567. sh "docker manifest annotate ${IMAGE}:latest ${IMAGE}:arm32v7-latest --os linux --arch arm"
  568. sh "docker manifest annotate ${IMAGE}:latest ${IMAGE}:arm64v8-latest --os linux --arch arm64 --variant v8"
  569. sh "docker manifest push --purge ${IMAGE}:${META_TAG} || :"
  570. sh "docker manifest create ${IMAGE}:${META_TAG} ${IMAGE}:amd64-${META_TAG} ${IMAGE}:arm32v7-${META_TAG} ${IMAGE}:arm64v8-${META_TAG}"
  571. sh "docker manifest annotate ${IMAGE}:${META_TAG} ${IMAGE}:arm32v7-${META_TAG} --os linux --arch arm"
  572. sh "docker manifest annotate ${IMAGE}:${META_TAG} ${IMAGE}:arm64v8-${META_TAG} --os linux --arch arm64 --variant v8"
  573. sh "docker manifest push --purge ${IMAGE}:latest"
  574. sh "docker manifest push --purge ${IMAGE}:${META_TAG}"
  575. sh '''docker rmi \
  576. ${IMAGE}:amd64-${META_TAG} \
  577. ${IMAGE}:amd64-latest \
  578. ${IMAGE}:arm32v7-${META_TAG} \
  579. ${IMAGE}:arm32v7-latest \
  580. ${IMAGE}:arm64v8-${META_TAG} \
  581. ${IMAGE}:arm64v8-latest \
  582. lsiodev/buildcache:arm32v7-${COMMIT_SHA}-${BUILD_NUMBER} \
  583. lsiodev/buildcache:arm64v8-${COMMIT_SHA}-${BUILD_NUMBER} || :'''
  584. }
  585. }
  586. }
  587. // If this is a public release tag it in the LS Github
  588. stage('Github-Tag-Push-Release') {
  589. when {
  590. branch "master"
  591. expression {
  592. env.LS_RELEASE != env.EXT_RELEASE_CLEAN + '-ls' + env.LS_TAG_NUMBER
  593. }
  594. environment name: 'CHANGE_ID', value: ''
  595. environment name: 'EXIT_STATUS', value: ''
  596. }
  597. steps {
  598. echo "Pushing New tag for current commit ${EXT_RELEASE_CLEAN}-ls${LS_TAG_NUMBER}"
  599. sh '''curl -H "Authorization: token ${GITHUB_TOKEN}" -X POST https://api.github.com/repos/${LS_USER}/${LS_REPO}/git/tags \
  600. -d '{"tag":"'${EXT_RELEASE_CLEAN}'-ls'${LS_TAG_NUMBER}'",\
  601. "object": "'${COMMIT_SHA}'",\
  602. "message": "Tagging Release '${EXT_RELEASE_CLEAN}'-ls'${LS_TAG_NUMBER}' to master",\
  603. "type": "commit",\
  604. "tagger": {"name": "LinuxServer Jenkins","email": "jenkins@linuxserver.io","date": "'${GITHUB_DATE}'"}}' '''
  605. echo "Pushing New release for Tag"
  606. sh '''#! /bin/bash
  607. curl -s https://api.github.com/repos/${EXT_USER}/${EXT_REPO}/commits/${EXT_GIT_BRANCH} | jq '. | .commit.message' | sed 's:^.\\(.*\\).$:\\1:' > releasebody.json
  608. echo '{"tag_name":"'${EXT_RELEASE_CLEAN}'-ls'${LS_TAG_NUMBER}'",\
  609. "target_commitish": "master",\
  610. "name": "'${EXT_RELEASE_CLEAN}'-ls'${LS_TAG_NUMBER}'",\
  611. "body": "**LinuxServer Changes:**\\n\\n'${LS_RELEASE_NOTES}'\\n**'${EXT_REPO}' Changes:**\\n\\n' > start
  612. printf '","draft": false,"prerelease": false}' >> releasebody.json
  613. paste -d'\\0' start releasebody.json > releasebody.json.done
  614. curl -H "Authorization: token ${GITHUB_TOKEN}" -X POST https://api.github.com/repos/${LS_USER}/${LS_REPO}/releases -d @releasebody.json.done'''
  615. }
  616. }
  617. // Use helper container to sync the current README on master to the dockerhub endpoint
  618. stage('Sync-README') {
  619. when {
  620. environment name: 'CHANGE_ID', value: ''
  621. environment name: 'EXIT_STATUS', value: ''
  622. }
  623. steps {
  624. withCredentials([
  625. [
  626. $class: 'UsernamePasswordMultiBinding',
  627. credentialsId: '3f9ba4d5-100d-45b0-a3c4-633fd6061207',
  628. usernameVariable: 'DOCKERUSER',
  629. passwordVariable: 'DOCKERPASS'
  630. ]
  631. ]) {
  632. sh '''#! /bin/bash
  633. docker pull lsiodev/readme-sync
  634. docker run --rm=true \
  635. -e DOCKERHUB_USERNAME=$DOCKERUSER \
  636. -e DOCKERHUB_PASSWORD=$DOCKERPASS \
  637. -e GIT_REPOSITORY=${LS_USER}/${LS_REPO} \
  638. -e DOCKER_REPOSITORY=${IMAGE} \
  639. -e GIT_BRANCH=master \
  640. lsiodev/readme-sync bash -c 'node sync' '''
  641. }
  642. }
  643. }
  644. // If this is a Pull request send the CI link as a comment on it
  645. stage('Pull Request Comment') {
  646. when {
  647. not {environment name: 'CHANGE_ID', value: ''}
  648. environment name: 'CI', value: 'true'
  649. environment name: 'EXIT_STATUS', value: ''
  650. }
  651. steps {
  652. sh '''curl -H "Authorization: token ${GITHUB_TOKEN}" -X POST https://api.github.com/repos/${LS_USER}/${LS_REPO}/issues/${PULL_REQUEST}/comments \
  653. -d '{"body": "I am a bot, here are the test results for this PR: \\n'${CI_URL}' \\n'${SHELLCHECK_URL}'"}' '''
  654. }
  655. }
  656. }
  657. /* ######################
  658. Send status to Discord
  659. ###################### */
  660. post {
  661. always {
  662. script{
  663. if (env.EXIT_STATUS == "ABORTED"){
  664. sh 'echo "build aborted"'
  665. }
  666. else if (currentBuild.currentResult == "SUCCESS"){
  667. sh ''' curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 1681177,\
  668. "description": "**Build:** '${BUILD_NUMBER}'\\n**CI Results:** '${CI_URL}'\\n**ShellCheck Results:** '${SHELLCHECK_URL}'\\n**Status:** Success\\n**Job:** '${RUN_DISPLAY_URL}'\\n**Change:** '${CODE_URL}'\\n**External Release:**: '${RELEASE_LINK}'\\n**DockerHub:** '${DOCKERHUB_LINK}'\\n"}],\
  669. "username": "Jenkins"}' ${BUILDS_DISCORD} '''
  670. }
  671. else {
  672. sh ''' curl -X POST --data '{"avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png","embeds": [{"color": 16711680,\
  673. "description": "**Build:** '${BUILD_NUMBER}'\\n**CI Results:** '${CI_URL}'\\n**ShellCheck Results:** '${SHELLCHECK_URL}'\\n**Status:** failure\\n**Job:** '${RUN_DISPLAY_URL}'\\n**Change:** '${CODE_URL}'\\n**External Release:**: '${RELEASE_LINK}'\\n**DockerHub:** '${DOCKERHUB_LINK}'\\n"}],\
  674. "username": "Jenkins"}' ${BUILDS_DISCORD} '''
  675. }
  676. }
  677. }
  678. }
  679. }