0

I'm trying to run bash script in my GitLab pipeline which looks like:

gcloud compute instances list --format=yaml --filter=tags.items:${SERVER_TAG} --format='value(name)' | while IFS= read -r instance_name
do
    instances+=('${instance_name}')
    echo "-----------------------------------"
    echo "L1 Instance Name: ${instance_name}"
    #gcloud compute ssh ${instance_name} --quiet --tunnel-through-iap --zone=us-east4-c --project=${PROJECT_ID} --impersonate-service-account=ci-cd-pipeline@iam.gserviceaccount.com --command="whoami"
    #sleep 60
    echo "==================================="
done

echo ">>1<<"

gcloud compute instances list --format=yaml --filter=tags.items:${SERVER_TAG} --format='value(name)' | while IFS= read -r instance_name
do
    instances+=('${instance_name}')
    echo "-----------------------------------"
    echo "L2 Instance Name: ${instance_name}"
    gcloud compute ssh ${instance_name} --quiet --tunnel-through-iap --zone=us-east4-c --project=${PROJECT_ID} --impersonate-service-account=ci-cd-pipeline@iam.gserviceaccount.com --command="whoami"
    sleep 120
    echo "==================================="
done

echo ">>2<<"

First part of it will be executed as expected:

-----------------------------------
L1 Instance Name: dev-001
===================================
-----------------------------------
L1 Instance Name: dev-002
===================================
>>1<<

but second part of it will only execute once:

-----------------------------------
L2 Instance Name: dev-001
(...)
WARNING: This command is using service account impersonation. All API calls will be executed as [ci-cd-pipeline@iam.gserviceaccount.com].
Warning: Permanently added 'compute.8057989512480153153' (ED25519) to the list of known hosts.
sa_104167209456792997386
===================================
>>2<<

Why instance dev-002 is not getting called by gcloud compute ssh by the loop?

PS. Solution from While loop stops reading after the first line in Bash is not working for gcloud compute ssh. In that case I have opened new more relative questions at gcloud compute ssh - how to make read from /dev/null? as above solution is not real solution for closing OP.

JackTheKnife
  • 3,795
  • 8
  • 57
  • 117
  • 1
    `gcloud compute ssh` undoubtedly behaves the same way `ssh` does, and drains stdin so that the while loop doesn't get anything else as in the duplicate. – that other guy Aug 15 '23 at 22:03
  • @thatotherguy ok, but provided example as the solution will not work for `gcloud compute ssh` – JackTheKnife Aug 15 '23 at 22:52
  • I'm unable to reproduce that problem. The top solution suggests passing `-n` to ssh, or `< /dev/null` generally. The second highest suggests using a different FD to read in the file loop. All three appear to work for `cloud compute ssh` for me. What were your attempts, and how did they fail? – that other guy Aug 15 '23 at 23:36
  • @thatotherguy adding `-n` error out as `ERROR: (gcloud.compute.ssh) unrecognized arguments: -n `. Adding `< /dev/null` caused pipeline job to stall. I have checked few solutions and adding `sleep 30` just after `glcloud compute ssh` fixed the problem. – JackTheKnife Aug 16 '23 at 00:56
  • 1
    You'd have to use `gcloud compute ssh ... -- -n whoami` as this is how you pass arguments to ssh with gcloud (see `gcloud compute ssh --help`). Adding a sleep after the `gcloud compute ssh` in the loop would not have fixed this particular problem, and I don't know why `< /dev/null` would cause anything to stall. I would really recommend double checking these assertions, but I voted to reopen the question in the mean time. – that other guy Aug 16 '23 at 17:13
  • @thatotherguy now I see what I have missed - `--` before `-n`. Regarding `< /dev/null` pipeline job stuck on three dots output in the gitlab. Maybe just a bad luck with the gitlab runner yesterday. You can close my question as the dup. Ppl can find all in the comments ;) – JackTheKnife Aug 16 '23 at 18:16

0 Answers0