aboutsummaryrefslogtreecommitdiffstats
path: root/gitlab-ci-template-multiarch.yml
blob: 1d082af5921e270b3615622daa35e097102d7196 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# https://blog.callr.tech/building-docker-images-with-gitlab-ci-best-practices/
# comments dropped, updated for podman builds

#
# Add this variable to your .gitlab-ci.yml to enable aarch64 builds.
# Also needs an aarch64 builder (tagged 'aarch64') to work.
#
#variables:
#  BUILD_AARCH64: "yes"

image: registry.gitlab.com/kraxel/podman-docker-builder:latest

stages:
  - build
  - manifest

.tag-name: &tag-name
  - >
    if test "$CI_COMMIT_BRANCH" != "";
    then
    TAGNAME=${CI_COMMIT_BRANCH/master/latest};
    else
    TAGNAME=${CI_COMMIT_SHA};
    fi
  - echo "tagname is -> ${TAGNAME} <-"

.build-default: &build-default
  - podman login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
  - podman pull $CI_REGISTRY_IMAGE:${TAGNAME}-${ARCH} || true
  - >
    podman build
    --pull
    --build-arg VCS_REF=$CI_COMMIT_SHA
    --build-arg VCS_URL=$CI_PROJECT_URL
    --cache-from $CI_REGISTRY_IMAGE:${TAGNAME}-${ARCH}
    --tag $CI_REGISTRY_IMAGE:${TAGNAME}-${ARCH}
    .
  - podman push $CI_REGISTRY_IMAGE:${TAGNAME}-${ARCH}

.build-nocache: &build-nocache
  - podman login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
  - >
    podman build
    --build-arg VCS_REF=$CI_COMMIT_SHA
    --build-arg VCS_URL=$CI_PROJECT_URL
    --tag $CI_REGISTRY_IMAGE:${TAGNAME}-${ARCH}
    .
  - podman push $CI_REGISTRY_IMAGE:${TAGNAME}-${ARCH}

build-x86_64:
  stage: build
  variables:
    ARCH: x86_64
  script:
    - *tag-name
    - *build-nocache

build-aarch64:
  stage: build
  tags:
    - aarch64
  variables:
    ARCH: aarch64
  rules:
    - if: '$BUILD_AARCH64 == "yes"'
  script:
    - *tag-name
    - *build-nocache

manifest:
  stage: manifest
  script:
    - *tag-name
    - TAGLIST=$(skopeo inspect docker://$CI_REGISTRY_IMAGE:${TAGNAME}-x86_64 | jq '.RepoTags[]' | tr -d '"' | grep ${TAGNAME}-)
    - buildah login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - buildah manifest create localhost/multiarch:${TAGNAME}
    - >
      for item in $TAGLIST;
      do echo "# -> $item <-";
      buildah manifest add localhost/multiarch:${TAGNAME} docker://$CI_REGISTRY_IMAGE:${item};
      done
    - buildah manifest inspect localhost/multiarch:${TAGNAME}
    - buildah manifest push -f v2s2 localhost/multiarch:${TAGNAME} docker://$CI_REGISTRY_IMAGE:${TAGNAME}