@@ -2,14 +2,14 @@ name: Build + Push + Deploy
22
33env :
44 DOCKER_REGISTRY_NAME : ${{ secrets.DOCKER_REGISTRY_NAME }}
5- DOCKER_REGISTRY_IMAGE : ${{ secrets.DOCKER_REGISTRY_NAME }}/codesark/codesark-portfolio-next
5+ DOCKER_REGISTRY_IMAGE : ${{ secrets.DOCKER_REGISTRY_NAME }}/codesark/codesark
66
77on :
88 workflow_dispatch :
99 push :
1010 branches :
1111 - main
12- - dev
12+ # - dev
1313 tags :
1414 - v*
1515
2323 - linux/amd64
2424 # - linux/arm/v6
2525 # - linux/arm/v7
26- - linux/arm64
26+ # - linux/arm64
2727 steps :
2828 - name : Checkout
2929 uses : actions/checkout@v4
5353 registry : ${{ env.DOCKER_REGISTRY_NAME }}
5454 username : ${{ secrets.DOCKER_REGISTRY_USERNAME }}
5555 password : ${{ secrets.DOCKER_REGISTRY_TOKEN }}
56+ - name : Create .env file
57+ run : |
58+ echo "${{ secrets.DEPLOYMENT_ENV_VARS }}" > .env
59+
5660 - name : Build and push by digest
5761 id : build
5862 uses : docker/build-push-action@v5
@@ -63,13 +67,15 @@ jobs:
6367 outputs : type=image,name=${{ env.DOCKER_REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
6468 cache-from : type=gha,scope=${{ github.workflow }}-${{ matrix.platform }}
6569 cache-to : type=gha,mode=max,scope=${{ github.workflow }}-${{ matrix.platform }}
70+ secret-files : |
71+ "env_file=.env"
6672 - name : Export digest
6773 run : |
6874 mkdir -p /tmp/digests
6975 digest="${{ steps.build.outputs.digest }}"
7076 touch "/tmp/digests/${digest#sha256:}"
7177 - name : Upload digest
72- uses : actions/upload-artifact@v3
78+ uses : actions/upload-artifact@v4
7379 with :
7480 name : digests
7581 path : /tmp/digests/*
8692 - build
8793 steps :
8894 - name : Download digests
89- uses : actions/download-artifact@v3
95+ uses : actions/download-artifact@v4
9096 with :
9197 name : digests
9298 path : /tmp/digests
@@ -121,26 +127,75 @@ jobs:
121127 - build
122128 - push
123129 runs-on : ubuntu-latest
124- # only run if branch is main or commit have "release" tag
125- if : ${{ needs.build.outputs.branch == 'main' || contains(github.event.head_commit.message, 'release') }}
130+ if : ${{ needs.build.outputs.branch == 'main' || contains(github.event.head_commit.message, ' release') }}
126131 steps :
127132 - name : SSH Deploy
128133129134 env :
130- IMAGE_WITH_TAG : ' ${{ env.DOCKER_REGISTRY_IMAGE }}:${{ needs.build.outputs.branch }}-${{ needs.build.outputs.sha_short }}'
135+ IMAGE_WITH_TAG : " ${{ env.DOCKER_REGISTRY_IMAGE }}:${{ needs.build.outputs.branch }}-${{ needs.build.outputs.sha_short }}"
136+ DOCKER_REGISTRY_NAME : ${{ secrets.DOCKER_REGISTRY_NAME }}
137+ DOCKER_USERNAME : ${{ secrets.DOCKER_REGISTRY_USERNAME }}
138+ DOCKER_PASSWORD : ${{ secrets.DOCKER_REGISTRY_TOKEN }}
131139 with :
132140 host : ${{ secrets.DEPLOYMENT_HOST }}
133141 username : ${{ secrets.DEPLOYMENT_USER }}
134142 key : ${{ secrets.DEPLOYMENT_KEY }}
135- envs : IMAGE_WITH_TAG
136- # script: |
137- # kubectl set image deployment/codesark-portfolio \
138- # codesark-portfolio=cr.skpd.io/codesark/codesark-portfolio-frontend:$IMAGE_TAG \
139- # -n codesark
143+ envs : IMAGE_WITH_TAG,DOCKER_REGISTRY_NAME,DOCKER_USERNAME,DOCKER_PASSWORD
140144 script : |
141- cd ~/compose-apps/codesark-portfolio && \
142- rm -f compose.yml comopse.yaml && \
143- nextappimage=$IMAGE_WITH_TAG envsubst '$nextappimage' < compose.template.yaml > compose.yaml && \
144- cat compose.yaml && \
145- docker compose pull && \
146- docker compose up -d
145+ # Define the working directory
146+ WORK_DIR=~/stacks/codesark.dev
147+
148+ # Check if the directory exists, and create it if it doesn't
149+ if [ ! -d "$WORK_DIR" ]; then
150+ echo "Directory $WORK_DIR does not exist. Creating it..."
151+ mkdir -p "$WORK_DIR"
152+ fi
153+
154+ # Navigate to the working directory
155+ cd "$WORK_DIR" || { echo "Failed to navigate to $WORK_DIR"; exit 1; }
156+
157+ # Login to Docker registry
158+ echo "Logging in to Docker registry..."
159+ echo "$DOCKER_PASSWORD" | docker login $DOCKER_REGISTRY_NAME -u "$DOCKER_USERNAME" --password-stdin || { echo "Docker login failed"; exit 1; }
160+
161+ # Check if a compose.yaml file exists
162+ if [ -f "compose.yaml" ] || [ -f "compose.yml" ]; then
163+ echo "Existing Docker Compose file detected."
164+
165+ # Stop and remove existing Docker Compose containers if running
166+ if docker compose ps -q &>/dev/null; then
167+ echo "Stopping running containers..."
168+ docker compose down || { echo "Failed to stop running containers"; exit 1; }
169+ fi
170+
171+ # Remove existing compose files
172+ echo "Removing old compose files..."
173+ rm -f compose.yaml compose.yml
174+ fi
175+
176+ # Create .env file from secret
177+ echo "Creating .env file from secret..."
178+ echo "${{ secrets.DEPLOYMENT_ENV_VARS }}" > .env
179+
180+ # Generate the new compose.yaml file
181+ echo "Creating a new compose.yaml file..."
182+ cat <<EOF > compose.yaml
183+ services:
184+ codesark:
185+ image: "${IMAGE_WITH_TAG}"
186+ container_name: codesark
187+ ports:
188+ - "8080:3000"
189+ environment:
190+ - NODE_ENV=production
191+ env_file:
192+ - .env
193+ restart: always
194+ EOF
195+
196+ # Pull the latest image and bring up the new container
197+ echo "Pulling the latest image..."
198+ docker compose pull || { echo "Failed to pull the image"; exit 1; }
199+
200+ echo "Starting the container..."
201+ docker compose up -d || { echo "Failed to start the container"; exit 1; }
0 commit comments