【发布时间】:2021-07-08 16:45:16
【问题描述】:
我也想在 GitLab CI 上运行 have a GitHub Workflow:
name: Node.js CI
on:
push:
branches:
- '*'
pull_request:
branches:
- master
jobs:
lint:
runs-on: ubuntu-18.04
name: Lint
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '12'
- run: yarn
- run: yarn lint
build:
runs-on: ubuntu-18.04
name: Build + Test
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '12'
- run: yarn
- run: yarn build
- run: yarn test
我尝试使用 Docker 执行器模板设置新作业:
docker-build:
# Use the official docker image.
image: docker:latest
stage: build
services:
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
# Default branch leaves tag empty (= latest tag)
# All other branches are tagged with the escaped branch name (commit ref slug)
script:
- |
if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
tag=""
echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
else
tag=":$CI_COMMIT_REF_SLUG"
echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
fi
- docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
- docker push "$CI_REGISTRY_IMAGE${tag}"
# Run this job in a branch where a Dockerfile exists
rules:
- if: $CI_COMMIT_BRANCH
exists:
- Dockerfile
我对其进行了修改以安装node:12 和docker-compose,但它不起作用,这样做感觉不对。在 GitLab CI 作业中运行 Node.js、Docker 和 docker-compose 是否有更简单/正确的方法?
这就是我尝试过的,但如前所述,它失败了:
docker-build:
# Use the official docker image.
image: docker:latest
stage: build
services:
- docker:dind
before_script:
- docker --version
- echo "shell" $0
- uname -a
- dnf -y install curl
- curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | sh
- export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
- nvm install 12
- nvm use 12
- curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- chmod +x /usr/local/bin/docker-compose
- docker-compose --version
# Default branch leaves tag empty (= latest tag)
# All other branches are tagged with the escaped branch name (commit ref slug)
script:
- echo "hello world"
结果:
Pulling docker image docker:latest ...
Using docker image sha256:08bdaf2f88f90320cd3e92a469969efb1f066c6d318631f94e7864828abd7c75 for docker:latest with digest docker@sha256:a61102937d2bda8319882998ef1ffa27387617f6eea6c298b18a05f7fba82c0d ...
Preparing environment 00:01
Running on runner-ed2dce3a-project-27995513-concurrent-0 via runner-ed2dce3a-srm-1625761937-d755bac1...
Getting source from Git repository 00:02
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/.../docker-compose/.git/
Created fresh repository.
Checking out 659d60d0 as test-gitlab-ci...
Skipping Git submodules setup
Executing "step_script" stage of the job script 00:00
Using docker image sha256:08bdaf2f88f90320cd3e92a469969efb1f066c6d318631f94e7864828abd7c75 for docker:latest with digest docker@sha256:a61102937d2bda8319882998ef1ffa27387617f6eea6c298b18a05f7fba82c0d ...
$ docker --version
Docker version 20.10.7, build f0df350
$ echo "shell" $0
shell /bin/sh
$ uname -a
Linux runner-ed2dce3a-project-27995513-concurrent-0 4.19.78-coreos #1 SMP Mon Oct 14 22:56:39 -00 2019 x86_64 Linux
$ dnf -y install curl
/bin/sh: eval: line 119: dnf: not found
更新:Aritra 的回答让我更进一步,但它仍然无法正常工作:
Using docker image sha256:08bdaf2f88f90320cd3e92a469969efb1f066c6d318631f94e7864828abd7c75 for docker:latest with digest docker@sha256:a61102937d2bda8319882998ef1ffa27387617f6eea6c298b18a05f7fba82c0d ...
$ docker --version
Docker version 20.10.7, build f0df350
$ echo "shell" $0
shell /bin/sh
$ uname -a
Linux runner-0277ea0f-project-27995513-concurrent-0 4.19.78-coreos #1 SMP Mon Oct 14 22:56:39 -00 2019 x86_64 Linux
$ apk add curl
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/community/x86_64/APKINDEX.tar.gz
(1/4) Installing brotli-libs (1.0.9-r3)
(2/4) Installing nghttp2-libs (1.42.0-r1)
(3/4) Installing libcurl (7.77.0-r1)
(4/4) Installing curl (7.77.0-r1)
Executing busybox-1.32.1-r6.trigger
OK: 13 MiB in 24 packages
$ apk add -U curl bash ca-certificates openssl ncurses coreutils python2 make gcc g++ libgcc linux-headers grep util-linux binutils findutils
(1/53) Upgrading musl (1.2.2-r0 -> 1.2.2-r1)
(2/53) Installing readline (8.1.0-r0)
(3/53) Installing bash (5.1.0-r0)
Executing bash-5.1.0-r0.post-install
(4/53) Installing libgcc (10.2.1_pre1-r3)
(5/53) Installing libstdc++ (10.2.1_pre1-r3)
(6/53) Installing binutils (2.35.2-r1)
(7/53) Installing libacl (2.2.53-r0)
(8/53) Installing libattr (2.4.48-r0)
(9/53) Installing skalibs (2.10.0.0-r0)
(10/53) Installing s6-ipcserver (2.10.0.0-r0)
(11/53) Installing utmps (0.1.0.0-r0)
Executing utmps-0.1.0.0-r0.pre-install
(12/53) Installing coreutils (8.32-r2)
(13/53) Installing findutils (4.8.0-r0)
(14/53) Installing libgomp (10.2.1_pre1-r3)
(15/53) Installing libatomic (10.2.1_pre1-r3)
(16/53) Installing libgphobos (10.2.1_pre1-r3)
(17/53) Installing gmp (6.2.1-r0)
(18/53) Installing isl22 (0.22-r0)
(19/53) Installing mpfr4 (4.1.0-r0)
(20/53) Installing mpc1 (1.2.0-r0)
(21/53) Installing gcc (10.2.1_pre1-r3)
(22/53) Installing musl-dev (1.2.2-r1)
(23/53) Installing libc-dev (0.7.2-r3)
(24/53) Installing g++ (10.2.1_pre1-r3)
(25/53) Installing pcre (8.44-r0)
(26/53) Installing grep (3.6-r0)
(27/53) Installing linux-headers (5.7.8-r0)
(28/53) Installing make (4.3-r0)
(29/53) Installing ncurses (6.2_p20210109-r0)
(30/53) Installing openssl (1.1.1k-r0)
(31/53) Installing libbz2 (1.0.8-r1)
(32/53) Installing expat (2.2.10-r1)
(33/53) Installing libffi (3.3-r2)
(34/53) Installing gdbm (1.19-r0)
(35/53) Installing sqlite-libs (3.34.1-r0)
(36/53) Installing python2 (2.7.18-r1)
(37/53) Installing libblkid (2.36.1-r1)
(38/53) Installing blkid (2.36.1-r1)
(39/53) Installing libcap-ng (0.8.2-r0)
(40/53) Installing setpriv (2.36.1-r1)
(41/53) Installing libmount (2.36.1-r1)
(42/53) Installing libsmartcols (2.36.1-r1)
(43/53) Installing findmnt (2.36.1-r1)
(44/53) Installing mcookie (2.36.1-r1)
(45/53) Installing hexdump (2.36.1-r1)
(46/53) Installing lsblk (2.36.1-r1)
(47/53) Installing libuuid (2.36.1-r1)
(48/53) Installing libfdisk (2.36.1-r1)
(49/53) Installing sfdisk (2.36.1-r1)
(50/53) Installing cfdisk (2.36.1-r1)
(51/53) Installing partx (2.36.1-r1)
(52/53) Installing libeconf (0.3.8-r0)
(53/53) Installing util-linux (2.36.1-r1)
Executing busybox-1.32.1-r6.trigger
OK: 253 MiB in 76 packages
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 14926 100 14926 0 0 182k 0 --:--:-- --:--:-- --:--:-- 184k
=> Downloading nvm as script to '/root/.nvm'
=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
OR
=> Append the following lines to the correct file yourself:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
$ export NVM_DIR="$HOME/.nvm"
$ . "$NVM_DIR/nvm.sh"
$ nvm install 12 -s
Downloading and installing node v12.22.3...
Downloading https://nodejs.org/dist/v12.22.3/node-v12.22.3-linux-x64.tar.gz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.22.3 (npm v)
Creating default alias: default -> 12 (-> v12.22.3 *)
$ nvm use 12
Now using node v12.22.3 (npm v)
$ which node
/root/.nvm/versions/node/v12.22.3/bin/node
$ node --version
/bin/sh: eval: line 135: node: not found
【问题讨论】:
标签: node.js docker docker-compose gitlab-ci