.gitlab-ci.yml 10.5 KB
Newer Older
1
2
3
4
default:
  image: "archlinux:latest"

stages:
5
  - cleanup
6
7
8
9
10
11
12
  - lint
  - rootfs
  - image
  - test
  - release
  - publish

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
cleanup:
  stage: cleanup
  tags:
    - secure
  only:
    refs:
      - schedules
    variables:
      - $CLEANUP_PACKAGE_REGISTRY == "TRUE"
  before_script:
    - pacman -Syu --noconfirm jq
  script:
    - |
      for id in $(curl --silent --fail --show-error "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages?per_page=100&order_by=created_at&sort=asc" | jq '.[] | select(.created_at | split("T")[0] | . < (now-60*60*24*60|strflocaltime("%Y-%m-%d"))) | .id'); do
        curl --silent --fail --show-error --request DELETE --header "PRIVATE-TOKEN: ${GITLAB_PROJECT_TOKEN}" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/${id}"
      done

30
31
lint:
  stage: lint
32
  image: hadolint/hadolint:latest-alpine
33
34
  # DL3018: We don't need alpine version pins
  script: hadolint --ignore DL3018 Dockerfile.template
35
36
  except:
    - releases
37
    - tags
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

get_version:
  stage: .pre
  script:
    - |
      # If we're building a tagged release, use the tag (without the 'v' prefix) as the
      # BUILD_VERSION. Otherwise, determine a new BUILD_VERSION.
      if [[ -n "$CI_COMMIT_TAG" ]]; then
        echo "BUILD_VERSION=${CI_COMMIT_TAG/v/}" > build.env
      else
        echo "BUILD_VERSION=$(date +%Y%m%d).0.$CI_JOB_ID" > build.env
      fi
    - export $(< build.env)
    - echo "PACKAGE_REGISTRY_URL=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/rootfs/${BUILD_VERSION}" >> build.env
  artifacts:
    reports:
      dotenv: build.env

.rootfs:
  stage: rootfs
  before_script:
    - pacman -Syu --noconfirm make devtools fakechroot fakeroot
60
61
  script:
    - make $PWD/output/Dockerfile.$GROUP
62
63
64
65
66
67
68
69
70
  artifacts:
    paths:
      - output/*
    expire_in: 2h

rootfs:
  extends: .rootfs
  except:
    - master
71
    - releases
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    - schedules
    - tags
  parallel:
    matrix:
      - GROUP: [base, base-devel]

rootfs:secure:
  extends: .rootfs
  tags:
    - secure
  only:
    - master
    - schedules
  except:
    - tags
87
    - releases
88
89
90
91
92
93
94
95
96
97
98
  parallel:
    matrix:
      - GROUP: [base, base-devel]

.image:
  stage: image
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    - /kaniko/executor
99
      --force
100
101
102
103
104
105
106
107
108
      --whitelist-var-run="false"
      --context $CI_PROJECT_DIR/output
      --dockerfile $CI_PROJECT_DIR/output/Dockerfile.$GROUP
      --destination $CI_REGISTRY_IMAGE:$GROUP-$CI_COMMIT_REF_SLUG

image:build:
  extends: .image
  except:
    - master
109
    - releases
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    - schedules
    - tags
  parallel:
    matrix:
      - GROUP: [base, base-devel]
  before_script:
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json

image:build:secure:
  extends: .image
  tags:
    - secure
  only:
    - master
    - schedules
  except:
    - tags
  parallel:
    matrix:
      - GROUP: [base, base-devel]
  before_script:
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$GITLAB_PROJECT_USER\",\"password\":\"$GITLAB_PROJECT_TOKEN\"}}}" > /kaniko/.docker/config.json

133
# Build and publish to the Arch Linux group namespace: https://hub.docker.com/r/archlinux/archlinux
134
135
image:publish:secure:
  extends: .image
136
  retry: 2
137
138
139
140
141
142
143
144
  tags:
    - secure
  only:
    - tags
  parallel:
    matrix:
      - GROUP: [base, base-devel]
  before_script:
145
    - echo "{\"auths\":{\"https://index.docker.io/v1/\":{\"username\":\"$DOCKERHUB_USERNAME\",\"password\":\"$DOCKERHUB_ACCESS_TOKEN\"}}}" > /kaniko/.docker/config.json
146
  script:
147
148
    - LATEST=""
    - if [[ "$GROUP" == "base" ]]; then
149
        LATEST="--destination archlinux/archlinux:latest";
150
      fi
151
    - /kaniko/executor
152
      --force
153
154
155
      --whitelist-var-run="false"
      --context $CI_PROJECT_DIR
      --dockerfile $CI_PROJECT_DIR/Dockerfile.$GROUP
156
      --destination archlinux/archlinux:$GROUP
157
      --destination archlinux/archlinux:$GROUP-$BUILD_VERSION
158
      $LATEST
159
160
161
162
163
164
165
166
167
168
169

.test:
  stage: test
  dependencies: []
  only:
    variables:
      # Workaround for https://gitlab.com/gitlab-org/gitlab/-/issues/259663
      # This is fine as at this point we're sure that the release works anyway.
      - $GITLAB_USER_EMAIL != "project10185_bot2@example.com"
  except:
    refs:
170
      - releases
171
      - tags
172
173

.test-script: &test-script
174
175
  - test "$(cat /etc/group | wc -l)" -gt 10
  - test "$(cat /etc/passwd | wc -l)" -gt 10
176
177
178
179
180
181
  - pacman -Sy
  - pacman -Qqk
  - pacman -Syu --noconfirm docker grep
  - docker -v
  - id -u http
  - locale | grep -q UTF-8
182
183
184
185

test:base:
  extends: .test
  image: $CI_REGISTRY_IMAGE:base-$CI_COMMIT_REF_SLUG
186
187
  script:
    - *test-script
188
189
190
191

test:base-devel:
  extends: .test
  image: $CI_REGISTRY_IMAGE:base-devel-$CI_COMMIT_REF_SLUG
192
193
  script:
    - *test-script
194
195
196
197
198
199
200
201
202
203
204
205
206
    - gcc -v
    - g++ -v
    - make -v

release:
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  tags:
    - secure
  only:
    refs:
      - schedules
    variables:
207
      - $PUBLISH_ARCHLINUX_REPOSITORY == "TRUE"
208
      - $PUBLISH_OFFICIAL_LIBRARY == "TRUE"
209
  before_script:
210
    - apk update
211
    - apk add jq curl httpie
212
213
  script:
    - |
214
215
216
217
218
      # Update the description on https://hub.docker.com/r/archlinux/archlinux
      TOKEN="$(http --ignore-stdin POST https://hub.docker.com/v2/users/login username="${DOCKERHUB_USERNAME}" password="${DOCKERHUB_PASSWORD}" | jq -er .token)"
      http --ignore-stdin PATCH https://hub.docker.com/v2/repositories/archlinux/archlinux/ Authorization:"JWT ${TOKEN}" full_description="$(cat README.md)"

      # Upload rootfs to the Generic Packages Repository
219
220
221
222
223
224
225
226
      for group in base base-devel; do
        sed -i "s|${group}.tar.xz|${group}-${BUILD_VERSION}.tar.xz|" output/${group}.tar.xz.SHA256
        echo "Uploading ${group}.tar.xz"
        curl -sSf --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file output/${group}.tar.xz ${PACKAGE_REGISTRY_URL}/${group}-${BUILD_VERSION}.tar.xz
        echo "Uploading ${group}.tar.xz.SHA256"
        curl -sSf --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file output/${group}.tar.xz.SHA256 ${PACKAGE_REGISTRY_URL}/${group}-${BUILD_VERSION}.tar.xz.SHA256
        sed "/TEMPLATE_ROOTFS_FILE/d" Dockerfile.template > output/Dockerfile.${group}
        package_url=$(./ci/get-public-download-for-generic-package.sh ${group}-${BUILD_VERSION}.tar.xz)
227
        sed -i "s|TEMPLATE_ROOTFS_RELEASE_URL|https://gitlab.archlinux.org/archlinux/archlinux-docker/-/releases/v${BUILD_VERSION}|" output/Dockerfile.${group}
228
        sed -i "s|TEMPLATE_ROOTFS_DOWNLOAD|ROOTFS=\"\$(curl -sOJL -w \"%{filename_effective}\" \"${package_url}\")\"|" output/Dockerfile.${group}
229
230
231
        sed -i "s|TEMPLATE_ROOTFS_HASH|$(cat output/${group}.tar.xz.SHA256)|" output/Dockerfile.${group}
      done
    - >
232
      curl -sSf --request POST -o commit-response.json
233
234
235
236
237
238
239
240
241
242
243
244
245
      --header "PRIVATE-TOKEN: ${GITLAB_PROJECT_TOKEN}"
      --form "branch=releases"
      --form "commit_message=Release ${BUILD_VERSION}"
      --form "actions[][action]=update"
      --form "actions[][file_path]=Dockerfile.base"
      --form "actions[][content]=<output/Dockerfile.base"
      --form "actions[][action]=update"
      --form "actions[][file_path]=Dockerfile.base-devel"
      --form "actions[][content]=<output/Dockerfile.base-devel"
      --form "actions[][action]=update"
      --form "actions[][file_path]=.gitlab-ci.yml"
      --form "actions[][content]=<.gitlab-ci.yml"
      "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/repository/commits"
246
    - echo "BUILD_COMMIT=$(jq -r '.id' commit-response.json)" >> build.env
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    - |
      base_url=$(./ci/get-public-download-for-generic-package.sh base-${BUILD_VERSION}.tar.xz)
      echo "${base_url}"
      base_sha_url=$(./ci/get-public-download-for-generic-package.sh base-${BUILD_VERSION}.tar.xz.SHA256)
      echo "${base_sha_url}"
      base_devel_url=$(./ci/get-public-download-for-generic-package.sh base-devel-${BUILD_VERSION}.tar.xz)
      echo "${base_devel_url}"
      base_devel_sha_url=$(./ci/get-public-download-for-generic-package.sh base-devel-${BUILD_VERSION}.tar.xz.SHA256)
      echo "${base_devel_sha_url}"

      # TODO: We should actually be able to do something like \"url\":\"${PACKAGE_REGISTRY_URL}/base-${BUILD_VERSION}.tar.xz\"
      # But it doesn't appear that those downloads are public. I consider this a bug and hopefully it's fixed in a future version!
      echo "Creating release"
      release-cli create --name "Release ${BUILD_VERSION}" --description "Release ${BUILD_VERSION}" \
261
      --private-token "${GITLAB_PROJECT_TOKEN}" \
262
263
264
265
266
      --tag-name v${BUILD_VERSION} --ref "releases" \
      --assets-link "{\"name\":\"base-${BUILD_VERSION}.tar.xz\",\"url\":\"${base_url}\"}" \
      --assets-link "{\"name\":\"base-${BUILD_VERSION}.tar.xz.SHA256\",\"url\":\"${base_sha_url}\"}" \
      --assets-link "{\"name\":\"base-devel-${BUILD_VERSION}.tar.xz\",\"url\":\"${base_devel_url}\"}" \
      --assets-link "{\"name\":\"base-devel-${BUILD_VERSION}.tar.xz.SHA256\",\"url\":\"${base_devel_sha_url}\"}"
267
268
269
  artifacts:
    reports:
      dotenv: build.env
270
271

# Publish to the official Docker namespace: https://hub.docker.com/_/archlinux
272
publish:
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
  stage: publish
  only:
    refs:
      - schedules
    variables:
      - $PUBLISH_OFFICIAL_LIBRARY == "TRUE"
  before_script:
    - export | grep -q BUILD_VERSION=
    - export | grep -q BUILD_COMMIT=
    - test -n "$BUILD_VERSION"
    - test -n "$BUILD_COMMIT"
    - test -n "$GITHUB_TOKEN"
    - pacman -Syu --noconfirm github-cli git gettext
    - git config --global user.email "github@archlinux.org"
    - git config --global user.name "Arch Linux Technical User"
  script:
    - mkdir official-images
    - cd official-images
    - git init
    - 'git remote add origin "https://x-access-token:${GITHUB_TOKEN}@github.com/archlinux/official-images.git"'
293
    - git fetch https://github.com/docker-library/official-images.git
294
295
296
297
298
299
300
301
302
303
304
305
306
    - git reset --hard FETCH_HEAD
    - head="release/${BUILD_VERSION}"
    - git checkout -b "$head"
    - envsubst < ../docker-library.template > library/archlinux
    - git diff
    - git add library/archlinux
    - maintainers="$(grep \(@ ../docker-library.template | cut -d\( -f2 | cut -d\) -f1 | xargs)"
    - test -n "$maintainers"
    - 'git commit
        -m "archlinux: Release ${BUILD_VERSION}"
        -m "This is an automated release [1]."
        -m "[1] ${CI_PROJECT_URL}/-/blob/master/.gitlab-ci.yml"'
    - git push -u origin "$head"
307
    - 'gh pr create
308
        --repo docker-library/official-images
309
310
        --title "$(git show --no-patch --format="%s")"
        --body "$(printf "%s\n\n---\n\nMaintainers: ${maintainers}\n" "$(git show --no-patch --format="%b")")"
311
        --base master
312
        --head archlinux:"$head"'