|
| 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 | + workflow_dispatch: |
| 11 | + |
| 12 | +env: |
| 13 | + DOCKER_BUILDKIT: 1 |
| 14 | + KAMAL_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }} |
| 15 | + KAMAL_REGISTRY_USERNAME: ${{ github.actor }} |
| 16 | + |
| 17 | +jobs: |
| 18 | + build-container: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 21 | + steps: |
| 22 | + - name: Checkout code |
| 23 | + uses: actions/checkout@v3 |
| 24 | + |
| 25 | + - name: Set up environment variables |
| 26 | + run: | |
| 27 | + echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
| 28 | + echo "repository_name=$(echo ${{ github.repository }} | cut -d '/' -f 2)" >> $GITHUB_ENV |
| 29 | + echo "repository_name_lower=$(echo ${{ github.repository }} | cut -d '/' -f 2 | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
| 30 | + echo "org_name=$(echo ${{ github.repository }} | cut -d '/' -f 1)" >> $GITHUB_ENV |
| 31 | + if [ -n "${{ secrets.APPSETTINGS_PATCH }}" ]; then |
| 32 | + echo "HAS_APPSETTINGS_PATCH=true" >> $GITHUB_ENV |
| 33 | + else |
| 34 | + echo "HAS_APPSETTINGS_PATCH=false" >> $GITHUB_ENV |
| 35 | + fi |
| 36 | + if [ -n "${{ secrets.KAMAL_DEPLOY_IP }}" ]; then |
| 37 | + echo "HAS_DEPLOY_ACTION=true" >> $GITHUB_ENV |
| 38 | + else |
| 39 | + echo "HAS_DEPLOY_ACTION=false" >> $GITHUB_ENV |
| 40 | + fi |
| 41 | +
|
| 42 | + # This step is for the deployment of the templates only, safe to delete |
| 43 | + - name: Modify csproj for template deploy |
| 44 | + if: env.HAS_DEPLOY_ACTION == 'true' |
| 45 | + run: | |
| 46 | + sed -i 's#<ContainerLabel Include="service" Value="my-app" />#<ContainerLabel Include="service" Value="${{ env.repository_name_lower }}" />#g' MyApp/MyApp.csproj |
| 47 | +
|
| 48 | + - name: Check for Client directory |
| 49 | + id: check_client |
| 50 | + run: | |
| 51 | + if [ -d "MyApp.Client" ]; then |
| 52 | + echo "client_exists=true" >> $GITHUB_OUTPUT |
| 53 | + else |
| 54 | + echo "client_exists=false" >> $GITHUB_OUTPUT |
| 55 | + fi |
| 56 | +
|
| 57 | + - name: Setup Node.js |
| 58 | + if: steps.check_client.outputs.client_exists == 'true' |
| 59 | + uses: actions/setup-node@v3 |
| 60 | + with: |
| 61 | + node-version: 22 |
| 62 | + |
| 63 | + - name: Install npm dependencies |
| 64 | + if: steps.check_client.outputs.client_exists == 'true' |
| 65 | + working-directory: ./MyApp.Client |
| 66 | + run: npm install |
| 67 | + |
| 68 | + - name: Install x tool |
| 69 | + run: dotnet tool install -g x |
| 70 | + |
| 71 | + - name: Apply Production AppSettings |
| 72 | + if: env.HAS_APPSETTINGS_PATCH == 'true' |
| 73 | + working-directory: ./MyApp |
| 74 | + run: | |
| 75 | + cat <<EOF >> appsettings.json.patch |
| 76 | + ${{ secrets.APPSETTINGS_PATCH }} |
| 77 | + EOF |
| 78 | + x patch appsettings.json.patch |
| 79 | +
|
| 80 | + - name: Login to GitHub Container Registry |
| 81 | + uses: docker/login-action@v3 |
| 82 | + with: |
| 83 | + registry: ghcr.io |
| 84 | + username: ${{ env.KAMAL_REGISTRY_USERNAME }} |
| 85 | + password: ${{ env.KAMAL_REGISTRY_PASSWORD }} |
| 86 | + |
| 87 | + - name: Setup .NET |
| 88 | + uses: actions/setup-dotnet@v3 |
| 89 | + with: |
| 90 | + dotnet-version: '8.0' |
| 91 | + |
| 92 | + - name: Build and push Docker image |
| 93 | + 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 |
0 commit comments