Skip to content

chore: version packages #5

chore: version packages

chore: version packages #5

Workflow file for this run

name: express_server_cicd
on:
push:
tags:
- 'express-server@*'
env:
SSH_PRIVATE_KEY: ${{secrets.SSH_PRIVATE_KEY}}
jobs:
extract:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.extract_version.outputs.version }}
steps:
- id: extract_version
run: |
echo "version=$(echo ${{ github.ref_name }} | sed 's/express-server@//')" >> $GITHUB_OUTPUT
build:
needs: extract
runs-on: ubuntu-latest
env:
IMAGE_VERSION: ${{ needs.extract.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Build & Push Image
run: |
echo "${{env.IMAGE_VERSION}}"
docker login --username=${{secrets.DOCKER_USERNAME}} --password=${{secrets.DOCKER_PASSWORD}} ${{secrets.DOCKER_REGISTRY}}
docker build --target express-backend -t ${{secrets.DOCKER_REGISTRY}}/${{secrets.DOCKER_NAMESPACE}}/fullstack-blog-express:${{env.IMAGE_VERSION}} .
docker push ${{secrets.DOCKER_REGISTRY}}/${{secrets.DOCKER_NAMESPACE}}/fullstack-blog-express:${{env.IMAGE_VERSION}}
deploy:
needs: [extract, build]
runs-on: ubuntu-latest
env:
IMAGE_VERSION: ${{ needs.extract.outputs.version }}
steps:
- name: SSH Auth && Deploy Image
run: |
ssh -V
mkdir -p ~/.ssh
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
cat >>~/.ssh/config <<END
Host remote
HostName ${{secrets.SSH_HOST}}
Port 22
User ${{secrets.SSH_USERNAME}}
IdentityFile ~/.ssh/id_rsa
StrictHostKeyChecking no
END
cat >>~/remote.sh <<END
docker ps
docker login --username=${{secrets.DOCKER_USERNAME}} --password=${{secrets.DOCKER_PASSWORD}} ${{secrets.DOCKER_REGISTRY}}
docker pull ${{secrets.DOCKER_REGISTRY}}/${{secrets.DOCKER_NAMESPACE}}/fullstack-blog-express:${{env.IMAGE_VERSION}}
cd ${{secrets.PROJECT_DIR}}
sed -i 's/^EXPRESS_SERVER_VERSION=.*/EXPRESS_SERVER_VERSION=${{env.IMAGE_VERSION}}/' .env.docker.local
docker compose --env-file .env.docker.local up -d
echo "Done"
END
ssh remote < ~/remote.sh