1

我有一个简单的 bash 脚本,它调用 github 上的 git 存储库(/home/user/simple_git.sh):

#!/bin/bash

# Change to the Git repoistory
cd /home/user/git/a_git_repo

remote=$(
    git ls-remote -h origin master |
    awk '{print $1}'
)

local=$(
    git rev-parse HEAD
)

printf "Local : %s\nRemote: %s\n" $local $remote

它提供以下输出:

Local : a10dc1d7d30ed67ed1e514a3c1ffc5a824cea14b
Remote: a10dc1d7d30ed67ed1e514a3c1ffc5a824cea14b

git 身份验证是通过 ssh 密钥完成的 - 以下是我的 .bashrc

# ssh
eval `ssh-agent -s`
ssh-add

该脚本作为用户以及使用 sudo (通过保留用户环境)运行得很好,即。

~/.simple_git.sh
or
sudo -E ~/simple_git.sh

但是,我还没有找到将脚本作为服务运行的方法(/etc/systemd/user/simple_git.service 或 /etc/systemd/system/simple_git.service)

[Unit]
Description=TestScript

[Service]
Type=simple
ExecStart=/home/user/simple_git.sh

我尝试使用 --user 选项运行 systemctl 命令以及修改 visudo 以包含该行

Defaults env_keep += SSH_AUTH_SOCK

但无济于事。每次我检查工作的状态时:

Feb 21 23:16:00 alarmpi systemd[484]: Started TestScript.
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: Permission denied (publickey).
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: fatal: Could not read from remote repository.
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: Please make sure you have the correct access rights
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: and the repository exists.
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: Local : a10dc1d7d30ed67ed1e514a3c1ffc5a824cea14b
4

2 回答 2

1

Systemd 未使用会话中的环境变量运行服务。我会建议你

  • 使用 git using https,它不需要身份验证(而不是ssh
  • 创建一个不受保护的“部署密钥”,它将位于标准位置 ( ~alarmpi/.ssh/id_rsa),git没有ssh-agent.
于 2017-02-22T11:45:31.417 回答
0

此时(工作实践政策)无法使用 https 连接到 github。虽然不受保护的部署密钥的想法很有用,但我最终还是使用了 systemctl --user import-environment ( wiki.archlinux.org/index.php/Systemd/User ) 来解决这个问题。

于 2017-05-03T13:00:37.647 回答