Skip to content

Commit 8e8ff3f

Browse files
committed
update GitHub Actions
1 parent ff4f92b commit 8e8ff3f

2 files changed

Lines changed: 116 additions & 34 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Build Container
2+
permissions:
3+
packages: write
4+
contents: write
5+
on:
6+
workflow_run:
7+
workflows: ["Build"]
8+
types:
9+
- completed
10+
branches:
11+
- main
12+
- master
13+
workflow_dispatch:
14+
15+
# Only update envs here if you need to change them for this workflow
16+
env:
17+
DOCKER_BUILDKIT: 1
18+
KAMAL_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
19+
KAMAL_REGISTRY_USERNAME: ${{ github.actor }}
20+
21+
jobs:
22+
build-container:
23+
runs-on: ubuntu-latest
24+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
25+
steps:
26+
- name: Checkout code
27+
uses: actions/checkout@v3
28+
29+
- name: Set up environment variables
30+
run: |
31+
echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
32+
echo "repository_name=$(echo ${{ github.repository }} | cut -d '/' -f 2)" >> $GITHUB_ENV
33+
echo "repository_name_lower=$(echo ${{ github.repository }} | cut -d '/' -f 2 | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
34+
echo "org_name=$(echo ${{ github.repository }} | cut -d '/' -f 1)" >> $GITHUB_ENV
35+
36+
- name: Check for Client directory and package.json
37+
id: check_client
38+
run: |
39+
if [ -d "MyApp.Client" ] && [ -f "MyApp.Client/package.json" ]; then
40+
echo "client_exists=true" >> $GITHUB_OUTPUT
41+
else
42+
echo "client_exists=false" >> $GITHUB_OUTPUT
43+
fi
44+
45+
- name: Setup Node.js
46+
if: steps.check_client.outputs.client_exists == 'true'
47+
uses: actions/setup-node@v3
48+
with:
49+
node-version: 22
50+
51+
- name: Install npm dependencies
52+
if: steps.check_client.outputs.client_exists == 'true'
53+
working-directory: ./MyApp.Client
54+
run: npm install
55+
56+
- name: Install x tool
57+
run: dotnet tool install -g x
58+
59+
- name: Apply Production AppSettings
60+
env:
61+
APPSETTINGS_PATCH: ${{ secrets.APPSETTINGS_PATCH }}
62+
if: env.APPSETTINGS_PATCH != null
63+
working-directory: ./MyApp
64+
run: |
65+
cat <<EOF >> appsettings.json.patch
66+
${{ secrets.APPSETTINGS_PATCH }}
67+
EOF
68+
x patch appsettings.json.patch
69+
70+
- name: Login to GitHub Container Registry
71+
uses: docker/login-action@v3
72+
with:
73+
registry: ghcr.io
74+
username: ${{ env.KAMAL_REGISTRY_USERNAME }}
75+
password: ${{ env.KAMAL_REGISTRY_PASSWORD }}
76+
77+
- name: Setup .NET
78+
uses: actions/setup-dotnet@v3
79+
with:
80+
dotnet-version: '8.0'
81+
82+
- name: Build and push Docker image
83+
run: |
84+
dotnet publish --os linux --arch x64 -c Release -p:ContainerRepository=${{ env.image_repository_name }} -p:ContainerRegistry=ghcr.io -p:ContainerImageTags=latest -p:ContainerPort=80

.github/workflows/release.yml

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
name: Release and Deploy
1+
name: Release
22
permissions:
33
packages: write
44
contents: write
55
on:
66
workflow_run:
7-
workflows: ["Build"]
7+
workflows: ["Build Container"]
88
types:
99
- completed
10+
branches:
11+
- main
12+
- master
1013
workflow_dispatch:
1114

12-
# Only update envs here if you need to change them for this workflow
1315
env:
1416
DOCKER_BUILDKIT: 1
1517
KAMAL_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
1618
KAMAL_REGISTRY_USERNAME: ${{ github.actor }}
1719

18-
# Standard steps for building and deploying a .NET app via Kamal
1920
jobs:
20-
build-and-deploy:
21+
release:
2122
runs-on: ubuntu-latest
23+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
2224
steps:
2325
- name: Checkout code
2426
uses: actions/checkout@v3
@@ -27,45 +29,36 @@ jobs:
2729
run: |
2830
echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
2931
echo "repository_name=$(echo ${{ github.repository }} | cut -d '/' -f 2)" >> $GITHUB_ENV
32+
echo "repository_name_lower=$(echo ${{ github.repository }} | cut -d '/' -f 2 | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV
33+
echo "org_name=$(echo ${{ github.repository }} | cut -d '/' -f 1)" >> $GITHUB_ENV
3034
if find . -maxdepth 2 -type f -name "Configure.Db.Migrations.cs" | grep -q .; then
3135
echo "HAS_MIGRATIONS=true" >> $GITHUB_ENV
3236
else
3337
echo "HAS_MIGRATIONS=false" >> $GITHUB_ENV
3438
fi
35-
if [ -n "${{ secrets.APPSETTINGS_PATCH }}" ]; then
36-
echo "HAS_APPSETTINGS_PATCH=true" >> $GITHUB_ENV
39+
if [ -n "${{ secrets.KAMAL_DEPLOY_IP }}" ]; then
40+
echo "HAS_DEPLOY_ACTION=true" >> $GITHUB_ENV
3741
else
38-
echo "HAS_APPSETTINGS_PATCH=false" >> $GITHUB_ENV
42+
echo "HAS_DEPLOY_ACTION=false" >> $GITHUB_ENV
3943
fi
4044
45+
# This step is for the deployment of the templates only, safe to delete
46+
- name: Modify deploy.yml
47+
if: env.HAS_DEPLOY_ACTION == 'true'
48+
run: |
49+
sed -i "s/service: my-app/service: ${{ env.repository_name_lower }}/g" config/deploy.yml
50+
sed -i "s#image: my-user/myapp#image: ${{ env.image_repository_name }}#g" config/deploy.yml
51+
sed -i "s/- 192.168.0.1/- ${{ secrets.KAMAL_DEPLOY_IP }}/g" config/deploy.yml
52+
sed -i "s/host: my-app.example.com/host: ${{ secrets.KAMAL_DEPLOY_HOST }}/g" config/deploy.yml
53+
sed -i "s/MyApp/${{ env.repository_name }}/g" config/deploy.yml
54+
4155
- name: Login to GitHub Container Registry
4256
uses: docker/login-action@v3
4357
with:
4458
registry: ghcr.io
4559
username: ${{ env.KAMAL_REGISTRY_USERNAME }}
4660
password: ${{ env.KAMAL_REGISTRY_PASSWORD }}
4761

48-
- name: Setup .NET
49-
uses: actions/setup-dotnet@v3
50-
with:
51-
dotnet-version: '8.0'
52-
53-
- name: Install x tool
54-
run: dotnet tool install -g x
55-
56-
- name: Apply Production AppSettings
57-
if: env.HAS_APPSETTINGS_PATCH == 'true'
58-
working-directory: ./BlazorGalleryWasm
59-
run: |
60-
cat <<EOF >> appsettings.json.patch
61-
${{ secrets.APPSETTINGS_PATCH }}
62-
EOF
63-
x patch appsettings.json.patch
64-
65-
- name: Build and push Docker image
66-
run: |
67-
dotnet publish --os linux --arch x64 -c Release -p:ContainerRepository=${{ env.image_repository_name }} -p:ContainerRegistry=ghcr.io -p:ContainerImageTags=latest -p:ContainerPort=80
68-
6962
- name: Set up SSH key
7063
uses: webfactory/ssh-agent@v0.9.0
7164
with:
@@ -78,7 +71,7 @@ jobs:
7871
bundler-cache: true
7972

8073
- name: Install Kamal
81-
run: gem install kamal -v 2.2.2
74+
run: gem install kamal -v 2.3.0
8275

8376
- name: Set up Docker Buildx
8477
uses: docker/setup-buildx-action@v3
@@ -93,17 +86,22 @@ jobs:
9386
FIRST_RUN_FILE=".${{ env.repository_name }}"
9487
if ! kamal server exec --no-interactive -q "test -f $FIRST_RUN_FILE"; then
9588
kamal server exec --no-interactive -q "touch $FIRST_RUN_FILE" || true
96-
kamal deploy -q -P --version latest > /dev/null 2>&1 || true
89+
kamal deploy -q -P --version latest || true
9790
else
9891
echo "Not first run, skipping kamal app boot"
99-
fi
92+
fi
10093
10194
- name: Ensure file permissions
102-
run: kamal server exec --no-interactive "mkdir -p /opt/docker/${{ env.repository_name }}/App_Data && chown -R 1654:1654 /opt/docker/${{ env.repository_name }}"
95+
run: |
96+
kamal server exec --no-interactive "mkdir -p /opt/docker/${{ env.repository_name }}/App_Data"
97+
kamal server exec --no-interactive "chown -R 1654:1654 /opt/docker/${{ env.repository_name }}"
10398
10499
- name: Migration
105100
if: env.HAS_MIGRATIONS == 'true'
106-
run: kamal app exec --no-reuse --no-interactive --version=latest "--AppTasks=migrate"
101+
run: |
102+
kamal server exec --no-interactive 'echo "${{ env.KAMAL_REGISTRY_PASSWORD }}" | docker login ghcr.io -u ${{ env.KAMAL_REGISTRY_USERNAME }} --password-stdin'
103+
kamal server exec --no-interactive "docker pull ghcr.io/${{ env.image_repository_name }}:latest || true"
104+
kamal app exec --no-reuse --no-interactive --version=latest "--AppTasks=migrate"
107105
108106
- name: Deploy with Kamal
109107
run: |

0 commit comments

Comments
 (0)