This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy latest images on DST Server via Terraform apply | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
# Step 1: Check out the code from the repository | |
- name: Checkout code from the repository | |
uses: actions/checkout@v3 | |
# Step 2: Extract the server IP | |
- name: Extract Server IP | |
id: extract_ip | |
run: | | |
echo "Reading terraform/variables.tf for server_ip..." | |
SERVER_IP=$(grep -A 3 -E '^variable "server_ip"' infra/terraform/variables.tf | grep 'default' | sed -n 's/.*default *= *"\([^"]*\)".*/\1/p') | |
echo "Extracted Server IP: ${SERVER_IP}" | |
echo "SERVER_IP=${SERVER_IP}" >> $GITHUB_ENV # Set it as an environment variable | |
echo "server_ip=${SERVER_IP}" >> $GITHUB_ENV # Use a different output name if necessary | |
# Step 3: Set up SSH using the PEM private key | |
- name: Set up SSH with PEM private key | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.SECRET_DST_PRIVATE_KEY }}" > ~/.ssh/data_enginering_machine.pem | |
chmod 600 ~/.ssh/data_enginering_machine.pem | |
SERVER_IP=${{ env.SERVER_IP }} # Access the environment variable set earlier | |
echo "Extracted Server IP: $SERVER_IP" | |
# Try to retrieve the SSH host keys | |
ssh-keyscan -H "$SERVER_IP" >> ~/.ssh/known_hosts || { | |
echo "Failed to run ssh-keyscan on $SERVER_IP" | |
exit 1 | |
} | |
# Debug | |
- name: Debug | |
run: | | |
echo "Private Key:" | |
cat ~/.ssh/data_enginering_machine.pem || true | |
echo "Known hosts:" | |
cat ~/.ssh/known_hosts || true | |
# Print the current known_hosts | |
- name: Show known_hosts | |
run: cat ~/.ssh/known_hosts | |
# Step 4: Copy script to server | |
- name: Copy script to server | |
run: | | |
SERVER_IP=${{ env.SERVER_IP }} # Access the environment variable set earlier | |
echo "Copying script to server with IP: $SERVER_IP" | |
scp -i ~/.ssh/data_enginering_machine.pem -o StrictHostKeyChecking=no infra/scripts/deploy-on-dst.sh ubuntu@$SERVER_IP:/home/ubuntu/ | |
# Step 5: Run script via SSH | |
- name: Run script via SSH | |
run: | | |
SERVER_IP=${{ env.SERVER_IP }} # Access the environment variable set earlier | |
echo "Deploying to server with IP: $SERVER_IP" | |
ssh -i ~/.ssh/data_enginering_machine.pem -o StrictHostKeyChecking=no ubuntu@$SERVER_IP << 'EOF' | |
chmod +x /home/ubuntu/deploy-on-dst.sh | |
/home/ubuntu/deploy-on-dst.sh | |
EOF |