|
11 | 11 |
|
12 | 12 | jobs: |
13 | 13 | publish: |
14 | | - name: Publish Docker Image |
| 14 | + name: Publish Docker Images |
15 | 15 | runs-on: ubuntu-latest |
16 | 16 | environment: ${{ inputs.target_env }} |
17 | 17 | outputs: |
@@ -51,44 +51,104 @@ jobs: |
51 | 51 | exit 1 |
52 | 52 | fi |
53 | 53 |
|
54 | | - - name: Backup existing image |
| 54 | + - name: Detect changed paths |
| 55 | + id: changes |
| 56 | + uses: dorny/paths-filter@v3 |
| 57 | + # todo: find a way to detect changes within same branch, for now do not push to dev/prod in parallel |
| 58 | + with: |
| 59 | + filters: | |
| 60 | + api: |
| 61 | + - 'src/api/**' |
| 62 | + jobs: |
| 63 | + - 'src/jobs/**' |
| 64 | +
|
| 65 | + - name: Backup existing API image |
55 | 66 | # Pulls the current image using the target tag and backup it with a previous tag if it exists. |
| 67 | + if: steps.changes.outputs.api == 'true' |
56 | 68 | run: | |
57 | 69 | OWNER="${{ github.repository_owner }}" |
58 | | - BASE="${{ github.event.repository.name }}" |
| 70 | + BASE="${{ github.event.repository.name }}-api" |
59 | 71 | IMAGE="ghcr.io/$OWNER/$BASE:${{ steps.vars.outputs.TARGET_TAG }}" |
60 | 72 | BACKUP="ghcr.io/$OWNER/$BASE:${{ steps.vars.outputs.BACKUP_TAG }}" |
61 | | - echo "Backing up image if it exists: $IMAGE → $BACKUP" |
| 73 | + echo "Backing up API image if it exists: $IMAGE → $BACKUP" |
62 | 74 | if docker pull "$IMAGE"; then |
63 | 75 | docker tag "$IMAGE" "$BACKUP" |
64 | 76 | docker push "$BACKUP" |
65 | 77 | else |
66 | | - echo "No image to back up." |
| 78 | + echo "No API image to back up." |
67 | 79 | fi |
68 | 80 |
|
69 | | - - name: Cleanup Old Image Digests |
70 | | - # Cleans up old images |
| 81 | + - name: Backup existing Jobs image |
| 82 | + # Pulls the current image using the target tag and backup it with a previous tag if it exists. |
| 83 | + if: steps.changes.outputs.jobs == 'true' |
| 84 | + run: | |
| 85 | + OWNER="${{ github.repository_owner }}" |
| 86 | + BASE="${{ github.event.repository.name }}-jobs" |
| 87 | + IMAGE="ghcr.io/$OWNER/$BASE:${{ steps.vars.outputs.TARGET_TAG }}" |
| 88 | + BACKUP="ghcr.io/$OWNER/$BASE:${{ steps.vars.outputs.BACKUP_TAG }}" |
| 89 | + echo "Backing up Jobs image if it exists: $IMAGE → $BACKUP" |
| 90 | + if docker pull "$IMAGE"; then |
| 91 | + docker tag "$IMAGE" "$BACKUP" |
| 92 | + docker push "$BACKUP" |
| 93 | + else |
| 94 | + echo "No Jobs image to back up." |
| 95 | + fi |
| 96 | +
|
| 97 | + - name: Cleanup Old API Image Digests |
| 98 | + # Cleans up old api images |
| 99 | + if: steps.changes.outputs.api == 'true' |
| 100 | + uses: actions/delete-package-versions@v5 |
| 101 | + continue-on-error: true |
| 102 | + with: |
| 103 | + package-name: ${{ github.event.repository.name }}-api |
| 104 | + package-type: container |
| 105 | + owner: ${{ github.repository_owner }} |
| 106 | + ignore-versions: "^(prod-latest|prod-previous|dev-latest|dev-previous)$" # keeps important tags (latest and previous) |
| 107 | + token: ${{ secrets.MACHINE_PAT }} |
| 108 | + |
| 109 | + - name: Cleanup Old Jobs Image Digests |
| 110 | + # Cleans up old jobs images |
| 111 | + if: steps.changes.outputs.jobs == 'true' |
71 | 112 | uses: actions/delete-package-versions@v5 |
72 | 113 | continue-on-error: true |
73 | 114 | with: |
74 | | - package-name: ${{ github.event.repository.name }} |
| 115 | + package-name: ${{ github.event.repository.name }}-jobs |
75 | 116 | package-type: container |
76 | 117 | owner: ${{ github.repository_owner }} |
77 | | - min-versions-to-keep: 2 # Keeps the 2 most recent versions (latest and previous) |
| 118 | + ignore-versions: "^(prod-latest|prod-previous|dev-latest|dev-previous)$" # keeps important tags (latest and previous) |
78 | 119 | token: ${{ secrets.MACHINE_PAT }} |
79 | 120 |
|
80 | | - - name: Build & push image |
| 121 | + - name: Build & push API image |
| 122 | + # Builds a new Docker image with the target tag and pushes it to GHCR. |
| 123 | + if: steps.changes.outputs.api == 'true' |
| 124 | + run: | |
| 125 | + OWNER=${{ github.repository_owner }} |
| 126 | + BASE=${{ github.event.repository.name }}-api |
| 127 | + TAG=${{ steps.vars.outputs.TARGET_TAG }} |
| 128 | + IMAGE=ghcr.io/$OWNER/$BASE:$TAG |
| 129 | +
|
| 130 | + echo "Building image with tag $IMAGE" |
| 131 | +
|
| 132 | + docker build \ |
| 133 | + --file docker/api/Dockerfile \ |
| 134 | + --tag $IMAGE \ |
| 135 | + . |
| 136 | +
|
| 137 | + docker push $IMAGE |
| 138 | +
|
| 139 | + - name: Build & push Jobs image |
81 | 140 | # Builds a new Docker image with the target tag and pushes it to GHCR. |
| 141 | + if: steps.changes.outputs.jobs == 'true' |
82 | 142 | run: | |
83 | 143 | OWNER=${{ github.repository_owner }} |
84 | | - BASE=${{ github.event.repository.name }} |
| 144 | + BASE=${{ github.event.repository.name }}-jobs |
85 | 145 | TAG=${{ steps.vars.outputs.TARGET_TAG }} |
86 | 146 | IMAGE=ghcr.io/$OWNER/$BASE:$TAG |
87 | 147 |
|
88 | 148 | echo "Building image with tag $IMAGE" |
89 | 149 |
|
90 | 150 | docker build \ |
91 | | - --file docker/Dockerfile \ |
| 151 | + --file docker/jobs/Dockerfile \ |
92 | 152 | --tag $IMAGE \ |
93 | 153 | . |
94 | 154 |
|
@@ -146,15 +206,17 @@ jobs: |
146 | 206 | # Deploys to VPS. |
147 | 207 | run: | |
148 | 208 | OWNER="${{ vars.GHCR_OWNER }}" |
149 | | - APPLICATION_IMAGE="ghcr.io/$OWNER/${{ github.event.repository.name }}:${{ needs.publish.outputs.target_tag }}" |
| 209 | + APPLICATION_API_IMAGE="ghcr.io/$OWNER/${{ github.event.repository.name }}-api:${{ needs.publish.outputs.target_tag }}" |
| 210 | + APPLICATION_JOBS_IMAGE="ghcr.io/$OWNER/${{ github.event.repository.name }}-jobs:${{ needs.publish.outputs.target_tag }}" |
150 | 211 | echo "Deploying to VPS..." |
151 | 212 | ssh -o StrictHostKeyChecking=no ${{ secrets.DEPLOYMENT_SSH_USER }}@${{ secrets.DEPLOYMENT_SERVER }} "\ |
152 | 213 |
|
153 | 214 | # exports general variables |
154 | 215 | export PROJECT_NAME='${{ vars.PROJECT_NAME }}' && \ |
155 | 216 | export GHCR_USER='${{ secrets.MACHINE_USER }}' && \ |
156 | 217 | export GHCR_PAT='${{ secrets.MACHINE_PAT }}' && \ |
157 | | - export APPLICATION_IMAGE='$APPLICATION_IMAGE' && \ |
| 218 | + export APPLICATION_API_IMAGE='$APPLICATION_API_IMAGE' && \ |
| 219 | + export APPLICATION_JOBS_IMAGE='$APPLICATION_JOBS_IMAGE' && \ |
158 | 220 |
|
159 | 221 | # applies only to production for logging |
160 | 222 | export HONEYCOMB_API_KEY='${{ secrets.HONEYCOMB_API_KEY }}' && \ |
|
0 commit comments