How do I use Docker to implement the PHP command-line program CI/CD?

Source: Internet
Author: User
Tags ssh server docker ps docker hub docker compose codeship

This article tags: ci/cd codeship of the Docker PHP command-line program

Content essentials:-use jet to set up the environment and run tests locally-configure Codeship Pro automatically runs the test each time a new code submission is passed-the update is automatically deployed to the server after the test passes in the previous step

Continuous integration

The application and test suites are already running locally, and the next step is to build some continuous integration systems. Although the server can be set up to do this, the process is slightly more intensive, so a service like Codeship Pro is recommended.

Using Jet for local testing

Before the code is submitted to the Codeship test, it is recommended to install its local version of the Continuous integration platform: Jet. This will push the work faster, and in the example configuration file below, you will need to make the appropriate adjustments based on the actual application.

After installing Jet, create two new files in the root directory of the project--

1) Codeship-services.yml – variant of docker-compose.yml file for codeship

2) codeship-steps.yml– in the process of continuous integration, command and sequence description

CODESHIP-SERVICES.YML files are almost the same as docker-compose.yml . The contents are as follows:

Version: "2.0"  services:    # PHP application  App:    build:.    Links:      -database    encrypted_env_file:. env.encrypted    command:cron-f  # database  :    image:mariadb    encrypted_env_file:. env.encrypted  # Composer  Composer:    image:composer/ Composer    volumes:      -./:/app

CODESHIP-STEPS.YML content is as follows, this file in the example is very simple, in order to execute these commands (one by one) is good. If the application allows, you can also run a few steps in parallel:

-type:serial  steps:  -service:composer    command:install  -service:app    Command:bash Docker /codeship-run.sh

To ensure that the application container and database container are started, you can see that the codeship-steps.yml file invokes a shell script that has not yet been created. If the database is migrated, the test passes. Put the script in the./docker/codeship-run.sh, which reads as follows:

#!/usr/bin/env bash## Ensure that the database was up and runningfunction test_database {    mysqladmin-h "$DB _host"-U "$DB _username "-P" $DB _password "ping}count=0  until (test_database) do    ((count++))  # # This would check up to The Times.  IF[${COUNT}-GT] then    echo "Services didn ' t become ready in Time"    exit1  fi  # # and the script Waits one second between each try  sleep1done## Create the databasemysql-h "$DB _host"-u "$DB _username"-P "$DB _ PASSWORD "-E" CREATE DATABASE IF not EXISTS laravel ' # # Run migrationsphp artisan migrate## run the test suitevendor/bin/php Unit

First, the script tries to connect to the database. The Codeship software automatically launches the application container and database container, but MySQL initialization takes a few seconds, so the test_database () feature must be retried until the database is successfully connected (or tried 100 times). This is outlined in more detail in the Docker documentation for Codeship.

Once the script can connect to the database, it creates the default database (the database is named Laravel). The migration is then run and the database tables and test suites are created through phpunit.

Finally, to test the configuration and run the results, use Jet to run all the steps:

$ jet Steps

If all is well, you will see a bunch of output during the build of the container image and run back to a success message:

{containerrunstdout=step_name: "serial_bash_docker/codeship-run.sh" service_name: "App"}: PHPUnit 5.7.19 by Sebastian Bergmann and contributors. {containerrunstdout=step_name: "serial_bash_docker/codeship-run.sh" service_name: "App"}:.                                                                   1/1 (100%) time:1.09 seconds, Memory:12.00mbok (1test,1 assertion)  {stepfinished=step_name: "serial_bash_docker/ Codeship-run.sh "type:step_finished_type_success}$
Connect the warehouse to the Codeship

If the local code has not been submitted to GitHub or BitBucket. Codeship automatically pulls code from a private or public repository each time a code change is committed, so you only need to set up codeship to monitor that warehouse.

Create a new project in Codeship and connect it to the warehouse:

When you jump out of the box, select Codeship Pro as the project type.

Now the project has been linked to codeship. The next time the code is submitted, Codeship will compile and run with the same codeship-steps.yml as the local. The only problem at this point is to use the local . env file, which is not submitted to the Code warehouse, but there is an easy way to set environment variables without compromising security.

Encrypt environment variables

Because the best practice is not to push the . env file to the continuous integration server, you need to present a secure way to pass the variable to the Codeship method--Encrypt the . env file. "Original address: http://whosmall.com/?post=480"

First, find the AES key (usually on the general page of the project settings) in Codeship and place it in a local root file named Codeship.aes . Do not forget to add this file to the. Gitignore because it is a key that should not be shared:

Next, update the codeship-services.yml file to use the encrypted. env file instead of the plaintext. env file:

Version: "2.0"  services:    # PHP application  App:    build:.    Links:      -database    encrypted_env_file:. env.encrypted    command:cron-f  # database  :    image:mariadb    encrypted_env_file:. env.encrypted  # Composer  Composer:    image:composer/ Composer    volumes:      -./:/app

Use Jet to encrypt the. env file into. env.encrypted, submit the encrypted. env.encrypted file to the repository, and then push it to the remote repository:

$ jet encrypt. env.env.encrypted$ git add-a &&git commit-am "Adding codeship config" $ git push origin

Codeship is compiling the code:

You can also click inside to see the compilation details and each step:

If this is done without error, you will finally see a successful compilation result:

Automatic deployment

While continuous integration services will let people know whether the compilation and test pass, but the use of codeship greater value lies in automating the deployment process, in order to do this, there are a few things to do:

    • Manually configure and deploy the code base on the server
    • There is an SSH key on the server that allows the code to be replaced
    • There is a script on the server to update the code and restart the container

Once you're ready, you can build a deployer container that works by logging in to the SSH server and running the update script at the end of the compilation process. "Original address: http://whosmall.com/?post=480"

This is just one way to deploy code using containers, perhaps not the best approach. Another option is to use a container repository such as the Docker hub to compile, and then update the container directly from the Docker hub. Docker's best practices in production are still being explored, and this approach is more applicable and relatively simple.

Manually deploy code for the first time

This step differs depending on the host service provider, but can be as long as the server meets the Git,docker and Docker compose installed. SSH Login server:

    • Create a new SSH key
    • Give SSH key to read access from code base
    • Cloning code base
    • Set the. env file to use a new App_key and database password
    • Use the Docker-compose up-d–build command to create a mirror and run the container

You can now run Docker PS view, and when you configure this project locally for the first time, you can see that the same two containers are running.

Add Script Update server code

Now in the local version of the code, add a shell script that gets the updated code from the repository and restarts the container:

#!/usr/bin/env bash## Pull the latest Codegit pull Origin master## Rebuild the containersdocker-compose up-d--build## Ru n migrationsdocker exec dockerphpcliexample_app_1 php artisan migrate--force

The file name deploy.sh is located in the docker/folder.

At this point, make sure the file is on the server, so commit the code to the code base and pull it out of the server. Run the command by running on the server: Bash docker/deploy.sh, and make sure the container is still working.

Create a Deployer container

In summary, after compiling and testing, you now need a container to run this deployment script remotely on the Codeship CI server. Create a new directory in the repository named deployer/that contains the Dockerfile file, the. env file, and the execute.sh file.

Dockerfile:
From alpine:latest# Install opensshrun apk update && apk add openssh# Prep for the ssh keyrun mkdir-p "$HOME/.ssh"  RUN Touch$home/.ssh/id_rsa  RUN chmod600$home/.ssh/id_rsa# Add The shell scriptcopy execute.sh execute.shcmd SH execute.sh
. ENV:
user=<server_ssh_username>  host=<server_host>  private_ssh_key=<ssh_key (with linebreaks replaced with ' \ n ') >
Execute.sh:
#!/usr/bin/env bashecho-e $PRIVATE _ssh_key>> $HOME/.ssh/id_rsa  ssh-t-ostricthostkeychecking=no [email Protected] $HOST "CD docker-php-cli-example && sh docker/deploy.sh"

This container will use the environment variables in the. env file, SSH login to the server to run the deployment script.

Let Codeship pro run the Deployer container

In order for Codeship to know the Deployer container, add it to the Codeship-services.yml file:

# Deployer  Deployer:    build:./deployer    Encrypted_env_file:deployer/.env.encryptedcodeship-steps.yml File:-service:deployer    command:sh execute.sh

Encrypt the deployer/.env file so that it can be submitted to the code base without exposing the server's SSH key. As with the master code base, use Jet to encrypt the. env file:

$ jet Encrypt deployer/.env deployer/.env.encrypted

Finally, push the updated code to the GitHub repository to ensure that codeship successfully compiles and deploys the code:

Original address: https://blog.codeship.com/adding-ci-and-cd-to-a-php-command-line-app-with-docker/

written at the end: for Freedom look outside the world, and it this line, not to go to Google data, finally, Amway some speed agent.

Accelerator recommendations Free Solutions Payment Plan Official website
A Red apricot accelerator Free program is not available, stable high-speed Enter 80 percent coupon code WH80, annual pay only 80 yuan/year Official website Direct HTTP://WHOSMALL.COM/GO/YZHX
Azumino Accelerator Best use of foreign trade VPN Minimum ¥30/Month Official website Direct Http://whosmall.com/go/ay
Loco Accelerator Free 2 hours per day Minimum ¥15/Month Official website Direct Http://whosmall.com/go/loco

This article tags: ci/cd codeship of the Docker PHP command-line program

Turn from SUN's BLOG-focus on Internet knowledge, share the spirit of the Internet!

Original Address : " How do I use Docker to implement PHP command-line program CI/CD?" "

Related reading : "Easy-to-start and full-featured motion diagram maker for Mac systems: GIF Brewery 3"

Related reading : Fast and efficient file Check tool on MacOS system: Gemini 2

Related reading : Efficient Task window management tool on MacOS System: Hazeover

Related reading : How does MacOS use Launchbar to upload files to Google drive? "

Related reading : " best Mac App Quick Start and Switch tool: Manico 2.0"

Related reading : Why do I choose Window Tidy as the MacOS split-screen tool? "

Related reading: useful for programmers: 2017 latest in Google's Hosts file download and summary of the various hosts encountered the problem of the solution and configuration of the detailed

Related blog:SUN's blog -focus on Internet knowledge, share the spirit of the Internet! Go and see:www.whosmall.com

Original address: http://whosmall.com/?post=480

How do I use Docker to implement the PHP command-line program CI/CD?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.