I have the following GitHub Actions file ./github/workflows/staging-deploy.yml
name: Deploy on Staging VM
on:
pull_request:
branches:
- staging
types:
- closed
permissions:
id-token: write
contents: read
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: 'Az CLI login'
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: 'Run Azure CLI commands'
run: |
az vm run-command invoke -g rclco-app -n rclco-app-staging-vm --command-id RunShellScript --scripts "cd /app && git config --global --add safe.directory /app && git fetch && git checkout staging && sudo systemctl restart backend"
Unfortunately, this does not appear to do anything after the action runs (and succeeds) in GitHub Actions. If I replace
--scripts "cd /app && git config --global --add safe.directory /app && git fetch && git checkout staging && sudo systemctl restart backend"
with
--scripts "cd /app && mkdir testdir"
/app/testdir
is created. It is showing a message that $HOME is not set, but I am unsure if that is the problem. Here is the output of my GitHub Actions:
When I ran the your workflow even I received the same error after creating app directory with mkdir app refer below:-
sudo mkdir app
Output:-
Workflow Output Same error :-
In order to resolve this error, I set HOME as my environment variable by adding this command in my az vm run command :-
export HOME=/home/usernameoflinuxvm
After adding this command if you receive this error
message": "Enable succeeded: \n[stdout]\n\n[stderr]\nfatal: not a git repository (or any of the parent directories): .git\n",
Just install git in your linux vm and initialize it in the app directory with git init
command:-
sudo apt-get update && sudo apt-get install git -y
run git init after cd into app dir
cd /app && git init
If you get the below error add git checkout -b staging
in your az vm run command.
Now getting this error "message": "Enable succeeded: \n[stdout]\n\n[stderr]\nerror: pathspec 'staging' did not match any file(s) known to git\n",
Make sure you create a backend.service
file so that the systemctl restart backend command works successfully.
cd /etc/systemd/system/
sudo nano backend.service
backend.service code:-
GNU nano 4.8 backend.service
[Unit] Description=Backend Service
[Service] ExecStart=/usr/bin/env bash -c 'cd pathtobackend && ./startupscript.sh'
Restart=always
[Install] WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable backend.service
This command switches the branch to staging:-
git checkout -b staging
My github workflow:-
name: Azure VM
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 'Az CLI login'
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: 'Run Azure CLI commands'
run: |
az vm run-command invoke -g siliconrg -n siliconvm --command-id RunShellScript --scripts "export HOME=/home/siliconuser && cd /app && git config --global --add safe.directory /app && git fetch && git checkout -b staging && sudo systemctl restart backend"
Azure CLI command that runs git fetch, switches branch and restarts backend.
az vm run command reference MS Document
az vm run-command invoke -g siliconrg -n siliconvm --command-id RunShellScript --scripts "export HOME=/home/siliconuser && cd /app && git config --global --add safe.directory /app && git fetch && git checkout -b staging && sudo systemctl restart backend"
Output Successful:-