Does anyone know why my saved output in github actions workflow doesn’t work on SSH with appleboy/ssh-action?

So, I am working with simple NestJS application, docker, and terraform. My CI/CD scripts consists of 3 jobs, Docker, Terraform, and Deploy (with GCP). So I need to store the external IP of my created vm instance (by terraform) during the Terraform job. When I use the stored external IP, it is worked when I print it using echo. But, it seems doesn’t work when I use it in SSH with appleboy/ssh-action. Here is the detail.

Screenshot of my GitHub Actions Jobs

And here is my CI/CD scripts.

name: 'Automatic Deployment with Terraform and Docker to GCP'

on:
  push:
    branches: [ "master" ]
  pull_request:

permissions:
  contents: read

jobs:
  docker:
    name: 'Docker'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v2

      - name: Docker Hub Authentication
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Build Docker Image
        run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ vars.DOCKER_IMAGE_NAME }}:latest .

      - name: Tag Docker Image
        run: docker tag ${{ secrets.DOCKERHUB_USERNAME }}/${{ vars.DOCKER_IMAGE_NAME }}:latest ${{ secrets.DOCKERHUB_USERNAME }}/${{ vars.DOCKER_IMAGE_NAME }}:latest

      - name: Push Docker Image to Docker Hub
        run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ vars.DOCKER_IMAGE_NAME }}:latest


  terraform:
    name: 'Terraform'
    runs-on: ubuntu-latest
    outputs:
      external_ip: ${{ steps.store.outputs.vm_external_ip }}
    # Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest
    defaults:
      run:
        shell: bash

    steps:
    # Checkout the repository to the GitHub Actions runner
    - name: Checkout
      uses: actions/checkout@v4

    # Authenticate via gcloud CLI
    - name: Setup gcloud CLI
      uses: google-github-actions/[email protected]
      with:
        project_id: ${{ secrets.GCP_PROJECT_ID }}
        service_account_key: ${{ secrets.GOOGLE_CREDENTIALS }}  
        export_default_credentials: true

    # Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token
    - name: Setup Terraform
      uses: hashicorp/setup-terraform@v1
      with:
        cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}

    - name: Create Terraform Variables
      id: vars
      run: |
        cat > terraform.tfvars <<EOF
        project="${{ secrets.GCP_PROJECT_ID }}"
        region="${{ vars.GCP_REGION }}"
        zone="${{ vars.GCP_ZONE }}"
        ssh_pub_key="${{ secrets.SSH_PUBLIC_KEY }}"
        ssh_username="${{ secrets.GCP_USER }}"
        vm_instance_name="${{ vars.VM_INSTANCE_NAME }}"
        EOF

    # Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
    - name: Terraform Init
      run: terraform init

    # Checks that all Terraform configuration files adhere to a canonical format
    - name: Terraform Format
      run: terraform fmt

    # Generates an execution plan for Terraform
    - name: Terraform Plan
      run: terraform plan -out=plan

      # On push to "master", build or change infrastructure according to Terraform configuration files
      # Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks
    - name: Terraform Apply
      run: terraform apply "plan"

    - name: Store Terraform Output
      run: |
        EXTERNAL_IP=$(terraform-bin output -raw vm_external_ip)
        echo "VM_EXTERNAL_IP=$EXTERNAL_IP" >> $GITHUB_ENV
        echo "not env: $EXTERNAL_IP"
        echo "env: ${{ env.VM_EXTERNAL_IP }}"
    
    - name: Echo Terraform Output 
      id: store
      env:
        EXTERNAL_IP: ${{ env.VM_EXTERNAL_IP }}
      run: |
        echo "env directly: ${{ env.VM_EXTERNAL_IP }}"
        echo "env variable: $EXTERNAL_IP"
        echo "vm_external_ip=$EXTERNAL_IP" >> "$GITHUB_OUTPUT"

    # - name: Terraform Destroy
    #   run: terraform destroy -auto-approve


  deploy:
    runs-on: ubuntu-latest
    needs: [docker, terraform]
    steps:
      - name: Echo VM External IP
        env:
          VM_EXTERNAL_IP: ${{ needs.terraform.outputs.external_ip }}
        run: echo "$VM_EXTERNAL_IP"

      - name: Set up Cloud CLI
        uses: google-github-actions/[email protected]
        with:
          project_id: ${{ secrets.GCP_PROJECT_ID }}
          service_account_key: ${{ secrets.GOOGLE_CREDENTIALS }}
          export_default_credentials: true

      - name: Access gcloud CLI
        run: |
          gcloud auth configure-docker

      - name: Checkout repository
        uses: actions/checkout@v3

      - name: SSH into GCP and Run Docker Container
        env:
            VM_EXTERNAL_IP: ${{ needs.terraform.outputs.external_ip }}
        uses: appleboy/ssh-action@master
        with:
          host: $VM_EXTERNAL_IP
          username: ${{ secrets.GCP_USER }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ vars.DOCKER_IMAGE_NAME }}:latest
            docker run -d -p 3000:3000 ${{ secrets.DOCKERHUB_USERNAME }}/${{ vars.DOCKER_IMAGE_NAME }}:latest

Thank you very much.

New contributor

Iqbal Pahlevi A is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật