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.