|
| 1 | +# This workflow will build and publish a Docker container which is then deployed through SSH Deploy. |
| 2 | +# |
| 3 | +# The build job in this workflow currently assumes that there is a Dockerfile that generates the relevant application image. |
| 4 | +# |
| 5 | +# 1. Decide where you are going to host your image. |
| 6 | +# This template uses the GitHub Registry for simplicity but if required you can update the relevant DOCKER_REGISTRY variables below. |
| 7 | + |
| 8 | + |
| 9 | +name: 'Build and Deploy to green' |
| 10 | + |
| 11 | +on: |
| 12 | + push: |
| 13 | + branches: |
| 14 | + - '"master"' |
| 15 | + |
| 16 | +jobs: |
| 17 | + build: |
| 18 | + name: Build |
| 19 | + runs-on: ubuntu-latest |
| 20 | + strategy: |
| 21 | + fail-fast: false # tells github to not run further steps if this one fails |
| 22 | + permissions: |
| 23 | + packages: write |
| 24 | + contents: read |
| 25 | + env: |
| 26 | + DOCKER_REGISTRY: ghcr.io # TODO: Update to your docker registry uri |
| 27 | + DOCKER_REGISTRY_USERNAME: ${{ github.actor }} # TODO: Update to your docker registry username |
| 28 | + DOCKER_REGISTRY_PASSWORD: ${{ secrets.GITHUB_TOKEN }} # TODO: Update to your docker registry password |
| 29 | + outputs: |
| 30 | + image_tag: ${{ steps.meta.outputs.version }} |
| 31 | + steps: |
| 32 | + - uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - name: Set up Docker Buildx |
| 35 | + uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 |
| 36 | + |
| 37 | + - name: Log in to the Container registry |
| 38 | + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 |
| 39 | + with: |
| 40 | + registry: ${{ env.DOCKER_REGISTRY }} |
| 41 | + username: ${{ env.DOCKER_REGISTRY_USERNAME }} |
| 42 | + password: ${{ env.DOCKER_REGISTRY_PASSWORD }} |
| 43 | + |
| 44 | + - name: Extract metadata (tags, labels) for Docker |
| 45 | + id: meta |
| 46 | + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 |
| 47 | + with: |
| 48 | + images: ${{ env.DOCKER_REGISTRY }}/${{ github.repository }} |
| 49 | + tags: type=semver,pattern={{version}},value=v1.0.0-{{sha}} |
| 50 | + |
| 51 | + - name: Build and push Docker image |
| 52 | + id: push |
| 53 | + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 |
| 54 | + with: |
| 55 | + context: . |
| 56 | + push: true |
| 57 | + tags: ${{ steps.meta.outputs.tags }} |
| 58 | + labels: ${{ steps.meta.outputs.labels }} |
| 59 | + deploy: |
| 60 | + name: Deploy |
| 61 | + permissions: |
| 62 | + id-token: write |
| 63 | + runs-on: ubuntu-latest |
| 64 | + needs: [ build ] |
| 65 | + if: |
| 66 | + github.ref == 'refs/heads/master' # we tell Github to only execute this step if we're on our master branch (so we don't put unfinished branches in production) |
| 67 | + env: |
| 68 | + SSH_HOST: ${{ secrets.SSH_HOST }} |
| 69 | + SSH_JUMPHOST: ${{ secrets.SSH_PROXY_HOST }} |
| 70 | + SSH_KEY: ${{ secrets.SSH_KEY }} |
| 71 | + SSH_PORT: ${{ secrets.SSH_PORT }} |
| 72 | + SSH_USER: ${{ secrets.SSH_USER }} |
| 73 | + |
| 74 | + steps: |
| 75 | + - name: Deploying to Digitalocean droplet |
| 76 | + uses: appleboy/ssh-action@master # An action made to control Linux servers |
| 77 | + with: # We set all our secrets here for the action, these won't be shown in the action logs |
| 78 | + host: ${{ env.SSH_HOST }} |
| 79 | + username: ${{ env.SSH_USERNAME }} |
| 80 | + key: ${{ env.SSH_KEY }} |
| 81 | + # password: ${{ secrets.PASSWORD }} |
| 82 | + port: ${{ env.SSH_PORT }} |
| 83 | + script: | |
| 84 | + cd ~/hackathon # we move into our app's folder |
| 85 | + git pull # we pull any changes from git |
| 86 | + ./run.sh |
0 commit comments