CI: Use kaniko to build images

This commit is contained in:
Igor Scheller 2021-07-10 16:18:10 +02:00
parent dd63cd70c7
commit d289d09147
2 changed files with 58 additions and 38 deletions

View File

@ -1,7 +1,6 @@
image: php image: php
variables: variables:
DOCKER_DRIVER: overlay2
TEST_IMAGE: ${CI_REGISTRY_IMAGE}/engelsystem:${CI_COMMIT_REF_SLUG} TEST_IMAGE: ${CI_REGISTRY_IMAGE}/engelsystem:${CI_COMMIT_REF_SLUG}
TEST_IMAGE_NGINX: ${CI_REGISTRY_IMAGE}/nginx:${CI_COMMIT_REF_SLUG} TEST_IMAGE_NGINX: ${CI_REGISTRY_IMAGE}/nginx:${CI_COMMIT_REF_SLUG}
RELEASE_IMAGE: ${CI_REGISTRY_IMAGE}/engelsystem:latest RELEASE_IMAGE: ${CI_REGISTRY_IMAGE}/engelsystem:latest
@ -57,49 +56,59 @@ validate-yarn:
script: script:
- pjv - pjv
generate-version:
image: alpine
stage: validate
artifacts:
name: "${CI_JOB_NAME}_${CI_JOB_ID}_version"
expire_in: 1 day
paths:
- ./storage/app/VERSION
before_script:
- apk add -q git
script:
- VERSION="$(git describe --abbrev=0 --tags)-${CI_COMMIT_REF_NAME}+${CI_PIPELINE_ID}.${CI_COMMIT_SHORT_SHA}"
- echo "${VERSION}"
- echo -n "${VERSION}" > storage/app/VERSION
# #
# Build # Build
# #
.docker_template: &docker_definition .container_template: &container_definition
image: docker image:
services: name: gcr.io/kaniko-project/executor:debug
- docker:dind entrypoint: [ "" ]
tags:
- dind
before_script: before_script:
- docker login -u gitlab-ci-token -p "${CI_JOB_TOKEN}" "${CI_REGISTRY}" - mkdir -p /kaniko/.docker
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}"
> /kaniko/.docker/config.json
build-image-nginx: build-image-nginx:
<<: *docker_definition <<: *container_definition
stage: build stage: build
needs: needs:
- check-editorconfig - check-editorconfig
- validate-yarn - validate-yarn
artifacts:
name: "${CI_JOB_NAME}_${CI_JOB_ID}_assets"
expire_in: 1 day
paths:
- ./public/assets
script: script:
- docker build --pull -t "${TEST_IMAGE_NGINX}" -f docker/nginx/Dockerfile . - /kaniko/executor --context ${CI_PROJECT_DIR}
- docker push "${TEST_IMAGE_NGINX}" --dockerfile ${CI_PROJECT_DIR}/docker/nginx/Dockerfile
- instance=$(docker create "${TEST_IMAGE_NGINX}") --destination "${TEST_IMAGE_NGINX}"
- docker cp "${instance}:/var/www/public/assets" public/
- docker rm "${instance}"
build-image: build-image:
<<: *docker_definition <<: *container_definition
stage: build stage: build
needs: needs:
- check-editorconfig - check-editorconfig
- validate-composer
- check-style - check-style
- generate-version
- validate-composer
dependencies:
- generate-version
script: script:
- apk add -q git - /kaniko/executor --context ${CI_PROJECT_DIR}
- VERSION="$(git describe --abbrev=0 --tags)-${CI_COMMIT_REF_NAME}+${CI_PIPELINE_ID}.${CI_COMMIT_SHORT_SHA}" --dockerfile ${CI_PROJECT_DIR}/docker/Dockerfile
- docker build --pull --build-arg VERSION="${VERSION}" -t "${TEST_IMAGE}" -f docker/Dockerfile . --destination "${TEST_IMAGE}"
- docker push "${TEST_IMAGE}"
# #
# Test # Test
@ -174,33 +183,43 @@ dump-database:
mysqldump -h "${MYSQL_HOST}" -u "${MYSQL_USER}" -p"${MYSQL_PASSWORD}" "${MYSQL_DATABASE}" mysqldump -h "${MYSQL_HOST}" -u "${MYSQL_USER}" -p"${MYSQL_PASSWORD}" "${MYSQL_DATABASE}"
> "${HOMEDIR}/initial-install.sql" > "${HOMEDIR}/initial-install.sql"
generate-assets:
image:
name: $TEST_IMAGE_NGINX
entrypoint: [ "" ]
stage: test
needs: [ build-image-nginx ]
artifacts:
name: "${CI_JOB_NAME}_${CI_JOB_ID}_assets"
expire_in: 1 day
paths:
- ./public/assets
script:
- mv /var/www/public/assets/ public/
# #
# Release # Release
# #
release-image: release-image:
<<: *docker_definition <<: *container_definition
stage: release stage: release
needs: [ test ] needs: [ test ]
dependencies: [ ] dependencies: [ ]
script: script:
- docker pull "${TEST_IMAGE}" - echo -e "FROM ${TEST_IMAGE}" | executor --dockerfile /dev/stdin --destination "${RELEASE_IMAGE}"
- docker tag "${TEST_IMAGE}" "${RELEASE_IMAGE}"
- docker push "${RELEASE_IMAGE}"
only: only:
- main - main
release-image-nginx: release-image-nginx:
<<: *docker_definition <<: *container_definition
stage: release stage: release
needs: needs:
- test - test
- build-image-nginx - build-image-nginx
dependencies: [ ] dependencies: [ ]
script: script:
- docker pull "${TEST_IMAGE_NGINX}" - echo -e "FROM ${TEST_IMAGE_NGINX}" | executor --dockerfile /dev/stdin --destination "${RELEASE_IMAGE_NGINX}"
- docker tag "${TEST_IMAGE_NGINX}" "${RELEASE_IMAGE_NGINX}"
- docker push "${RELEASE_IMAGE_NGINX}"
only: only:
- main - main
@ -220,10 +239,11 @@ build-release-file:
- audit-composer - audit-composer
- test - test
- dump-database - dump-database
- generate-assets
dependencies: dependencies:
- build-image - build-image
- build-image-nginx
- dump-database - dump-database
- generate-assets
artifacts: artifacts:
name: release_${CI_COMMIT_REF_SLUG}_${CI_JOB_ID}_${CI_COMMIT_SHA} name: release_${CI_COMMIT_REF_SLUG}_${CI_JOB_ID}_${CI_COMMIT_SHA}
expire_in: 1 week expire_in: 1 week
@ -267,7 +287,7 @@ deploy:
stage: deploy stage: deploy
needs: &deploy_needs needs: &deploy_needs
- release-image - release-image
- build-image-nginx - generate-assets
dependencies: *deploy_needs dependencies: *deploy_needs
environment: environment:
name: staging name: staging
@ -376,10 +396,10 @@ deploy-production:
- audit-yarn - audit-yarn
- audit-composer - audit-composer
- build-image - build-image
- build-image-nginx - generate-assets
dependencies: dependencies:
- build-image - build-image
- build-image-nginx - generate-assets
environment: environment:
name: production name: production
when: manual when: manual

View File

@ -1,5 +1,5 @@
# composer install # composer install
FROM composer AS composer FROM composer:latest AS composer
COPY ./ /app/ COPY ./ /app/
RUN composer --no-ansi install --no-dev --ignore-platform-reqs RUN composer --no-ansi install --no-dev --ignore-platform-reqs
RUN composer --no-ansi dump-autoload --optimize RUN composer --no-ansi dump-autoload --optimize