无法在 gitlab-ci.yml 脚本中使用“systemctl”

问题描述 投票:0回答:1

这是我的任务。

Design an automation deployed with Gitlab pipelines to create load 
balanced simple nginx hosted on 1 or more virtual machines 
on AWS with the following assumption

1. CIDR retrieve from REST API https://FDQN/vend ip _return
{
"ip_address":"192.168.0.0"
"subnet_size"."/16"
}
2. Create subnets with size /24
3. Generate SSH key for VM credential

1. Take into consideration CSP Best Practices such as security and resiliency
2. Take into consideration coding scripting practices
3. Leverage on native cloud metrics logging for error handling
4. Use bash for the stack, Gitlab for the IAC pipeline

这是我用于 gitlab-ci.yml 的脚本

stages:
  - deploy

deploy:
  stage: deploy

  before_script:
  # Create .ssh dir to store private key
  - mkdir ~/.ssh
  - echo "$AWS_PRIVATE_KEY" > ~/.ssh/id_rsa
  - chmod 600 ~/.ssh/id_rsa
  #Create Folder for nginx
  - mkdir -p /var/www/html
  - chmod 600 /var/www/html

  script:
    - echo "Start of script"
    - apt-get update -y
    - apt-get install -y nginx

    - systemctl start nginx
    - systemctl enable nginx
    - systemctl status nginx

但是我一直收到错误1,


Setting up nginx (1.18.0-6ubuntu14.4) ...
Processing triggers for libc-bin (2.35-0ubuntu3.6) ...
$ systemctl start nginx
/usr/bin/bash: line 157: systemctl: command not found
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: exit code 1

这个问题是 gitlab runner 的问题还是我正在使用的 aws 帐户的用户权限问题?

amazon-web-services gitlab gitlab-ci
1个回答
0
投票

在 GitLab CI/CD 管道中,由于 CI 环境的隔离性质,直接访问 systemd 命令(如

systemctl
)是受限

所以你应该在后台运行服务或使用 Docker 容器。

启动服务作为后台进程:

stages:
  - deploy

deploy:
  stage: deploy
  script:
    - nginx -g 'daemon off;'

作为 Docker 镜像启动服务:

stages:
  - deploy

deploy:
  stage: deploy
  image: nginx:latest
  script:
    - nginx -g 'daemon off;'

有用的链接:

https://developers.redhat.com/blog/2014/05/05/running-systemd-within-docker-container https://developers.redhat.com/blog/2016/09/13/running-systemd-in-a-non-privileged-container

© www.soinside.com 2019 - 2024. All rights reserved.