Skip to content

Commit 3b2babf

Browse files
committed
Update GitHub Actions
1 parent a351f19 commit 3b2babf

File tree

3 files changed

+89
-37
lines changed

3 files changed

+89
-37
lines changed

.github/workflows/build-container.yml

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ on:
1515
# Only update envs here if you need to change them for this workflow
1616
env:
1717
DOCKER_BUILDKIT: 1
18-
KAMAL_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
19-
KAMAL_REGISTRY_USERNAME: ${{ github.actor }}
18+
KAMAL_DEPLOY_HOST: ${{ secrets.KAMAL_DEPLOY_HOST }}
2019

2120
jobs:
2221
build-container:
@@ -33,6 +32,24 @@ jobs:
3332
echo "repository_name_lower=$(echo ${{ github.repository }} | cut -d '/' -f 2 | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
3433
echo "org_name=$(echo ${{ github.repository }} | cut -d '/' -f 1)" >> $GITHUB_ENV
3534
35+
# Set SERVICE_LABEL: derive from GITHUB_REPOSITORY (replace dots with dashes)
36+
echo "SERVICE_LABEL=$(echo ${{ github.repository }} | cut -d '/' -f 2 | tr '.' '-')" >> $GITHUB_ENV
37+
38+
# Set KAMAL_DEPLOY_HOST: use secret if available, otherwise use repository name
39+
if [ -n "${{ secrets.KAMAL_DEPLOY_HOST }}" ]; then
40+
DEPLOY_HOST="${{ secrets.KAMAL_DEPLOY_HOST }}"
41+
else
42+
DEPLOY_HOST="$(echo ${{ github.repository }} | cut -d '/' -f 2)"
43+
fi
44+
45+
# Validate KAMAL_DEPLOY_HOST contains at least one '.'
46+
if [[ ! "$DEPLOY_HOST" == *.* ]]; then
47+
echo "Error: KAMAL_DEPLOY_HOST must contain a hostname, e.g. example.com (got: $DEPLOY_HOST)"
48+
exit 1
49+
fi
50+
51+
echo "KAMAL_DEPLOY_HOST=$DEPLOY_HOST" >> $GITHUB_ENV
52+
3653
# This step is for the deployment of the templates only, safe to delete
3754
- name: Modify csproj for template deploy
3855
env:
@@ -61,6 +78,11 @@ jobs:
6178
working-directory: ./MyApp.Client
6279
run: npm install
6380

81+
- name: Build client
82+
if: steps.check_client.outputs.client_exists == 'true'
83+
working-directory: ./MyApp.Client
84+
run: npm run build
85+
6486
- name: Install x tool
6587
run: dotnet tool install -g x
6688

@@ -79,16 +101,21 @@ jobs:
79101
uses: docker/login-action@v3
80102
with:
81103
registry: ghcr.io
82-
username: ${{ env.KAMAL_REGISTRY_USERNAME }}
83-
password: ${{ env.KAMAL_REGISTRY_PASSWORD }}
104+
username: ${{ github.actor }}
105+
password: ${{ secrets.GITHUB_TOKEN }}
84106

85107
- name: Setup .NET
86108
uses: actions/setup-dotnet@v5
87109
with:
88-
dotnet-version: 8.0.x
110+
dotnet-version: 10.0.x
89111

90112
- name: Build and push Docker image
91113
env:
92114
SERVICESTACK_LICENSE: ${{ secrets.SERVICESTACK_LICENSE }}
115+
KAMAL_DEPLOY_HOST: ${{ secrets.KAMAL_DEPLOY_HOST }}
93116
run: |
94-
dotnet publish --os linux --arch x64 -c Release -p:ContainerRepository=${{ env.image_repository_name }} -p:ContainerRegistry=ghcr.io -p:ContainerImageTags=latest -p:ContainerPort=80 -p:ContainerEnvironmentVariable="SERVICESTACK_LICENSE=${{ secrets.SERVICESTACK_LICENSE }}"
117+
dotnet publish --os linux --arch x64 -c Release \
118+
-p:ContainerRepository=${{ env.image_repository_name }} \
119+
-p:ContainerRegistry=ghcr.io -p:ContainerImageTags=latest \
120+
-p:ContainerPort=80 \
121+
-p:ContainerEnvironmentVariable="SERVICESTACK_LICENSE=${{ env.SERVICESTACK_LICENSE }}"

.github/workflows/build.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,26 @@ jobs:
1616
- name: Setup dotnet
1717
uses: actions/setup-dotnet@v5
1818
with:
19-
dotnet-version: 8.0.x
19+
dotnet-version: 10.0.x
20+
21+
- name: Restore NuGet packages (use repo NuGet.config)
22+
run: dotnet restore MyApp.slnx --configfile ./NuGet.Config
23+
24+
# If your feed requires authentication, enable and configure the step below.
25+
# This example uses a Personal Access Token stored in secrets.NUGET_API_KEY.
26+
# Alternatively, you can use the NuGet Authenticate action from Azure Pipelines.
27+
# - name: Authenticate private NuGet feed
28+
# env:
29+
# NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
30+
# run: |
31+
# dotnet nuget add source "https://YOUR_FEED_URL/index.json" \
32+
# --name "PrivateFeed" \
33+
# --username "YOUR_USERNAME" \
34+
# --password "$NUGET_API_KEY" \
35+
# --store-password-in-clear-text
2036

2137
- name: build
22-
run: dotnet build
38+
run: dotnet build --no-restore
2339
working-directory: .
2440

2541
- name: test

.github/workflows/release.yml

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ on:
1414

1515
env:
1616
DOCKER_BUILDKIT: 1
17-
KAMAL_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
17+
SERVICESTACK_LICENSE: ${{ secrets.SERVICESTACK_LICENSE }}
18+
KAMAL_DEPLOY_IP: ${{ secrets.KAMAL_DEPLOY_IP }}
19+
KAMAL_DEPLOY_HOST: ${{ secrets.KAMAL_DEPLOY_HOST }}
1820
KAMAL_REGISTRY_USERNAME: ${{ github.actor }}
21+
KAMAL_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
1922

2023
jobs:
2124
release:
@@ -37,24 +40,12 @@ jobs:
3740
echo "HAS_MIGRATIONS=false" >> $GITHUB_ENV
3841
fi
3942
40-
# This step is for the deployment of the templates only, safe to delete
41-
- name: Modify deploy.yml
42-
env:
43-
KAMAL_DEPLOY_IP: ${{ secrets.KAMAL_DEPLOY_IP }}
44-
if: env.KAMAL_DEPLOY_IP != null
45-
run: |
46-
sed -i "s/service: my-app/service: ${{ env.repository_name_lower }}/g" config/deploy.yml
47-
sed -i "s#image: my-user/myapp#image: ${{ env.image_repository_name }}#g" config/deploy.yml
48-
sed -i "s/- 192.168.0.1/- ${{ secrets.KAMAL_DEPLOY_IP }}/g" config/deploy.yml
49-
sed -i "s/host: my-app.example.com/host: ${{ secrets.KAMAL_DEPLOY_HOST }}/g" config/deploy.yml
50-
sed -i "s/MyApp/${{ env.repository_name }}/g" config/deploy.yml
51-
5243
- name: Login to GitHub Container Registry
5344
uses: docker/login-action@v3
5445
with:
5546
registry: ghcr.io
56-
username: ${{ env.KAMAL_REGISTRY_USERNAME }}
57-
password: ${{ env.KAMAL_REGISTRY_PASSWORD }}
47+
username: ${{ github.actor }}
48+
password: ${{ secrets.GITHUB_TOKEN }}
5849

5950
- name: Set up SSH key
6051
uses: webfactory/[email protected]
@@ -79,29 +70,47 @@ jobs:
7970
run: |
8071
kamal server bootstrap
8172
73+
- name: Ensure directories exist with correct permissions
74+
run: |
75+
echo "Creating directories with correct permissions"
76+
kamal server exec "mkdir -p /opt/docker/${{ env.repository_name }}/App_Data /opt/docker/${{ env.repository_name }}/initdb.d"
77+
78+
echo "Setting app file permissions"
79+
kamal server exec "chown -R 1654:1654 /opt/docker/${{ env.repository_name }}/App_Data /opt/docker/${{ env.repository_name }}/initdb.d"
80+
8281
- name: Check if first run and execute kamal app boot if necessary
8382
run: |
84-
FIRST_RUN_FILE=".${{ env.repository_name }}"
85-
if ! kamal server exec --no-interactive -q "test -f $FIRST_RUN_FILE"; then
86-
kamal server exec --no-interactive -q "touch $FIRST_RUN_FILE" || true
83+
FIRST_RUN_FILE="~/first-run/${{ env.repository_name }}"
84+
if ! kamal server exec -q "test -f $FIRST_RUN_FILE"; then
85+
kamal server exec -q "mkdir -p ~/first-run && touch $FIRST_RUN_FILE" || true
86+
87+
if [ -n "${{env.INIT_DB_SQL}}" ]; then
88+
echo "Initializing DB with INIT_DB_SQL secret..."
89+
# Save the SQL content to a temporary file
90+
echo "${{ env.INIT_DB_SQL }}" > init-db.sql
91+
cat init-db.sql | kamal server exec -i "cat > /opt/docker/${{ env.repository_name }}/initdb.d/${{ env.repository_name }}.sql" && rm init-db.sql || true
92+
fi
93+
# Start all kamal accessories
94+
kamal accessory boot all || true
95+
96+
# Deploy latest version
8797
kamal deploy -q -P --version latest || true
8898
else
8999
echo "Not first run, skipping kamal app boot"
90-
fi
100+
fi
91101
92-
- name: Ensure file permissions
102+
- name: Verify file permissions before deploy
93103
run: |
94-
kamal server exec --no-interactive "mkdir -p /opt/docker/${{ env.repository_name }}/App_Data && chown -R 1654:1654 /opt/docker/${{ env.repository_name }}"
104+
kamal server exec --no-interactive "chown -R 1654:1654 /opt/docker/${{ env.repository_name }}/App_Data /opt/docker/${{ env.repository_name }}/initdb.d"
105+
106+
- name: Deploy with Kamal
107+
run: |
108+
kamal lock release -v
109+
kamal server exec --no-interactive 'echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin'
110+
kamal server exec --no-interactive 'docker pull ghcr.io/${{ env.image_repository_name }}:latest'
111+
kamal deploy -P --version latest
95112
96113
- name: Migration
97114
if: env.HAS_MIGRATIONS == 'true'
98115
run: |
99-
kamal server exec --no-interactive 'echo "${{ env.KAMAL_REGISTRY_PASSWORD }}" | docker login ghcr.io -u ${{ env.KAMAL_REGISTRY_USERNAME }} --password-stdin'
100-
kamal server exec --no-interactive "docker pull ghcr.io/${{ env.image_repository_name }}:latest || true"
101116
kamal app exec --no-reuse --no-interactive --version=latest "--AppTasks=migrate"
102-
103-
- name: Deploy with Kamal
104-
run: |
105-
kamal lock release -v
106-
kamal server exec --no-interactive 'echo "${{ env.KAMAL_REGISTRY_PASSWORD }}" | docker login ghcr.io -u ${{ env.KAMAL_REGISTRY_USERNAME }} --password-stdin'
107-
kamal deploy -P --version latest

0 commit comments

Comments
 (0)