admin管理员组文章数量:1124561
Given a .gitlab-ci.yml
file in which it is needed to log in to a remote server via SSH
and run there a list of commands. I tried using here-doc
but without much luck. Based on my attempts to find the root of the problem, it looks like .gitlab-ci.yml
doesn't accept here-doc
. And it produced a bunch of errors.
Below is an excerpt:
deploy_to_server_job:
stage: deploy_to_server
tags:
- server-runner
image: ubuntu:22.04
script:
- chmod og= $ID_RSA
- |
ssh -tt -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP bash << EOF
docker system prune --force
echo "Running container with image: $FULL_IMAGE_NAME"
docker run -p 127.0.0.1:$SERVER_PORT:$SERVER_PORT \
--env JWT_AUTH_SECRET=$JWT_AUTH_SECRET \
--env JWT_AUTH_EXPIRES=$JWT_AUTH_EXPIRES \
--env JWT_REFRESH_SECRET=$JWT_REFRESH_SECRET \
--env JWT_REFRESH_EXPIRES=$JWT_REFRESH_EXPIRES \
--env MONGODB_URI=$MONGODB_URI \
--env PORT=$PORT \
-d --name admin-server $FULL_IMAGE_NAME
exit
EOF
Then I rewrote an ssh
command in one line:
- ssh -tt -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "whoami; echo Running container with image $FULL_IMAGE_NAME; docker system prune --force; docker run --name admin-server -d -p 127.0.0.1:$SERVER_PORT:$SERVER_PORT --env JWT_AUTH_SECRET=$JWT_AUTH_SECRET --env JWT_AUTH_EXPIRES=$JWT_AUTH_EXPIRES --env JWT_REFRESH_SECRET=$JWT_REFRESH_SECRET --env JWT_REFRESH_EXPIRES=$JWT_REFRESH_EXPIRES --env MONGODB_URI=$MONGODB_URI --env PORT=$PORT $FULL_IMAGE_NAME; exit"
All the error have gone except one:
bash: line 2: --env: command not found
I cannot figure out why the docker run
--env
flag is treated as a separate command. When changing all --env
to -e
, the error becomes bash: line 2: -e: command not found
(so, it is that flag from docker run
).
Question: In .gitlab-ci.yml
, how to properly put commands that will run on a remote server?
Note: I've just started learning GitLab
pipelines (I've thought I'd find a solution, but I've been struggling with this issue for a week), and maybe it's not only docker run
that will throw such errors.
Given a .gitlab-ci.yml
file in which it is needed to log in to a remote server via SSH
and run there a list of commands. I tried using here-doc
but without much luck. Based on my attempts to find the root of the problem, it looks like .gitlab-ci.yml
doesn't accept here-doc
. And it produced a bunch of errors.
Below is an excerpt:
deploy_to_server_job:
stage: deploy_to_server
tags:
- server-runner
image: ubuntu:22.04
script:
- chmod og= $ID_RSA
- |
ssh -tt -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP bash << EOF
docker system prune --force
echo "Running container with image: $FULL_IMAGE_NAME"
docker run -p 127.0.0.1:$SERVER_PORT:$SERVER_PORT \
--env JWT_AUTH_SECRET=$JWT_AUTH_SECRET \
--env JWT_AUTH_EXPIRES=$JWT_AUTH_EXPIRES \
--env JWT_REFRESH_SECRET=$JWT_REFRESH_SECRET \
--env JWT_REFRESH_EXPIRES=$JWT_REFRESH_EXPIRES \
--env MONGODB_URI=$MONGODB_URI \
--env PORT=$PORT \
-d --name admin-server $FULL_IMAGE_NAME
exit
EOF
Then I rewrote an ssh
command in one line:
- ssh -tt -i $ID_RSA -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "whoami; echo Running container with image $FULL_IMAGE_NAME; docker system prune --force; docker run --name admin-server -d -p 127.0.0.1:$SERVER_PORT:$SERVER_PORT --env JWT_AUTH_SECRET=$JWT_AUTH_SECRET --env JWT_AUTH_EXPIRES=$JWT_AUTH_EXPIRES --env JWT_REFRESH_SECRET=$JWT_REFRESH_SECRET --env JWT_REFRESH_EXPIRES=$JWT_REFRESH_EXPIRES --env MONGODB_URI=$MONGODB_URI --env PORT=$PORT $FULL_IMAGE_NAME; exit"
All the error have gone except one:
bash: line 2: --env: command not found
I cannot figure out why the docker run
--env
flag is treated as a separate command. When changing all --env
to -e
, the error becomes bash: line 2: -e: command not found
(so, it is that flag from docker run
).
Question: In .gitlab-ci.yml
, how to properly put commands that will run on a remote server?
Note: I've just started learning GitLab
pipelines (I've thought I'd find a solution, but I've been struggling with this issue for a week), and maybe it's not only docker run
that will throw such errors.
1 Answer
Reset to default 0Jim's comment gave me an idea, and I managed to find the root of the problem: MONGODB_URI
value should be quoted:
--env MONGODB_URI="$MONGODB_URI" \
本文标签: gitlabciyml Executing Multiple Commands on Remote Server via SSHStack Overflow
版权声明:本文标题:.gitlab-ci.yml: Executing Multiple Commands on Remote Server via SSH - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736639197a1945954.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
env
s. It appears that the issue is somehow caused by theMONGO_URI
value. Yes. I put the value between double quotes and it worked. Thank you! – ENIAC Commented yesterday