Skip to content

Commit 3f5f5dc

Browse files
Merge pull request #833 from TransactionProcessing/task/#832_linux_release_workflow
Transition deployment to Linux-based service
2 parents 9e78643 + 02ea77e commit 3f5f5dc

File tree

1 file changed

+129
-37
lines changed

1 file changed

+129
-37
lines changed

.github/workflows/createrelease.yml

Lines changed: 129 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
5555
- name: Publish API
5656
if: ${{ github.event.release.prerelease == false }}
57-
run: dotnet publish "TransactionProcessor\TransactionProcessor.csproj" --configuration Release --output publishOutput -r win-x64 --self-contained
57+
run: dotnet publish "TransactionProcessor\TransactionProcessor.csproj" --configuration Release --output publishOutput -r linux-x64 --self-contained
5858

5959
- name: Build Release Package
6060
run: |
@@ -80,7 +80,7 @@ jobs:
8080
dotnet nuget push Nugets/TransactionProcessor.Database.${{ steps.get_version.outputs.VERSION }}.nupkg --api-key ${{ secrets.PRIVATEFEED_APIKEY }} --source ${{ secrets.PRIVATEFEED_URL }} --skip-duplicate
8181
8282
deploystaging:
83-
runs-on: stagingserver
83+
runs-on: [stagingserver,linux]
8484
needs: buildlinux
8585
environment: staging
8686
name: "Deploy to Staging"
@@ -90,30 +90,76 @@ jobs:
9090
uses: actions/[email protected]
9191
with:
9292
name: transactionprocessor
93-
94-
- name: Remove existing Windows service
93+
path: /tmp/transactionprocessor # Download to a temporary directory
94+
95+
- name: Remove existing service (if applicable)
9596
run: |
96-
$serviceName = "Transaction Processing - Transaction Processor"
97-
# Check if the service exists
98-
if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) {
99-
Stop-Service -Name $serviceName
100-
sc.exe delete $serviceName
101-
}
102-
97+
SERVICE_NAME="transactionprocessor"
98+
if systemctl is-active --quiet "$SERVICE_NAME"; then
99+
echo "Stopping existing service..."
100+
sudo systemctl stop "$SERVICE_NAME"
101+
fi
102+
if systemctl is-enabled --quiet "$SERVICE_NAME"; then
103+
echo "Disabling existing service..."
104+
sudo systemctl disable "$SERVICE_NAME"
105+
fi
106+
if [ -f "/etc/systemd/system/${SERVICE_NAME}.service" ]; then
107+
echo "Removing existing service unit file..."
108+
sudo rm "/etc/systemd/system/${SERVICE_NAME}.service"
109+
sudo systemctl daemon-reload
110+
fi
111+
103112
- name: Unzip the files
104113
run: |
105-
Expand-Archive -Path transactionprocessor.zip -DestinationPath "C:\txnproc\transactionprocessing\transactionprocessor" -Force
106-
107-
- name: Install as a Windows service
114+
sudo mkdir -p /opt/txnproc/transactionprocessing/transactionprocessor
115+
sudo unzip -o /tmp/transactionprocessor/transactionprocessor.zip -d /opt/txnproc/transactionprocessing/transactionprocessor
116+
117+
# IMPORTANT: Add a step to ensure the .NET runtime is installed on the server
118+
# This assumes it's not already there. If your base image already has it, you can skip this.
119+
- name: Install .NET Runtime
120+
run: |
121+
# Example for Ubuntu. Adjust based on your .NET version (e.g., 8.0, 7.0)
122+
# and if you need the SDK or just the runtime.
123+
# This uses Microsoft's package repository for the latest versions.
124+
wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
125+
sudo dpkg -i packages-microsoft-prod.deb
126+
rm packages-microsoft-prod.deb
127+
sudo apt update
128+
sudo apt install -y aspnetcore-runtime-9.0
129+
130+
- name: Install and Start as a Linux service
108131
run: |
109-
$serviceName = "Transaction Processing - Transaction Processor"
110-
$servicePath = "C:\txnproc\transactionprocessing\transactionprocessor\TransactionProcessor.exe"
111-
112-
New-Service -Name $serviceName -BinaryPathName $servicePath -Description "Transaction Processing - Transaction Processor" -DisplayName "Transaction Processing - Transaction Processor" -StartupType Automatic
113-
Start-Service -Name $serviceName
132+
SERVICE_NAME="transactionprocessor"
133+
# The WorkingDirectory is crucial for .NET apps to find appsettings.json and other files
134+
WORKING_DIRECTORY="/opt/txnproc/transactionprocessing/transactionprocessor"
135+
DLL_NAME="TransactionProcessor.dll" # Your application's DLL
136+
SERVICE_DESCRIPTION="Transaction Processing - Transaction Processor"
137+
138+
# Create a systemd service file
139+
echo "[Unit]" | sudo tee /etc/systemd/system/${SERVICE_NAME}.service
140+
echo "Description=${SERVICE_DESCRIPTION}" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
141+
echo "After=network.target" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
142+
echo "" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
143+
echo "[Service]" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
144+
# IMPORTANT: Use 'dotnet' to run your DLL
145+
echo "ExecStart=/usr/bin/dotnet ${WORKING_DIRECTORY}/${DLL_NAME}" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
146+
echo "WorkingDirectory=${WORKING_DIRECTORY}" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
147+
echo "Restart=always" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
148+
echo "User=youruser" # IMPORTANT: Change to a dedicated, less privileged user
149+
echo "Group=yourgroup" # IMPORTANT: Change to a dedicated, less privileged group
150+
echo "Environment=ASPNETCORE_ENVIRONMENT=Production" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service # Example
151+
echo "" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
152+
echo "[Install]" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
153+
echo "WantedBy=multi-user.target" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
154+
155+
# Reload systemd, enable, and start the service
156+
sudo systemctl daemon-reload
157+
sudo systemctl enable "$SERVICE_NAME"
158+
sudo systemctl start "$SERVICE_NAME"
159+
sudo systemctl status "$SERVICE_NAME" --no-pager # For debugging/verification
114160

115161
deployproduction:
116-
runs-on: productionserver
162+
runs-on: [productionserver, linux]
117163
needs: [buildlinux, deploystaging]
118164
environment: production
119165
name: "Deploy to Production"
@@ -123,24 +169,70 @@ jobs:
123169
uses: actions/[email protected]
124170
with:
125171
name: transactionprocessor
126-
127-
- name: Remove existing Windows service
172+
path: /tmp/transactionprocessor # Download to a temporary directory
173+
174+
- name: Remove existing service (if applicable)
128175
run: |
129-
$serviceName = "Transaction Processing - Transaction Processor"
130-
# Check if the service exists
131-
if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) {
132-
Stop-Service -Name $serviceName
133-
sc.exe delete $serviceName
134-
}
135-
176+
SERVICE_NAME="transactionprocessor"
177+
if systemctl is-active --quiet "$SERVICE_NAME"; then
178+
echo "Stopping existing service..."
179+
sudo systemctl stop "$SERVICE_NAME"
180+
fi
181+
if systemctl is-enabled --quiet "$SERVICE_NAME"; then
182+
echo "Disabling existing service..."
183+
sudo systemctl disable "$SERVICE_NAME"
184+
fi
185+
if [ -f "/etc/systemd/system/${SERVICE_NAME}.service" ]; then
186+
echo "Removing existing service unit file..."
187+
sudo rm "/etc/systemd/system/${SERVICE_NAME}.service"
188+
sudo systemctl daemon-reload
189+
fi
190+
136191
- name: Unzip the files
137192
run: |
138-
Expand-Archive -Path transactionprocessor.zip -DestinationPath "C:\txnproc\transactionprocessing\transactionprocessor" -Force
139-
140-
- name: Install as a Windows service
193+
sudo mkdir -p /opt/txnproc/transactionprocessing/transactionprocessor
194+
sudo unzip -o /tmp/transactionprocessor/transactionprocessor.zip -d /opt/txnproc/transactionprocessing/transactionprocessor
195+
196+
# IMPORTANT: Add a step to ensure the .NET runtime is installed on the server
197+
# This assumes it's not already there. If your base image already has it, you can skip this.
198+
- name: Install .NET Runtime
199+
run: |
200+
# Example for Ubuntu. Adjust based on your .NET version (e.g., 8.0, 7.0)
201+
# and if you need the SDK or just the runtime.
202+
# This uses Microsoft's package repository for the latest versions.
203+
wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
204+
sudo dpkg -i packages-microsoft-prod.deb
205+
rm packages-microsoft-prod.deb
206+
sudo apt update
207+
sudo apt install -y aspnetcore-runtime-9.0
208+
209+
- name: Install and Start as a Linux service
141210
run: |
142-
$serviceName = "Transaction Processing - Transaction Processor"
143-
$servicePath = "C:\txnproc\transactionprocessing\transactionprocessor\TransactionProcessor.exe"
144-
145-
New-Service -Name $serviceName -BinaryPathName $servicePath -Description "Transaction Processing - Transaction Processor" -DisplayName "Transaction Processing - Transaction Processor" -StartupType Automatic
146-
Start-Service -Name $serviceName
211+
SERVICE_NAME="transactionprocessor"
212+
# The WorkingDirectory is crucial for .NET apps to find appsettings.json and other files
213+
WORKING_DIRECTORY="/opt/txnproc/transactionprocessing/transactionprocessor"
214+
DLL_NAME="TransactionProcessor.dll" # Your application's DLL
215+
SERVICE_DESCRIPTION="Transaction Processing - Transaction Processor"
216+
217+
# Create a systemd service file
218+
echo "[Unit]" | sudo tee /etc/systemd/system/${SERVICE_NAME}.service
219+
echo "Description=${SERVICE_DESCRIPTION}" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
220+
echo "After=network.target" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
221+
echo "" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
222+
echo "[Service]" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
223+
# IMPORTANT: Use 'dotnet' to run your DLL
224+
echo "ExecStart=/usr/bin/dotnet ${WORKING_DIRECTORY}/${DLL_NAME}" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
225+
echo "WorkingDirectory=${WORKING_DIRECTORY}" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
226+
echo "Restart=always" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
227+
echo "User=youruser" # IMPORTANT: Change to a dedicated, less privileged user
228+
echo "Group=yourgroup" # IMPORTANT: Change to a dedicated, less privileged group
229+
echo "Environment=ASPNETCORE_ENVIRONMENT=Production" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service # Example
230+
echo "" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
231+
echo "[Install]" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
232+
echo "WantedBy=multi-user.target" | sudo tee -a /etc/systemd/system/${SERVICE_NAME}.service
233+
234+
# Reload systemd, enable, and start the service
235+
sudo systemctl daemon-reload
236+
sudo systemctl enable "$SERVICE_NAME"
237+
sudo systemctl start "$SERVICE_NAME"
238+
sudo systemctl status "$SERVICE_NAME" --no-pager # For debugging/verification

0 commit comments

Comments
 (0)