|
| 1 | +# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created |
| 2 | +# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages |
| 3 | + |
| 4 | +name: Build and Deploy to Github Pages and DockerHub |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: [ "master" ] |
| 9 | + pull_request: |
| 10 | + branches: [ "master" ] |
| 11 | + release: |
| 12 | + types: [published] |
| 13 | + |
| 14 | +jobs: |
| 15 | + build: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + - uses: actions/setup-node@v4 |
| 20 | + with: |
| 21 | + node-version: 20 |
| 22 | + - run: npm ci |
| 23 | + - run: npm run build |
| 24 | + |
| 25 | + publish-github-pages: |
| 26 | + needs: build |
| 27 | + runs-on: ubuntu-latest |
| 28 | + if: startsWith(github.ref, 'refs/tags/') # runs only when release with a new tag |
| 29 | + steps: |
| 30 | + - uses: actions/checkout@v4 |
| 31 | + - uses: actions/setup-node@v4 |
| 32 | + with: |
| 33 | + node-version: 20 |
| 34 | + - run: npm ci |
| 35 | + - run: npm run build |
| 36 | + - run: |
| 37 | + git config user.name "github-actions" |
| 38 | + git config user.email "github-actions@github.com" |
| 39 | + npm run deploy |
| 40 | + |
| 41 | + publish-docker-image: |
| 42 | + needs: [build, publish-github-pages] |
| 43 | + runs-on: ubuntu-latest |
| 44 | + if: startsWith(github.ref, 'refs/tags/') # runs only when release with a new tag |
| 45 | + steps: |
| 46 | + - uses: actions/checkout@v4 |
| 47 | + |
| 48 | + - name: Log in to Docker Hub |
| 49 | + uses: docker/login-action@v3 |
| 50 | + with: |
| 51 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 52 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 53 | + |
| 54 | + - name: Extract release version |
| 55 | + id: get_version |
| 56 | + run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV |
| 57 | + |
| 58 | + - name: Build and push Docker image |
| 59 | + run: | |
| 60 | + docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/portfolio:${{ env.RELEASE_VERSION }} . |
| 61 | + docker tag ${{ secrets.DOCKERHUB_USERNAME }}/portfolio:${{ env.RELEASE_VERSION }} ${{ secrets.DOCKERHUB_USERNAME }}/portfolio:latest |
| 62 | + docker push ${{ secrets.DOCKERHUB_USERNAME }}/portfolio:${{ env.RELEASE_VERSION }} |
| 63 | + docker push ${{ secrets.DOCKERHUB_USERNAME }}/portfolio:latest |
| 64 | +
|
| 65 | + deploy-to-droplet: |
| 66 | + runs-on: ubuntu-latest |
| 67 | + needs: publish-docker-image |
| 68 | + if: startsWith(github.ref, 'refs/tags/') # runs only when release with a new tag |
| 69 | + steps: |
| 70 | + - name: Deploy to Digital Ocean droplet via SSH action |
| 71 | + uses: appleboy/ssh-action@v1.2.2 |
| 72 | + with: |
| 73 | + host: ${{ secrets.DIGITALOCEAN_HOST }} |
| 74 | + username: ${{ secrets.DIGITALOCEAN_USERNAME }} |
| 75 | + key: ${{ secrets.DIGITALOCEAN_SSHKEY }} |
| 76 | + passphrase: ${{ secrets.DIGITALOCEAN_PASSPHRASE }} |
| 77 | + script: | |
| 78 | + docker pull skuill/portfolio:latest |
| 79 | + docker rm -f skuill-portfolio |
| 80 | + docker run -dit --restart=always --network common-skuill-network -p 3000:80 --name skuill-portfolio skuill/portfolio:latest |
| 81 | + |
0 commit comments