Skip to content

This is an example of building a CI/CD pipeline using GitHub actions. ✅:octocat:🐋

License

Notifications You must be signed in to change notification settings

szepeviktor/CI-CD

 
 

Repository files navigation

Contributors Forks Stargazers Issues MIT License CI/CD

CI/CD GitHub Action 🚀

A CI/CD workflow for your maven project!

Getting Started

This is an example of building a CI & CD pipeline using GitHub actions.

The CI pipeline depends on maven to compile and test the project's code. JPA/MySQL is supported.

The CD pipeline will create a docker image of the project's code and deploy the image as a docker container along with a MySQL container on a remote server using SSH. The bridge driver of docker networks connects these containers.

In the docker-compose.yml, you can configure the tag of the images. By default, the tag of the project is dev. However, you can change the tag to a specific version to control the deployment. The workflow supports tags in the sem-version format like v*.*.*.

To create a new release using a specific tag, navigate to your GitHub repository. Go to tags -> create new release -> publish the release.

Prepare the production server

  1. Connect to your server

    ssh -i <private key path> <user>@<host>
  2. Add environment variables

    echo "MYSQL_ROOT_PASSWORD=myrootpw
    MYSQL_DATABASE=mydb
    MYSQL_USER=myuser
    MYSQL_PASSWORD=mypw
    DOCKER_IMAGE_NAME=ci-cd
    GITHUB_USER=mathiasreker" > ~/.env

    MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER and MYSQL_PASSWORD can be anything.

    DOCKER_IMAGE_NAME must match the name of the docker image defined in workflow.

    GITHUB_USER must be the user/organisation of the repository in lower case.

This script replaces step 3-7 (optional)

bash <(curl -s https://raw.githubusercontent.com/MathiasReker/CI-CD/develop/install.bash)
  1. Update and upgrade packages

    sudo apt-get update && sudo apt-get -y upgrade
  2. Install docker-compose

    sudo apt-get install -y docker-compose
  3. Generate a new key named github-actions with an empty password

    ssh-keygen -t rsa -b 4096 -f ~/.ssh/github-actions -q -P ""
  4. Copy the content of github-actions into authorized_keys

    cat ~/.ssh/github-actions.pub >> ~/.ssh/authorized_keys
  5. Grap the private key, as you will need it soon

    cat ~/.ssh/github-actions

Install action secrets

Navigate to your GitHub repository. Go to settings -> secrets -> actions.

Add the following secrets:

  • SSH_USER
  • SSH_HOST
  • SSH_PRIVATE_KEY

Install the workflow to your project

  1. Copy the workflow to this path of your repository: /.github/workflows/ci-cd.yml.

  2. Copy the docker-compose.yml to the root folder of your project.

  3. Copy the Dockerfile to the root folder of your project.

Depending on your project, it is necessary to adapt the docker-compose.yml and the Dockerfile.

Package visibility

To see the package, the CD pipeline must run at least once. The pipeline will create a package linked to your GitHub repository.

Navigate to your GitHub repository. Go to the package -> select package settings -> select danger zone -> change visibility -> make it public.

(back to top)

Usage

The CI pipeline is triggered on any change to the code on your repository.

The CD pipeline is triggered on push events. The CD pipeline will be skipped if the CI pipeline fails.

(back to top)

Roadmap

  • CI workflow
  • CD workflow
  • Unit test
  • Integration test using JPA

See the open issues for a complete list of proposed features (and known issues).

(back to top)

Contributing

If you have a suggestion to improve this, please fork the repo and create a pull request. You can also open an issue with the tag "enhancement". Finally, don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

It is distributed under the MIT License. See LICENSE for more information.

(back to top)

About

This is an example of building a CI/CD pipeline using GitHub actions. ✅:octocat:🐋

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages

  • Java 72.2%
  • Shell 24.4%
  • Dockerfile 3.4%