Skip to content

Commit 19e49a6

Browse files
authored
refactor: simplify docker build (#27174)
1 parent c915159 commit 19e49a6

18 files changed

+352
-192
lines changed

.dockerignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*
2+
!tools/docker/bin
3+
!dist/
4+
!node_modules/
5+
!package.json
6+
!pnpm-lock.yaml
7+
!renovate-schema.json
8+
!license

.github/workflows/build.yml

+19-5
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ jobs:
466466
- name: Build
467467
run: pnpm build
468468

469+
- name: Build docker
470+
run: pnpm build:docker build --tries=3
471+
env:
472+
LOG_LEVEL: debug
473+
469474
- name: Pack
470475
run: pnpm test-e2e:pack
471476

@@ -550,6 +555,7 @@ jobs:
550555
issues: write
551556
pull-requests: write
552557
id-token: write
558+
packages: write
553559

554560
steps:
555561
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
@@ -558,12 +564,24 @@ jobs:
558564
show-progress: false
559565
filter: blob:none # we don't need all blobs, only the full tree
560566

567+
- name: docker-config
568+
uses: containerbase/internal-tools@e7bd2e8cedd99c9b24982865534cb7c9bf88620b # v3.0.55
569+
with:
570+
command: docker-config
571+
561572
- name: Setup Node.js
562573
uses: ./.github/actions/setup-node
563574
with:
564575
node-version: ${{ env.NODE_VERSION }}
565576
os: ${{ runner.os }}
566577

578+
- uses: sigstore/cosign-installer@e1523de7571e31dbe865fd2e80c5c7c23ae71eb4 # v3.4.0
579+
580+
- name: Docker registry login
581+
run: |
582+
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
583+
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin
584+
567585
- name: Check dry run
568586
run: |
569587
if [[ "${{github.event_name}}" == "workflow_dispatch" && "${{ github.event.inputs.dryRun }}" != "true" ]]; then
@@ -574,13 +592,9 @@ jobs:
574592
echo "DRY_RUN=false" >> "$GITHUB_ENV"
575593
fi
576594
577-
# TODO: move to semantic-release prepare
578-
- name: Build
579-
run: pnpm build
580-
581595
- name: semantic-release
582596
run: |
583597
pnpm semantic-release --dry-run ${{env.DRY_RUN}}
584598
env:
585-
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
599+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} # TODO: use action token?
586600
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.releaserc.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
[
1919
"@semantic-release/exec",
2020
{
21-
"prepareCmd": "pnpm release:prepare --release=${nextRelease.version} --sha=${nextRelease.gitHead} --tag=${nextRelease.channel}",
22-
"publishCmd": "pnpm release:publish --release=${nextRelease.version} --sha=${nextRelease.gitHead} --tag=${nextRelease.channel}"
21+
"prepareCmd": "pnpm release:prepare --version=${nextRelease.version} --sha=${nextRelease.gitHead} --tries=3 --platform=linux/amd64,linux/arm64 --exit-on-error=false",
22+
"publishCmd": "pnpm release:publish --version=${nextRelease.version} --sha=${nextRelease.gitHead} --platform=linux/amd64,linux/arm64 --exit-on-error=false"
2323
}
2424
]
2525
],

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
},
1010
"scripts": {
1111
"build": "run-s clean 'generate:*' 'compile:*' create-json-schema",
12-
"build:docker": "node tools/docker.mjs",
13-
"build:docs": "run-s 'release:prepare {@}' --",
12+
"build:docker": "ts-node tools/docker.ts",
13+
"build:docs": "ts-node tools/generate-docs.ts",
1414
"clean": "rimraf dist tmp",
1515
"clean-cache": "node tools/clean-cache.mjs",
1616
"compile:ts": "tsc -p tsconfig.app.json",
@@ -45,8 +45,8 @@
4545
"pretest": "run-s 'generate:*'",
4646
"prettier": "prettier --cache --check '**/*.{ts,js,mjs,json,md,yml}'",
4747
"prettier-fix": "prettier --write --cache '**/*.{ts,js,mjs,json,md,yml}'",
48-
"release:prepare": "ts-node tools/generate-docs.ts",
49-
"release:publish": "node tools/release.mjs",
48+
"release:prepare": "ts-node tools/prepare-release.ts",
49+
"release:publish": "ts-node tools/publish-release.ts",
5050
"start": "ts-node lib/renovate.ts",
5151
"test": "run-s lint test-schema jest",
5252
"test-dirty": "git diff --exit-code",

tools/docker.mjs renamed to tools/docker.ts

+11-36
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,23 @@
11
import { Command } from 'commander';
2-
import { bake } from './utils/docker.mjs';
2+
import { logger } from '../lib/logger';
3+
import { parsePositiveInt, parseVersion } from './utils';
4+
import { bake } from './utils/docker';
35

46
const program = new Command('pnpm build:docker');
57

6-
/**
7-
*
8-
* @param {string | undefined} val
9-
*/
10-
function parseInt(val) {
11-
if (!val) {
12-
return 0;
13-
}
14-
const r = Number.parseInt(val, 10);
15-
if (!Number.isFinite(r) || r < 0) {
16-
throw new Error(`Invalid number: ${val}`);
17-
}
18-
19-
return r;
20-
}
21-
22-
/**
23-
*
24-
* @param {string | undefined} val
25-
*/
26-
function parseVersion(val) {
27-
if (!val) {
28-
return val;
29-
}
30-
31-
if (!/^\d+\.\d+\.\d+(?:-.+)?$/.test(val)) {
32-
throw new Error(`Invalid version: ${val}`);
33-
}
34-
35-
return val;
36-
}
37-
388
program
399
.command('build')
4010
.description('Build docker images')
4111
.option('--platform <type>', 'docker platforms to build')
4212
.option('--version <version>', 'version to use as tag', parseVersion)
43-
.option('--tries <tries>', 'number of tries on failure', parseInt)
13+
.option('--tries <tries>', 'number of tries on failure', parsePositiveInt)
14+
.option(
15+
'--delay <delay>',
16+
'delay between tries for docker build (eg. 5s, 10m, 1h)',
17+
'30s',
18+
)
4419
.action(async (opts) => {
45-
console.log('Building docker images ...');
20+
logger.info('Building docker images ...');
4621
await bake('build', opts, opts.tries - 1);
4722
});
4823

@@ -52,7 +27,7 @@ program
5227
.option('--platform <type>', 'docker platforms to build')
5328
.option('--version <version>', 'version to use as tag', parseVersion)
5429
.action(async (opts) => {
55-
console.log('Publishing docker images ...');
30+
logger.info('Publishing docker images ...');
5631
await bake('push', opts);
5732
});
5833

tools/docker/Dockerfile

+40-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
ARG RENOVATE_VERSION
21
ARG BASE_IMAGE_TYPE=slim
32

43
# --------------------------------------
@@ -11,6 +10,37 @@ FROM ghcr.io/renovatebot/base-image:1.22.0@sha256:59606f80b6194a99f9d7d4a2667dcc
1110
# --------------------------------------
1211
FROM ghcr.io/renovatebot/base-image:1.22.0-full@sha256:7a371dcfff219fc638301ce1856d92ee2a09993f628a7b641d8da12c6e23eb0d AS full-base
1312

13+
# --------------------------------------
14+
# build image
15+
# --------------------------------------
16+
FROM slim-base as build
17+
18+
WORKDIR /usr/local/renovate
19+
20+
ENV CI=1 npm_config_modules_cache_max_age=0
21+
22+
COPY pnpm-lock.yaml ./
23+
24+
# only fetch deps from lockfile https://pnpm.io/cli/fetch
25+
RUN pnpm fetch --prod
26+
27+
COPY . ./
28+
29+
# install
30+
ENV RE2_DOWNLOAD_MIRROR=https://github.com/containerbase/node-re2-prebuild/releases/download RE2_DOWNLOAD_SKIP_PATH=1
31+
RUN set -ex; \
32+
pnpm install --prod --offline --ignore-scripts; \
33+
npm explore re2 -- npm run install; \
34+
true
35+
36+
# test
37+
COPY tools/docker/bin/ /usr/local/bin/
38+
RUN set -ex; \
39+
renovate --version; \
40+
renovate-config-validator; \
41+
node -e "new require('re2')('.*').exec('test')"; \
42+
true
43+
1444
# --------------------------------------
1545
# final image
1646
# --------------------------------------
@@ -21,19 +51,24 @@ LABEL org.opencontainers.image.source="https://github.com/renovatebot/renovate"
2151
org.opencontainers.image.url="https://renovatebot.com" \
2252
org.opencontainers.image.licenses="AGPL-3.0-only"
2353

24-
2554
WORKDIR /usr/src/app
2655

2756
ENV RENOVATE_X_IGNORE_NODE_WARN=true
2857

29-
COPY bin/ /usr/local/bin/
58+
COPY tools/docker/bin/ /usr/local/bin/
3059
CMD ["renovate"]
3160

3261
ARG RENOVATE_VERSION
33-
RUN install-tool renovate
62+
63+
COPY --from=build --chown=root:root /usr/local/renovate/ /usr/local/renovate/
3464

3565
# Compabillity, so `config.js` can access renovate and deps
36-
RUN ln -sf /opt/containerbase/tools/renovate/${RENOVATE_VERSION}/node_modules ./node_modules;
66+
RUN set -ex; \
67+
mkdir /opt/containerbase/tools/renovate; \
68+
echo "${RENOVATE_VERSION}" > /opt/containerbase/versions/renovate; \
69+
ln -sf /usr/local/renovate /opt/containerbase/tools/renovate/${RENOVATE_VERSION}; \
70+
ln -sf /usr/local/renovate/node_modules ./node_modules; \
71+
true
3772

3873
RUN set -ex; \
3974
renovate --version; \

tools/docker/bake.hcl

+12-6
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,15 @@ group "push" {
4242
]
4343
}
4444

45+
group "push-cache" {
46+
targets = [
47+
"push-cache-slim",
48+
"push-cache-full",
49+
]
50+
}
51+
4552
target "settings" {
46-
context = "tools/docker"
53+
dockerfile = "tools/docker/Dockerfile"
4754
args = {
4855
APT_HTTP_PROXY = "${APT_HTTP_PROXY}"
4956
CONTAINERBASE_DEBUG = "${CONTAINERBASE_DEBUG}"
@@ -54,7 +61,7 @@ target "settings" {
5461

5562
target "slim" {
5663
cache-from = [
57-
"type=registry,ref=ghcr.io/${OWNER}/docker-build-cache:${FILE}-${RENOVATE_VERSION}",
64+
"type=registry,ref=ghcr.io/${OWNER}/docker-build-cache:${FILE}",
5865
]
5966
tags = [
6067
"ghcr.io/${OWNER}/${FILE}:${RENOVATE_VERSION}",
@@ -67,7 +74,7 @@ target "full" {
6774
BASE_IMAGE_TYPE = "full"
6875
}
6976
cache-from = [
70-
"type=registry,ref=ghcr.io/${OWNER}/docker-build-cache:${FILE}-${RENOVATE_VERSION}-full",
77+
"type=registry,ref=ghcr.io/${OWNER}/docker-build-cache:${FILE}-full",
7178
]
7279
tags = [
7380
"ghcr.io/${OWNER}/${FILE}:${RENOVATE_VERSION}-full",
@@ -87,7 +94,7 @@ target "push-cache-slim" {
8794
"slim",
8895
]
8996
tags = [
90-
"ghcr.io/${OWNER}/docker-build-cache:${FILE}-${RENOVATE_VERSION}",
97+
"ghcr.io/${OWNER}/docker-build-cache:${FILE}",
9198
]
9299
}
93100

@@ -98,7 +105,7 @@ target "push-cache-full" {
98105
"full",
99106
]
100107
tags = [
101-
"ghcr.io/${OWNER}/docker-build-cache:${FILE}-${RENOVATE_VERSION}-full",
108+
"ghcr.io/${OWNER}/docker-build-cache:${FILE}-full",
102109
]
103110
}
104111

@@ -108,7 +115,6 @@ target "build-slim" {
108115

109116
target "build-full" {
110117
inherits = ["settings", "full"]
111-
112118
}
113119

114120
target "push-slim" {

tools/docker/bin/renovate

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
if [[ -f "/usr/local/etc/env" && -z "${CONTAINERBASE_ENV+x}" ]]; then
4+
# shellcheck source=/dev/null
5+
. /usr/local/etc/env
6+
fi
7+
8+
node /usr/local/renovate/dist/renovate.js "$@"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
if [[ -f "/usr/local/etc/env" && -z "${CONTAINERBASE_ENV+x}" ]]; then
4+
# shellcheck source=/dev/null
5+
. /usr/local/etc/env
6+
fi
7+
8+
node /usr/local/renovate/dist/config-validator.js "$@"

0 commit comments

Comments
 (0)