|
| 1 | +name: Deploy Node.js App with Ansible |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +jobs: |
| 9 | + deploy: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + environment: dev |
| 12 | + |
| 13 | + env: |
| 14 | + IMAGE_NAME: docker.io/therealpad/my-node-app |
| 15 | + CONTAINER_NAME: my-node-app |
| 16 | + PORT: 3000 |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout repository |
| 20 | + uses: actions/checkout@v4 |
| 21 | + |
| 22 | + - name: Log in to Docker Hub |
| 23 | + uses: docker/login-action@v2 |
| 24 | + with: |
| 25 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 26 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 27 | + |
| 28 | + - name: Build Docker image |
| 29 | + run: | |
| 30 | + docker build -t $IMAGE_NAME:latest ./server |
| 31 | +
|
| 32 | + - name: Push Docker image |
| 33 | + run: | |
| 34 | + docker push $IMAGE_NAME:latest |
| 35 | +
|
| 36 | + - name: Set up Python |
| 37 | + uses: actions/setup-python@v4 |
| 38 | + with: |
| 39 | + python-version: "3.x" |
| 40 | + |
| 41 | + - name: Install Ansible |
| 42 | + run: | |
| 43 | + python -m pip install --upgrade pip |
| 44 | + pip install ansible |
| 45 | +
|
| 46 | + - name: Set up SSH |
| 47 | + uses: webfactory/ssh-agent@v0.9.0 |
| 48 | + with: |
| 49 | + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 50 | + |
| 51 | + - name: Add target host to known_hosts |
| 52 | + run: | |
| 53 | + HOST_IP=$(grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' inventory.ini | head -n 1) |
| 54 | + ssh-keyscan $HOST_IP >> ~/.ssh/known_hosts |
| 55 | +
|
| 56 | + - name: Run Ansible Playbook |
| 57 | + run: | |
| 58 | + ansible-playbook -i inventory.ini setup.yml \ |
| 59 | + --extra-vars "node_username=${{ secrets.NODE_USERNAME }} \ |
| 60 | + node_password=${{ secrets.NODE_PASSWORD }} \ |
| 61 | + secret_message='${{ secrets.SECRET_MESSAGE }}'" |
| 62 | + env: |
| 63 | + ANSIBLE_HOST_KEY_CHECKING: "False" |
0 commit comments