Skip to content

Instantly share code, notes, and snippets.

@doevelopper
Last active October 30, 2021 10:04
Show Gist options
  • Save doevelopper/21af1b2ddc48b223edc8f548b5bc9761 to your computer and use it in GitHub Desktop.
Save doevelopper/21af1b2ddc48b223edc8f548b5bc9761 to your computer and use it in GitHub Desktop.
Gitlab ci template config for my c++ project
variables:
GIT_SSL_NO_VERIFY: "true"
GIT_DEPTH: "3"
GET_SOURCES_ATTEMPTS: "3"
ARTIFACT_DOWNLOAD_ATTEMPTS: "2"
GIT_SUBMODULE_STRATEGY: "recursive"
GIT_STRATEGY: "fetch"
GIT_CHECKOUT: "false"
CMAKE_BUILD_TYPE: "Release"
BUILD_DIRECTORY: "build-$CI_PROJECT_NAME"
SONAR_URL: "https://latest-fiasco.acme"
CACHE_DIR: "${PWD}/.cache"
BUILD_GENERATOR: "Unix Makefiles"
stages:
- validate
- initialize
- generate-sources
- process-sources
- generate-resources
- process-resources
- compile
- process-classes
- generate-test-sources
- process-test-sources
- generate-test-resources
- process-test-resources
- test-compile
- process-test-classes
- test
- prepare-package
- package
- pre-integration-test
- integration-test
- post-integration-test
- verify
- install
- deploy
- pre-clean
- clean
- post-clean
- pre-site
- site
- post-site
- site-deploy
before_script:
- date
- export NUM_CORES=$(nproc --all)
- export MAX_BUILD_CORES=$(( $(awk '( $1 == "MemTotal:" ) { print $2 }' /proc/meminfo) / ( 4 * 1024 * 1024 ) ))
- "[[ $MAX_BUILD_CORES -lt $NUM_CORES ]] && export NUM_BUILD_CORES=$MAX_BUILD_CORES || export NUM_BUILD_CORES=$NUM_CORES"
- gcc --version
- g++ --version
- cmake --version
- doxygen --version
- uncrustify --version
- export CCACHE_BASEDIR=$CI_PROJECT_DIR
- export BUILD_DATE=$(date +%Y-%m-%d-%H:%M:%S)
- export VERSION=`git describe --first-parent`
- export CMAKE_EXTRA_TARGETS="test"
# - export BUILD_GENERATOR="Unix Makefiles"
- echo "Version ${CI_BUILD_REF} / ${CI_BUILD_REF_NAME}"
- export PATH=/home/happyman/dev_install/bin:${PATH}
- export LD_LIBRARY_PATH=/home/happyman/dev_install/lib:${LD_LIBRARY_PATH}
after_script:
- date
###############################################################################
## ##
## Build templates ##
## ##
###############################################################################
.armv8l-build-template: &armv8l
before_script:
- export PATH=/opt/aarch64-linux-gnu/bin/:/opt/armv8l-linux-gnueabihf/bin/:$PATH
- export TRIPLET=armv8l-linux-gnueabihf
.aarch64-build-template: &aarch64
before_script:
- export PATH=/opt/aarch64-linux-gnu/bin/:/opt/armv8l-linux-gnueabihf/bin/:$PATH
- export TRIPLET=aarch64-linux-gnu
.vxworks-build-template: &itli86
before_script:
- export PATH=/opt/aarch64-linux-gnu/bin/:/opt/armv8l-linux-gnueabihf/bin/:$PATH
- export TRIPLET={PFX}-pentim.exe
.build_template: &build_definition
script:
- $CXX --version
- export CCACHE_BASEDIR=$CI_PROJECT_DIR
tags:
- dds-ci
.benchmark_template: &benchmark_definition
script:
- $CXX --version
- cmake --version
- ccache --version
only:
variables:
- $ENABLE_BENCHMARKS
tags:
- benchmark
artifacts:
paths:
- $CI_PROJECT_DIR/build/benchmark.txt
.variables: &build_debug_variables
CMAKE_BUILD_TYPE: "Debug"
.variables: &build_release_variables
CMAKE_BUILD_TYPE: "Release"
.variables: &cross-compile
CC: ${TRIPLET}-gcc
CXX: ${TRIPLET}-g++
NM: ${TRIPLET}-nm
AR: ${TRIPLET}-ar
AS: ${TRIPLET}-as
LD: ${TRIPLET}-ld
CPP: ${TRIPLET}-cpp
OBJDUMP: ${TRIPLET}-objdump
OBJCOPY: ${TRIPLET}-objcopy
READELF: ${TRIPLET}-readelf
RANLIB: ${TRIPLET}-ranlib
GDB: ${TRIPLET}-gdb
GFORTRAN: ${TRIPLET}-gfortran
STRINGS: ${TRIPLET}-strings
SIZE: ${TRIPLET}-size
GCOV: ${TRIPLET}-gcov
STRIP: ${TRIPLET}-strip
GPROF: ${TRIPLET}-gprof
.publish_aarch64:
stage: publish_aarch64
image: $DOCKER_BST_AARCH64
<<: *flatpak_runtimes_publish
tags:
- aarch64
variables:
ARCH: aarch64
.publish_x86_64:
stage: publish_x86_64
image: $DOCKER_BST_AMD64
<<: *flatpak_runtimes_publish
tags:
- x86_64
variables:
ARCH: x86_64
# Skip all jobs except the ones that begin with 'docs/'.
# Used for commits including ONLY documentation changes.
# https://docs.gitlab.com/ce/development/writing_documentation.html#testing
.except-docs: &except-docs
except:
- /(^docs[\/-].*|.*-docs$)/
.except-qa: &except-qa
except:
- /(^qa[\/-].*|.*-qa$)/
.except-docs-and-qa: &except-docs-and-qa
except:
- /(^docs[\/-].*|.*-docs$)/
- /(^qa[\/-].*|.*-qa$)
#linux_build_x86_64:
# <<: *build_definition
# variables:
# <<: *build_release_variables
# only:
# variables:
# - $LINUX_BUILDS
.only_stable: &only_stable
only:
- master
- /^release-.*$/
.tumbleweed: &tumbleweed
only:
- develop
- /^feature-.*$/
- /^hotfix-.*$/
- /^bugfix-.*$/
- /^support-.*$/
- /^poc-.*$/
validate:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: validate
tags:
- dds-ci
before_script:
- echo "execute this instead of global before script"
- echo "Validate the project is correct and all necessary information is available."
script:
- >
if [ ! -d "${BUILD_DIRECTORY}" ]; then
cmake -E make_directory ${BUILD_DIRECTORY}
fi
after_script:
- echo "execute this after my script"
artifacts:
name: "${CI_BUILD_NAME}"
expire_in: 2 hour
paths:
- $BUILD_DIRECTORY
initialize:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: initialize
dependencies:
- validate
tags:
- dds-ci
before_script:
- echo "Initialize build state, e.g. set properties or create directories."
script:
- uname -a
- cmake -E chdir $BUILD_DIRECTORY cmake -G "Unix Makefiles"
-DCMAKE_BUILD_TYPE=$CMAKE_BUILD_TYPE
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-DCMAKE_INSTALL_PREFIX=/home/happyman/dev_install/ ..
after_script:
- echo "Project initialization done"
artifacts:
name: "${CI_BUILD_NAME}"
expire_in: 2 hour
paths:
- $BUILD_DIRECTORY
generate-sources:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: generate-sources
tags:
- dds-ci
before_script:
- echo "Generate any source code for inclusion in compilation."
script:
- uname -a
after_script:
- echo "Project generation done"
artifacts:
paths:
- $BUILD_DIRECTORY
process-sources:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: process-sources
tags:
- dds-ci
before_script:
- echo "Process the source code, for example to filter any values."
script:
- uname -a
after_script:
- echo "Sources processed done"
artifacts:
paths:
- $BUILD_DIRECTORY
prettify-sources:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: process-sources
tags:
- dds-ci
before_script:
- echo "Prettying source code with uncrustify"
script:
- uname -a
after_script:
- echo "Sources processed done"
artifacts:
paths:
- $BUILD_DIRECTORY
generate-resources:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: generate-resources
tags:
- dds-ci
before_script:
- echo "Generate resources for inclusion in the package."
script:
- uname -a
after_script:
- echo "Resources generation done"
artifacts:
paths:
- $BUILD_DIRECTORY
process-resources:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: process-resources
tags:
- dds-ci
before_script:
- echo "Copy and process the resources into the destination directory, ready for packaging."
script:
- uname -a
after_script:
- echo "Resources process done"
artifacts:
paths:
- $BUILD_DIRECTORY
compile:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: compile
tags:
- dds-ci
before_script:
- echo "Compile the source code of the project."
script:
# - cmake --build $BUILD_DIRECTORY --target all --clean-first -- -j$NUM_CORES
- make -C $BUILD_DIRECTORY -j$NUM_CORES
dependencies:
- prettify-sources
after_script:
- echo "Compile the source code done."
artifacts:
paths:
- $BUILD_DIRECTORY
process-classes:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: process-classes
tags:
- dds-ci
before_script:
- echo "Post-process the generated files from compilation."
script:
- uname -a
after_script:
- echo "Post-process done."
artifacts:
paths:
- $BUILD_DIRECTORY
generate-test-sources:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: generate-test-sources
tags:
- dds-ci
before_script:
- echo "Generate any test source code for inclusion in compilation."
script:
- uname -a
after_script:
- echo "Test source code for inclusion in compilation done."
artifacts:
paths:
- $BUILD_DIRECTORY
process-test-sources:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: process-test-sources
tags:
- dds-ci
before_script:
- echo "Process the test source code, for example to filter any values."
script:
- uname -a
after_script:
- echo "Test source code process done."
artifacts:
paths:
- $BUILD_DIRECTORY
generate-test-resources:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: generate-test-resources
tags:
- dds-ci
before_script:
- echo "Create resources for testing."
script:
- uname -a
after_script:
- echo "Resources for testing done."
artifacts:
paths:
- $BUILD_DIRECTORY
process-test-resources:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: generate-test-resources
tags:
- dds-ci
before_script:
- echo "Copy and process the resources into the test destination directory."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
test-compile:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: test-compile
tags:
- dds-ci
before_script:
- echo "Compile the test source code into the test destination directory."
script:
- uname -a
dependencies:
- compile
after_script:
- echo "Test copilation done."
artifacts:
paths:
- $BUILD_DIRECTORY
process-test-classes:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: process-test-classes
tags:
- dds-ci
before_script:
- echo "Post-process the generated files from test compilation."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
test:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
TEST_ARGS: "--iterration 1 --outputpath $BUILD_DIRECTORY --module dummyNAme"
stage: test
tags:
- dds-ci
before_script:
- echo "Run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed."
script:
- make -C $BUILD_DIRECTORY test ARGS="--extra-verbose"
# - cmake --build $BUILD_DIRECTORY --target test
# - ctest $BUILD_DIRECTORY -V --build-and-test
# - cmake --build $BUILD_DIRECTORY --target test -- ARGS="$TEST_ARGS"
# - cmake -E env CTEST_OUTPUT_ON_FAILURE=TRUE cmake --build $BUILD_DIRECTORY --target test
dependencies:
- test-compile
after_script:
- echo "Unit testing done."
artifacts:
paths:
- $BUILD_DIRECTORY
prepare-package:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: prepare-package
tags:
- dds-ci
before_script:
- echo "Perform any operations necessary to prepare a package before the actual packaging."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
package:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: package
tags:
- dds-ci
before_script:
- echo "Take the compiled code and package it in its distributable format, such as a ZIP."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
name: "$CI_JOB_STAGE-$CI_COMMIT_REF_NAME"
paths:
- $BUILD_DIRECTORY
pre-integration-test:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: pre-integration-test
tags:
- dds-ci
before_script:
- echo "Perform actions required before integration tests are executed. This may involve things such as setting up the required environment."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
integration-test:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: integration-test
tags:
- dds-ci
before_script:
- echo "Process and deploy the package if necessary into an environment where integration tests can be run."
script:
- uname -a
- cmake --build $BUILD_DIRECTORY --target integration-test
dependencies:
- test-compile
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
post-integration-test:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: post-integration-test
tags:
- dds-ci
before_script:
- echo "Perform actions required after integration tests have been executed. This may including cleaning up the environment."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
verify:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: verify
tags:
- dds-ci
before_script:
- echo "Run any checks to verify the package is valid and meets quality criteria."
script:
- uname -a
- make --version
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
static-code-analysis:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: verify
tags:
- dds-ci
before_script:
- echo "Run Static code analysis."
script:
- uname -a
- make --version
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
code-linter:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: verify
tags:
- dds-ci
before_script:
- echo "Check the C++ source code to analyze it for syntax errors and other faults."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
code-complexity:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: verify
tags:
- dds-ci
before_script:
- echo "Complexity Analyzer."
script:
- uname -a
- make --version
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
code-scan:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
SONAR_PROJECT_KEY: "<YOUR PROJECT KEY>"
SONAR_PROJECT_NAME: "$CI_PROJECT_NAME"
SONAR_PROJECT_VERSION: "$CI_BUILD_ID"
stage: verify
tags:
- dds-ci
before_script:
- echo "Sonnar code source continuous Inspection."
script:
- echo "Sonar scanner should be installed"
# - sonar-scanner
# -Dsonar.source=.
# -Dsonar.host.url=$SONAR_URL
# -Dsonar.login=$SONAR_LOGIN
# -Dsonar.password=$PASSWD
# -Dsonar.projectKey="KEY"
# -Dsonar.projectVersion=$SONAR_PROJECT_VERSION
# -Dsonar.gitlab.projectName=$CI_PROJECT_ID
# -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA
# -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
# -Dsonar.branch=$SONAR_BRANCH
# -Dsonar.analysis.mode=$SONAR_ANALYSIS_MODE
# -Dsonar.issuesReport.console.enable=true"
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
only:
- /^features\/.*/
except:
- tags
- schedules
code-scan-publish:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: verify
tags:
- dds-ci
before_script:
- echo "Publish code Inspection."
script:
- echo "Sonar scanner should be installed"
# - sonar-scanner
# -Dsonar.source=.
# -Dsonar.host.url=$SONAR_URL
# -Dsonar.login=$SONAR_LOGIN
# -Dsonar.gitlab.project=$CI_PROJECT_ID
# -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA
# -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
# -Dsonar.analysis.mode=publish
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
only:
- master
except:
- tags
- schedules
code-performance:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: verify
allow_failure: true
tags:
- dds-ci
before_script:
- echo "Run any checks to verify the package is valid and meets quality criteria."
script:
- uname -a
- make --version
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
only:
refs:
- branches
except:
variables:
- $PERFORMANCE_DISABLED
verify:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: verify
tags:
- dds-ci
before_script:
- echo "Run any checks to verify the package is valid and meets quality criteria."
script:
- uname -a
- make --version
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
install:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: install
tags:
- dds-ci
before_script:
- echo "Install the package into the local repository, for use as a dependency in other projects locally."
script:
- uname -a
- make --version
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
deploy-statging:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: deploy
when: manual
tags:
- dds-ci
before_script:
- echo "Integration or release environment, copies the final package to the remote repository for sharing with other developers and projects."
script:
- echo "Deploy to staging server"
- uname -a
after_script:
- echo "Done."
environment:
name: staging
url: https://staging.acme
only:
- master
# - tags
artifacts:
paths:
- $BUILD_DIRECTORY
deploy-review:
stage: deploy
tags:
- dds-ci
allow_failure: false
script:
- echo "Deploy a review app"
environment:
name: review/$CI_COMMIT_REF_NAME
url: https://$CI_ENVIRONMENT_SLUG.review.acme
only:
- branches
except:
- master
- /^release-.*/
- /^production-.*/
artifacts:
paths:
- $BUILD_DIRECTORY
code-review:
stage: verify
environment:
name: review-$CI_COMMIT_REF_SLUG
on_stop: stop-review
script:
- echo "Sent for review"
#- curl -u "$GF_API_KEY":"$GF_API_SECRET" "${GF_DEPLOY_URL}?sha=${CI_COMMIT_SHA}&ref=${CI_COMMIT_REF_SLUG}&org=${GF_TARGET_ORG_NAME}&env=${GF_TARGET_ENV_UUID}&api=${GF_TARGET_API_UUID}&dockerImg=${CI_REGISTRY_IMAGE}&name=${GITLAB_PROJECT_NAME}&projId=${GITLAB_PROJECT_ID}&port=${APP_SERVICE_PORT}"
except:
- /^[Ww][Ii][Pp]-.*$/
stop-review:
stage: verify
variables:
GIT_STRATEGY: none
environment:
name: review-$CI_COMMIT_REF_SLUG
action: stop
script:
- echo "Review stopped"
# - curl -u "$GF_API_KEY":"$GF_API_SECRET" "${GF_DEPLOY_URL}?entryPoint=stop&ref=${CI_COMMIT_REF_SLUG}&org=${GF_TARGET_ORG_NAME}&env=${GF_TARGET_ENV_UUID}&name=${GITLAB_PROJECT_NAME}&projId=${GITLAB_PROJECT_ID}"
# when: manual
except:
- master
- tags
only:
- branches
deploy-prod:
stage: deploy
tags:
- dds-ci
script:
- echo "Deploy to production server"
environment:
name: production
url: https://releases.acme
when: manual
only:
- master
artifacts:
paths:
- $BUILD_DIRECTORY
pre-site:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: pre-site
tags:
- dds-ci
before_script:
- echo "Execute processes needed prior to the actual project site generation."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
site:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: site
tags:
- dds-ci
before_script:
- echo "Generate the project's site documentation."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
post-site:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: post-site
tags:
- dds-ci
before_script:
- echo "Execute processes needed to finalize the site generation, and to prepare for site deployment."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
site-deploy:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: site-deploy
tags:
- dds-ci
before_script:
- echo "Deploy the generated site documentation to the specified web server."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
pre-clean:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: pre-clean
tags:
- dds-ci
before_script:
- echo "Execute processes needed prior to the actual project cleaning."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
clean:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: clean
tags:
- dds-ci
before_script:
- echo "Remove all files generated by the previous build."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
post-clean:
variables:
JOB_DATE: "date +%Y-%m-%d-%H:%M:%S"
stage: post-clean
tags:
- dds-ci
before_script:
- echo "Execute processes needed to finalize the project cleaning."
script:
- uname -a
after_script:
- echo "Done."
artifacts:
paths:
- $BUILD_DIRECTORY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment