forked from thixpin/ansible-cicd
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.github/workflows | ||
/devops | ||
/node_modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
name: Ansible CI/CD with Docker | ||
|
||
env: | ||
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | ||
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | ||
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} | ||
|
||
SSH_USER: ${{ vars.SSH_USER }} | ||
TARGET_HOST: ${{ vars.TARGET_HOST }} | ||
IMAGE_NAME: ${{ vars.IMAGE_NAME }} | ||
DOMAIN_NAME: ${{ vars.DOMAIN_NAME }} | ||
CONTAINER_NAME: ${{ vars.CONTAINER_NAME }} | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Login to Docker Hub with token | ||
run: | | ||
echo $DOCKERHUB_TOKEN | docker login -u $DOCKERHUB_USERNAME --password-stdin | ||
- name: Build Docker image | ||
run: docker build -t $IMAGE_NAME:latest . | ||
|
||
- name: Push Docker image | ||
run: docker push $IMAGE_NAME:latest | ||
|
||
|
||
deploy: | ||
needs: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Create SSH key | ||
run: | | ||
echo "$SSH_PRIVATE_KEY" > private.key | ||
sudo chmod 400 private.key | ||
eval `ssh-agent -s` | ||
ssh-add private.key | ||
mkdir -p ~/.ssh | ||
touch ~/.ssh/known_hosts | ||
chmod 644 ~/.ssh/known_hosts | ||
ssh-keyscan -H $TARGET_HOST >> ~/.ssh/known_hosts | ||
- name: Set up Ansible | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install -y ansible | ||
- name: Creae inventory file | ||
run: | | ||
echo "server ansible_host=$TARGET_HOST ansible_user=$SSH_USER ansible_ssh_private_key_file=private.key" > inventory | ||
- name: Deploy with Ansible | ||
run: | | ||
ansible-playbook -i inventory \ | ||
--extra-vars "image_name=$IMAGE_NAME" \ | ||
--extra-vars "domain_name=$DOMAIN_NAME" \ | ||
--extra-vars "container_name=$CONTAINER_NAME" \ | ||
--extra-vars "dockerhub_username=$DOCKERHUB_USERNAME" \ | ||
--extra-vars "dockerhub_token=$DOCKERHUB_TOKEN" \ | ||
--extra-vars "ansible_ssh_common_args='-o StrictHostKeyChecking=no'" \ | ||
devops/playbook.yml |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
FROM node:20-alpine | ||
|
||
LABEL maintainer="Soe Thura <[email protected]>" | ||
LABEL description="Docker image for Next.js app" | ||
|
||
# Set working directory | ||
WORKDIR /app | ||
|
||
# Copy package.json and package-lock.json | ||
COPY package*.json ./ | ||
|
||
# Install dependencies | ||
RUN npm install | ||
|
||
# Copy the rest of the application code excluding the files in .dockerignore | ||
COPY . . | ||
|
||
# Build the Next.js app | ||
RUN npm run build | ||
|
||
# Expose port 3000 | ||
EXPOSE 3000 | ||
|
||
# Command to run the Next.js app | ||
CMD ["npm", "start"] | ||
|
||
# Build the Docker image | ||
# docker build -t nextjs-app . | ||
# docker run -p 3000:3000 -d --name nextjs-app nextjs-app |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
--- | ||
- name: Deploy Next.js Docker container | ||
hosts: all | ||
become: yes | ||
vars: | ||
IMAGE_NAME: "{{ image_name }}" | ||
DOCKERHUB_USERNAME: "{{ dockerhub_username }}" | ||
DOCKERHUB_TOKEN: "{{ dockerhub_token }}" | ||
DOMAIN_NAME: "{{ domain_name }}" | ||
CONTAINER_NAME: "{{ container_name }}" | ||
|
||
tasks: | ||
- name: Run Apt update | ||
apt: | ||
update_cache: yes | ||
|
||
- name: Install Docker | ||
apt: | ||
name: docker.io | ||
state: present | ||
|
||
- name: Start Docker service | ||
service: | ||
name: docker | ||
state: started | ||
|
||
- name: Install Nginx | ||
apt: | ||
name: nginx | ||
state: present | ||
|
||
- name: Start Nginx service | ||
service: | ||
name: nginx | ||
state: started | ||
|
||
- name: Login to Docker Hub with token | ||
docker_login: | ||
username: "{{ DOCKERHUB_USERNAME }}" | ||
password: "{{ DOCKERHUB_TOKEN }}" | ||
|
||
- name: Pull Docker image | ||
docker_image: | ||
name: "{{ IMAGE_NAME }}" | ||
source: pull | ||
|
||
- name: Run Docker container | ||
docker_container: | ||
name: "{{ CONTAINER_NAME }}" | ||
image: "{{ IMAGE_NAME }}" | ||
state: started | ||
pull: true | ||
restart_policy: always | ||
ports: | ||
- "3001:3000" | ||
|
||
- name: Wait for container to start | ||
wait_for: | ||
host: localhost | ||
port: 3001 | ||
delay: 5 | ||
timeout: 60 | ||
|
||
- name: Check if Nginx config file exists | ||
stat: | ||
path: "/etc/nginx/sites-available/{{ DOMAIN_NAME }}" | ||
register: nginx_config | ||
|
||
- name: Configure Nginx | ||
template: | ||
src: nginx.conf.j2 | ||
dest: "/etc/nginx/sites-available/{{ DOMAIN_NAME }}" | ||
when: not nginx_config.stat.exists | ||
|
||
- name: Enable Nginx site | ||
file: | ||
src: "/etc/nginx/sites-available/{{ DOMAIN_NAME }}" | ||
dest: "/etc/nginx/sites-enabled/{{ DOMAIN_NAME }}" | ||
state: link | ||
when: not nginx_config.stat.exists | ||
notify: Reload Nginx | ||
|
||
handlers: | ||
- name: Reload Nginx | ||
service: | ||
name: nginx | ||
state: reloaded |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
server { | ||
|
||
listen 80; | ||
server_name {{ DOMAIN_NAME }}; | ||
|
||
# Proxy requests to the Next.js server | ||
location / { | ||
# Pass requests to the Next.js server running on localhost:3000 | ||
proxy_pass http://localhost:3001; | ||
|
||
# Proxy settings to handle WebSockets and HTTP/1.1 | ||
proxy_http_version 1.1; | ||
proxy_set_header Upgrade $http_upgrade; | ||
proxy_set_header Connection 'upgrade'; | ||
proxy_set_header Host $host; | ||
|
||
# Additional proxy headers | ||
proxy_set_header X-Real-IP $remote_addr; | ||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
proxy_set_header X-Forwarded-Proto $scheme; | ||
|
||
# Error handling and redirects | ||
error_page 500 502 503 504 /50x.html; | ||
location = /50x.html { | ||
root /usr/share/nginx/html; | ||
} | ||
} | ||
} |