Last active
November 12, 2024 06:57
-
-
Save abu-raihan-ddclbd/c9f68b562fa1b3b8e4c81d3234bb7d2a to your computer and use it in GitHub Desktop.
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: Build, Push, and Deploy Docker Image | |
on: | |
push: | |
branches: | |
- staging | |
jobs: | |
build-push-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Add Registry to /etc/hosts | |
run: | | |
echo "${{ secrets.HOSTS_ENTRY }}" | sudo tee -a /etc/hosts | |
- name: Configure Docker for Insecure Registry | |
run: | | |
sudo mkdir -p /etc/docker | |
echo '{ "insecure-registries": ["https://harbor.example.com"] }' | sudo tee /etc/docker/daemon.json | |
sudo systemctl restart docker | |
- name: Log in to Docker Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: harbor.example.com | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Generate Date-Time-Based Tag | |
id: generate_tag | |
run: | | |
new_version=$(date -u +"v%Y.%m.%d.%H%M%S") | |
echo "Generated tag: $new_version" | |
echo "version=$new_version" >> $GITHUB_ENV | |
- name: Configure Git for SSH | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.SSH_PRIVATE_KEY_2 }}" > ~/.ssh/id_ed25519 | |
chmod 600 ~/.ssh/id_ed25519 | |
ssh-keyscan github.com >> ~/.ssh/known_hosts || true | |
git config --global user.name "abu-raihan-ddclbd" | |
git config --global user.email "abu.raihan.ddclbd@gmail.com" | |
echo "IdentityFile ~/.ssh/id_ed25519" >> ~/.ssh/config | |
chmod 600 ~/.ssh/config | |
- name: Change Remote to SSH and Push | |
run: | | |
git remote set-url origin git@github.com:${{ github.repository }}.git | |
git tag -a "${{ env.version }}" -m "Release ${GITHUB_REF} at ${GITHUB_SHA}" | |
git push origin "${{ env.version }}" | |
- name: Build Docker Image | |
run: | | |
# Extract the repository name from the github.repository context | |
PROJECT_NAME=$(echo "${GITHUB_REPOSITORY}" | cut -d'/' -f2) | |
echo "Project Name: $PROJECT_NAME" | |
docker build -f .docker/Dockerfile \ | |
-t harbor.example.com/${{ vars.PROJECT_NAME }}/$PROJECT_NAME:${{ env.version }} -t harbor.example.com/${{ vars.PROJECT_NAME }}/$PROJECT_NAME:latest \ | |
. | |
env: | |
GITHUB_REPOSITORY: ${{ github.repository }} | |
- name: Push Docker Image | |
run: | | |
# Extract the repository name from the github.repository context | |
PROJECT_NAME=$(echo "${GITHUB_REPOSITORY}" | cut -d'/' -f2) | |
echo "Project Name: $PROJECT_NAME" | |
docker push harbor.example.com/${{ vars.PROJECT_NAME }}/$PROJECT_NAME:${{ env.version }} | |
docker push harbor.example.com/${{ vars.PROJECT_NAME }}/$PROJECT_NAME:latest | |
env: | |
GITHUB_REPOSITORY: ${{ github.repository }} | |
- name: Deploy to Remote Server | |
if: success() | |
env: | |
SSH_HOST: ${{ secrets.SSH_HOST }} | |
SSH_USERNAME: ${{ secrets.SSH_USERNAME }} | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
DEPLOY_COMMANDS: ${{ secrets.DEPLOY_COMMANDS }} | |
run: | | |
echo "${SSH_PRIVATE_KEY}" > /tmp/private_key | |
chmod 600 /tmp/private_key | |
ssh -o StrictHostKeyChecking=no -i /tmp/private_key ${SSH_USERNAME}@${SSH_HOST} "${DEPLOY_COMMANDS}" | |
shell: bash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment