ci_cd #42
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: ci_cd | |
on: | |
release: | |
types: [created] | |
env: | |
SSH_PRIVATE_KEY: ${{secrets.SSH_PRIVATE_KEY}} | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Build & Push Image | |
run: | | |
docker login --username=${{secrets.DOCKER_USERNAME}} --password=${{secrets.DOCKER_PASSWORD}} ${{secrets.DOCKER_REGISTRY}} | |
docker build -t ${{secrets.DOCKER_REGISTRY}}/${{secrets.DOCKER_NAMESPACE}}/${{secrets.DOCKER_REPOSITORY}}:${{github.ref_name}} . | |
docker push ${{secrets.DOCKER_REGISTRY}}/${{secrets.DOCKER_NAMESPACE}}/${{secrets.DOCKER_REPOSITORY}}:${{github.ref_name}} | |
deploy: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: SSH Auth && Deploy Image | |
run: | | |
ssh -V | |
mkdir -p ~/.ssh | |
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts | |
chmod 644 ~/.ssh/known_hosts | |
cat >>~/.ssh/config <<END | |
Host remote | |
HostName ${{secrets.SSH_HOST}} | |
User ${{secrets.SSH_USERNAME}} | |
IdentityFile ~/.ssh/id_rsa | |
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}}/${{secrets.DOCKER_REPOSITORY}}:${{github.ref_name}} | |
docker stop ${{secrets.DOCKER_CONTAINER_NAME}} | |
docker rm ${{secrets.DOCKER_CONTAINER_NAME}} | |
docker run -dp ${{secrets.HOST_PORT}}:${{secrets.CONTAINER_PORT}} --name ${{secrets.DOCKER_CONTAINER_NAME}} ${{secrets.DOCKER_NAMESPACE}}/${{secrets.DOCKER_REPOSITORY}}:${{github.ref_name}} | |
END | |
ssh remote < ~/remote.sh |