Fix commit hash #41
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: Deploy to Server | |
on: | |
push: | |
branches: | |
- deploytest | |
permissions: | |
packages: write | |
jobs: | |
commit-hash: | |
runs-on: ubuntu-latest | |
outputs: | |
commit_hash: ${{ steps.get_commit.outputs.commit_hash }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Get commit hash | |
id: get_commit | |
run: echo "::set-output name=commit_hash::$(git rev-parse HEAD)" | |
build-and-test: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup Go | |
uses: actions/setup-go@v5 | |
with: | |
go-version: '1.22.x' | |
- name: Install dependencies | |
run: go get . | |
- name: Build | |
run: go build -v ./... | |
- name: Test with the Go CLI | |
run: go test | |
build-and-push-image: | |
needs: | |
- build-and-test | |
- commit-hash | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Set short git commit SHA | |
id: vars | |
run: | | |
calculatedSha=$(git rev-parse --short ${{ github.sha }}) | |
echo "COMMIT_SHORT_SHA=$calculatedSha" >> $GITHUB_ENV | |
- name: Log in to the Container registry | |
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 | |
with: | |
registry: https://ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc | |
with: | |
context: . | |
push: true | |
tags: ghcr.io/dreamsofcode-io/guestbook:${{ needs.commit-hash.outputs.commit_hash }} | |
deploy: | |
runs-on: ubuntu-latest | |
needs: | |
- build-and-push-image | |
- commit-hash | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Setup SSH | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }} | |
- name: Add host | |
run: | | |
ssh-keyscan zenful.cloud > ~/.ssh/known_hosts | |
- name: Set up Docker Compose | |
run: | | |
# Install Docker Compose | |
curl -L "https://github.com/docker/compose/releases/download/v2.19.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose | |
chmod +x /usr/local/bin/docker-compose | |
- name: Copy over password | |
run: | | |
ssh [email protected] "echo ${{ secrets.DB_PASSWORD }} > /home/deploytest/db-password.txt" | |
- name: Run Docker Compose | |
run: | | |
export GIT_COMMIT_HASH=${{ needs.commit-hash.outputs.commit_hash }} | |
export DB_PASSWORD_PATH="/home/deploytest/db-password.txt" | |
export DOCKER_HOST=ssh://[email protected] | |
docker-compose -f ./compose.prod.yaml up -d |