在 Docker 容器构建/运行命令上设置 GitHub Actions Secret
我正处于构建自动化管道的早期阶段。仍处于探索阶段。现在,我正在努力为我要运行应用程序的容器设置环境变量。
这个想法是将我的环境变量设置为 GitHub Actions Secrets,并在运行 docker 容器命令时在容器中配置它们。该容器是根据 Docker Composer 文件构建的。
现在,我有这个工作:
- name: Backend - Build Container (proof of concept)
run: |
BACKEND_CONTAINER_SH_SCRIPT='eval "GITHUB_USER=123 GITHUB_REPO_NAME=321 docker-compose -f ss-build-files/ubuntu-container-build.yml up -d && docker-compose -f ss-build-files/ubuntu-container-build.yml logs";'
echo "${{ secrets.CONFIG_SERVER_SSH_KEY }}" > id_rsa_ssh_key_server_temp.pem;
chmod 600 id_rsa_ssh_key_server_temp.pem;
ssh -v -t -t -i id_rsa_ssh_key_server_temp.pem -o StrictHostKeyChecking=no ubuntu@${{ env.CONFIG_SERVER_IP }} "$BACKEND_CONTAINER_SH_SCRIPT"
rm id_rsa_ssh_key_server_temp.pem;
echo "Status check: jobs completed successfully!";
但是,当我尝试与 GitHub 机密集成时,它在 GitHub Runner 中返回一个错误:
- name: Backend - Build Container (proof of concept)
run: |
BACKEND_CONTAINER_SH_SCRIPT='eval "GITHUB_USER=x${{ secrets.TEST_1 }} GITHUB_REPO_NAME=xx${{ secrets.TEST_2 }} docker-compose -f ss-build-files/ubuntu-container-build.yml up -d && docker-compose -f ss-build-files/ubuntu-container-build.yml logs";'
echo "${{ secrets.CONFIG_SERVER_SSH_KEY }}" > id_rsa_ssh_key_server_temp.pem;
chmod 600 id_rsa_ssh_key_server_temp.pem;
ssh -v -t -t -i id_rsa_ssh_key_server_temp.pem -o StrictHostKeyChecking=no ubuntu@${{ env.CONFIG_SERVER_IP }} "$BACKEND_CONTAINER_SH_SCRIPT"
rm id_rsa_ssh_key_server_temp.pem;
echo "Status check: jobs completed successfully!";
错误:
debug1: Sending command: eval "GITHUB_USER=x*** GITHUB_REPO_NAME=xx*** docker-compose -f ss-build-files/ubuntu-container-build.yml up -d && docker-compose -f ss-build-files/ubuntu-container-build.yml logs";
bash: line 1: secret1: command not found
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
Connection to 3.90.173.47 closed.
Transferred: sent 2892, received 2676 bytes, in 0.5 seconds
Bytes per second: sent 6131.9, received 5673.9
debug1: Exit status 127
Error: Process completed with exit code 127.
注意:我以这种方式构建,因为eval "GITHUB_USER=123 GITHUB_REPO_NAME=321 docker-compose -f ss-build-files/ubuntu-container-build.yml up -d && docker-compose -f ss-build-files/ubuntu-container-build.yml logs"
考虑到我希望完成的任务,这似乎是它的唯一工作方式,即创建一个节点脚本,该脚本从存储库读取所有 Secret(应用程序有许多环境变量)并输出像这样的字符串:
TEST_1=${{ secrets.TEST_1 }} TEST_2=${{ secrets.TEST_2 }}
我将把这个字符串与 Docker 容器命令的其余部分合并。
任何人都知道它给我这个错误的原因,或者可能有另一个想法如何在不硬编码服务器/容器中的 .env 文件的情况下自动化这部分?